Compute array length from type for unconditional panic.
This commit is contained in:
parent
14f303bc14
commit
6b67c46a25
3 changed files with 27 additions and 7 deletions
|
@ -600,13 +600,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Len(place) => {
|
Len(place) => {
|
||||||
let len = match self.get_const(place)? {
|
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::Immediate(src) => src.len(&self.ecx).discard_err()?,
|
||||||
Value::Aggregate { fields, .. } => fields.len() as u64,
|
Value::Aggregate { fields, .. } => fields.len() as u64,
|
||||||
Value::Uninit => match place.ty(self.local_decls(), self.tcx).ty.kind() {
|
Value::Uninit => return None,
|
||||||
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?,
|
}
|
||||||
_ => return None,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
ImmTy::from_scalar(Scalar::from_target_usize(len, self), layout).into()
|
ImmTy::from_scalar(Scalar::from_target_usize(len, self), layout).into()
|
||||||
}
|
}
|
||||||
|
|
8
tests/ui/lint/unconditional_panic_promoted.rs
Normal file
8
tests/ui/lint/unconditional_panic_promoted.rs
Normal 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]
|
||||||
|
}
|
10
tests/ui/lint/unconditional_panic_promoted.stderr
Normal file
10
tests/ui/lint/unconditional_panic_promoted.stderr
Normal 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue