From 529b94ea6a3f0606d38e4bb32ec5eb692c3c0d71 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Tue, 4 Jun 2019 21:51:30 -0400 Subject: [PATCH] [const-prop] Fix ICE when casting function pointers This fixes an ICE when building libcore with `-Z mir-opt-level=3`. --- src/librustc_mir/transform/const_prop.rs | 10 ++++----- src/test/mir-opt/const_prop/reify_fn_ptr.rs | 25 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/test/mir-opt/const_prop/reify_fn_ptr.rs diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index dbaa4e557c66..1d4d01e60aad 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -525,12 +525,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> { source_info: SourceInfo, ) { trace!("attepting to replace {:?} with {:?}", rval, value); - self.ecx.validate_operand( - value, - vec![], - None, - true, - ).expect("value should already be a valid const"); + if let Err(e) = self.ecx.validate_operand(value, vec![], None, true) { + trace!("validation error, attempt failed: {:?}", e); + return; + } // FIXME> figure out what tho do when try_read_immediate fails let imm = self.use_ecx(source_info, |this| { diff --git a/src/test/mir-opt/const_prop/reify_fn_ptr.rs b/src/test/mir-opt/const_prop/reify_fn_ptr.rs new file mode 100644 index 000000000000..809eb19ade89 --- /dev/null +++ b/src/test/mir-opt/const_prop/reify_fn_ptr.rs @@ -0,0 +1,25 @@ +fn main() { + let _ = main as usize as *const fn(); +} + +// END RUST SOURCE +// START rustc.main.ConstProp.before.mir +// bb0: { +// ... +// _3 = const main as fn() (Pointer(ReifyFnPointer)); +// _2 = move _3 as usize (Misc); +// ... +// _1 = move _2 as *const fn() (Misc); +// ... +// } +// END rustc.main.ConstProp.before.mir +// START rustc.main.ConstProp.after.mir +// bb0: { +// ... +// _3 = const Scalar(AllocId(1).0x0) : fn(); +// _2 = move _3 as usize (Misc); +// ... +// _1 = const Scalar(AllocId(1).0x0) : *const fn(); +// ... +// } +// END rustc.main.ConstProp.after.mir