Compute array length from type for unconditional panic.

This commit is contained in:
Camille GILLOT 2024-08-24 18:19:38 +00:00
parent 14f303bc14
commit 6b67c46a25
3 changed files with 27 additions and 7 deletions

View File

@ -600,13 +600,15 @@ fn eval_rvalue(&mut self, rvalue: &Rvalue<'tcx>, dest: &Place<'tcx>) -> Option<(
}
Len(place) => {
let len = match self.get_const(place)? {
Value::Immediate(src) => src.len(&self.ecx).discard_err()?,
Value::Aggregate { fields, .. } => fields.len() as u64,
Value::Uninit => match place.ty(self.local_decls(), self.tcx).ty.kind() {
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?,
_ => return None,
},
let len = if let ty::Array(_, n) = place.ty(self.local_decls(), self.tcx).ty.kind()
{
n.try_eval_target_usize(self.tcx, self.param_env)?
} else {
match self.get_const(place)? {
Value::Immediate(src) => src.len(&self.ecx).discard_err()?,
Value::Aggregate { fields, .. } => fields.len() as u64,
Value::Uninit => return None,
}
};
ImmTy::from_scalar(Scalar::from_target_usize(len, self), layout).into()
}

View File

@ -0,0 +1,8 @@
//@ build-fail
fn main() {
// MIR encodes this as a reborrow from a promoted constant.
// But the array lenth can still be gotten from the type.
let slice = &[0, 1];
let _ = slice[2]; //~ ERROR: this operation will panic at runtime [unconditional_panic]
}

View File

@ -0,0 +1,10 @@
error: this operation will panic at runtime
--> $DIR/unconditional_panic_promoted.rs:7:13
|
LL | let _ = slice[2];
| ^^^^^^^^ index out of bounds: the length is 2 but the index is 2
|
= note: `#[deny(unconditional_panic)]` on by default
error: aborting due to 1 previous error