diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index d0bbca08a4068..f014852f39fc6 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -456,9 +456,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { let value = &self.eval_operand(cond, location)?; trace!("assertion on {:?} should be {:?}", value, expected); - let expected = Scalar::from_bool(expected); let value_const = self.use_ecx(location, |this| this.ecx.read_scalar(value))?; + if value_const == Scalar::from_bool(false) { + // Skip the assertion if the condition is evaluated to false. + return None; + } + + let expected = Scalar::from_bool(expected); + if expected != value_const { // Poison all places this operand references so that further code // doesn't use the invalid value diff --git a/tests/ui/const_prop/issue-119908.rs b/tests/ui/const_prop/issue-119908.rs new file mode 100644 index 0000000000000..ecb297709de65 --- /dev/null +++ b/tests/ui/const_prop/issue-119908.rs @@ -0,0 +1,7 @@ +// run-pass +fn main() { + let x: [i32; 0] = []; + if x.len() > 0 { + x[0]; + } +}