Support array length.

This commit is contained in:
Camille GILLOT 2023-04-23 16:15:22 +00:00
parent 22986b72e5
commit fc63543792
32 changed files with 782 additions and 26 deletions

View File

@ -1751,6 +1751,13 @@ impl<'tcx> PlaceRef<'tcx> {
}
}
impl From<Local> for PlaceRef<'_> {
#[inline]
fn from(local: Local) -> Self {
PlaceRef { local, projection: &[] }
}
}
impl Debug for Place<'_> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
for elem in self.projection.iter().rev() {

View File

@ -581,6 +581,14 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
}
}
/// Retrieve the value stored for a place, or if it is not tracked.
pub fn get_len(&self, place: PlaceRef<'_>, map: &Map) -> V {
match map.find_len(place) {
Some(place) => self.get_idx(place, map),
None => V::TOP,
}
}
/// Retrieve the value stored for a place index, or if it is not tracked.
pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V {
match &self.0 {
@ -750,6 +758,24 @@ impl Map {
self.value_count += 1;
}
if let Some(ref_ty) = ty.builtin_deref(true) && let ty::Slice(..) = ref_ty.ty.kind() {
assert!(self.places[place].value_index.is_none(), "slices are not scalars");
// Prepend new child to the linked list.
let len = self.places.push(PlaceInfo::new(Some(TrackElem::DerefLen)));
self.places[len].next_sibling = self.places[place].first_child;
self.places[place].first_child = Some(len);
let old = self.projections.insert((place, TrackElem::DerefLen), len);
assert!(old.is_none());
// Allocate a value slot if it doesn't have one.
if self.places[len].value_index.is_none() {
self.places[len].value_index = Some(self.value_count.into());
self.value_count += 1;
}
}
// Recurse with all fields of this place.
iter_fields(ty, tcx, param_env, |variant, field, ty| {
worklist.push_back((
@ -818,6 +844,11 @@ impl Map {
self.find_extra(place, [TrackElem::Discriminant])
}
/// Locates the given place and applies `DerefLen`, if it exists in the tree.
pub fn find_len(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
self.find_extra(place, [TrackElem::DerefLen])
}
/// Iterate over all direct children.
pub fn children(&self, parent: PlaceIndex) -> impl Iterator<Item = PlaceIndex> + '_ {
Children::new(self, parent)
@ -969,6 +1000,8 @@ pub enum TrackElem {
Field(FieldIdx),
Variant(VariantIdx),
Discriminant,
// Length of a slice.
DerefLen,
}
impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
@ -1108,6 +1141,9 @@ fn debug_with_context_rec<V: Debug + Eq>(
format!("{}.{}", place_str, field.index())
}
}
TrackElem::DerefLen => {
format!("Len(*{})", place_str)
}
};
debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
}

View File

@ -184,6 +184,23 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
}
}
}
Rvalue::Cast(
CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize),
operand,
_,
) => {
let pointer = self.handle_operand(operand, state);
state.assign(target.as_ref(), pointer, self.map());
if let Some(target_len) = self.map().find_len(target.as_ref())
&& let operand_ty = operand.ty(self.local_decls, self.tcx)
&& let Some(operand_ty) = operand_ty.builtin_deref(true)
&& let ty::Array(_, len) = operand_ty.ty.kind()
&& let Some(len) = ConstantKind::Ty(*len).eval(self.tcx, self.param_env).try_to_scalar_int()
{
state.insert_value_idx(target_len, FlatSet::Elem(len), self.map());
}
}
_ => self.super_assign(target, rvalue, state),
}
}
@ -194,6 +211,19 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
state: &mut State<Self::Value>,
) -> ValueOrPlace<Self::Value> {
let val = match rvalue {
Rvalue::Len(place) => {
let place_ty = place.ty(self.local_decls, self.tcx);
if let ty::Array(_, len) = place_ty.ty.kind() {
ConstantKind::Ty(*len)
.eval(self.tcx, self.param_env)
.try_to_scalar_int()
.map_or(FlatSet::Top, FlatSet::Elem)
} else if let [ProjectionElem::Deref] = place.projection[..] {
state.get_len(place.local.into(), self.map())
} else {
FlatSet::Top
}
}
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
match self.eval_operand(operand, state) {
FlatSet::Elem(op) => self

View File

@ -34,11 +34,11 @@
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
_7 = const 3_usize;
_7 = Len((*_1));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
+ _8 = const false;
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind unreachable];
+ _8 = Lt(const 3_usize, _7);
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind unreachable];
}
bb1: {

View File

@ -34,11 +34,11 @@
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
_7 = const 3_usize;
_7 = Len((*_1));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
+ _8 = const false;
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind continue];
+ _8 = Lt(const 3_usize, _7);
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind continue];
}
bb1: {

View File

@ -34,11 +34,11 @@
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
_7 = const 3_usize;
_7 = Len((*_1));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
+ _8 = const false;
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind unreachable];
+ _8 = Lt(const 3_usize, _7);
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind unreachable];
}
bb1: {

View File

@ -34,11 +34,11 @@
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
_7 = const 3_usize;
_7 = Len((*_1));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
+ _8 = const false;
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind continue];
+ _8 = Lt(const 3_usize, _7);
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind continue];
}
bb1: {

View File

@ -1,8 +1,7 @@
// unit-test: ConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// compile-flags: -Zmir-enable-passes=+NormalizeArrayLen
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR bad_op_unsafe_oob_for_slices.main.ConstProp.diff
#[allow(unconditional_panic)]
fn main() {

View File

@ -1,6 +1,5 @@
// unit-test: ConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// compile-flags: -Zmir-enable-passes=+NormalizeArrayLen
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR large_array_index.main.ConstProp.diff

View File

@ -1,6 +1,5 @@
// unit-test: ConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// compile-flags: -Zmir-enable-passes=+NormalizeArrayLen
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR repeat.main.ConstProp.diff

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: [u32; 4];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: [u32; 4];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: [u32; 4];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: [u32; 4];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,8 @@
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// unit-test: DataflowConstProp
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR array_index.main.DataflowConstProp.diff
fn main() {
let x: u32 = [0, 1, 2, 3][2];
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u8;
let mut _2: [u8; 5000];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u8; 5000];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _4 = const 5000_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 5000_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u8;
let mut _2: [u8; 5000];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u8; 5000];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _4 = const 5000_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 5000_usize, const 2_usize) -> [success: bb1, unwind continue];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u8;
let mut _2: [u8; 5000];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u8; 5000];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _4 = const 5000_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 5000_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,38 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u8;
let mut _2: [u8; 5000];
let _3: usize;
let mut _4: usize;
let mut _5: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = [const 0_u8; 5000];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _4 = const 5000_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 5000_usize, const 2_usize) -> [success: bb1, unwind continue];
}
bb1: {
_1 = _2[_3];
StorageDead(_3);
StorageDead(_2);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,9 @@
// unit-test: DataflowConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR large_array_index.main.DataflowConstProp.diff
fn main() {
// check that we don't propagate this, because it's too large
let x: u8 = [0_u8; 5000][2];
}

View File

@ -0,0 +1,42 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: u32;
let mut _3: [u32; 8];
let _4: usize;
let mut _5: usize;
let mut _6: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_3 = [const 42_u32; 8];
StorageLive(_4);
_4 = const 2_usize;
- _5 = Len(_3);
- _6 = Lt(_4, _5);
- assert(move _6, "index out of bounds: the length is {} but the index is {}", move _5, _4) -> [success: bb1, unwind unreachable];
+ _5 = const 8_usize;
+ _6 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 8_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_2 = _3[_4];
_1 = Add(move _2, const 0_u32);
StorageDead(_2);
StorageDead(_4);
StorageDead(_3);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,42 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: u32;
let mut _3: [u32; 8];
let _4: usize;
let mut _5: usize;
let mut _6: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_3 = [const 42_u32; 8];
StorageLive(_4);
_4 = const 2_usize;
- _5 = Len(_3);
- _6 = Lt(_4, _5);
- assert(move _6, "index out of bounds: the length is {} but the index is {}", move _5, _4) -> [success: bb1, unwind continue];
+ _5 = const 8_usize;
+ _6 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 8_usize, const 2_usize) -> [success: bb1, unwind continue];
}
bb1: {
_2 = _3[_4];
_1 = Add(move _2, const 0_u32);
StorageDead(_2);
StorageDead(_4);
StorageDead(_3);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,42 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: u32;
let mut _3: [u32; 8];
let _4: usize;
let mut _5: usize;
let mut _6: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_3 = [const 42_u32; 8];
StorageLive(_4);
_4 = const 2_usize;
- _5 = Len(_3);
- _6 = Lt(_4, _5);
- assert(move _6, "index out of bounds: the length is {} but the index is {}", move _5, _4) -> [success: bb1, unwind unreachable];
+ _5 = const 8_usize;
+ _6 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 8_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_2 = _3[_4];
_1 = Add(move _2, const 0_u32);
StorageDead(_2);
StorageDead(_4);
StorageDead(_3);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,42 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: u32;
let mut _3: [u32; 8];
let _4: usize;
let mut _5: usize;
let mut _6: bool;
scope 1 {
debug x => _1;
}
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_3 = [const 42_u32; 8];
StorageLive(_4);
_4 = const 2_usize;
- _5 = Len(_3);
- _6 = Lt(_4, _5);
- assert(move _6, "index out of bounds: the length is {} but the index is {}", move _5, _4) -> [success: bb1, unwind continue];
+ _5 = const 8_usize;
+ _6 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 8_usize, const 2_usize) -> [success: bb1, unwind continue];
}
bb1: {
_2 = _3[_4];
_1 = Add(move _2, const 0_u32);
StorageDead(_2);
StorageDead(_4);
StorageDead(_3);
_0 = const ();
StorageDead(_1);
return;
}
}

View File

@ -0,0 +1,8 @@
// unit-test: DataflowConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR repeat.main.DataflowConstProp.diff
fn main() {
let x: u32 = [42; 8][2] + 0;
}

View File

@ -0,0 +1,46 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: &[u32];
let mut _3: &[u32; 3];
let _4: &[u32; 3];
let _5: [u32; 3];
let _6: usize;
let mut _7: usize;
let mut _8: bool;
let mut _9: &[u32; 3];
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_4 = _9;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
StorageDead(_3);
StorageLive(_6);
_6 = const 1_usize;
- _7 = Len((*_2));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
+ _7 = const 3_usize;
+ _8 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_1 = (*_2)[_6];
StorageDead(_6);
StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
}
}

View File

@ -0,0 +1,46 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: &[u32];
let mut _3: &[u32; 3];
let _4: &[u32; 3];
let _5: [u32; 3];
let _6: usize;
let mut _7: usize;
let mut _8: bool;
let mut _9: &[u32; 3];
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_4 = _9;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
StorageDead(_3);
StorageLive(_6);
_6 = const 1_usize;
- _7 = Len((*_2));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
+ _7 = const 3_usize;
+ _8 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
}
bb1: {
_1 = (*_2)[_6];
StorageDead(_6);
StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
}
}

View File

@ -0,0 +1,46 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: &[u32];
let mut _3: &[u32; 3];
let _4: &[u32; 3];
let _5: [u32; 3];
let _6: usize;
let mut _7: usize;
let mut _8: bool;
let mut _9: &[u32; 3];
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_4 = _9;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
StorageDead(_3);
StorageLive(_6);
_6 = const 1_usize;
- _7 = Len((*_2));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
+ _7 = const 3_usize;
+ _8 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
}
bb1: {
_1 = (*_2)[_6];
StorageDead(_6);
StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
}
}

View File

@ -0,0 +1,46 @@
- // MIR for `main` before DataflowConstProp
+ // MIR for `main` after DataflowConstProp
fn main() -> () {
let mut _0: ();
let _1: u32;
let mut _2: &[u32];
let mut _3: &[u32; 3];
let _4: &[u32; 3];
let _5: [u32; 3];
let _6: usize;
let mut _7: usize;
let mut _8: bool;
let mut _9: &[u32; 3];
bb0: {
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_4 = _9;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
StorageDead(_3);
StorageLive(_6);
_6 = const 1_usize;
- _7 = Len((*_2));
- _8 = Lt(_6, _7);
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
+ _7 = const 3_usize;
+ _8 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
}
bb1: {
_1 = (*_2)[_6];
StorageDead(_6);
StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
}
}

View File

@ -0,0 +1,9 @@
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// unit-test: DataflowConstProp
// compile-flags: -Zmir-enable-passes=+InstSimplify
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR slice_len.main.DataflowConstProp.diff
fn main() {
(&[1u32, 2, 3] as &[u32])[1];
}

View File

@ -33,12 +33,10 @@
_3 = &_4;
_2 = move _3 as &[T] (PointerCoercion(Unsize));
StorageDead(_3);
_8 = Len((*_2));
_8 = const 3_usize;
_9 = const 3_usize;
- _10 = Eq(move _8, const 3_usize);
- switchInt(move _10) -> [0: bb1, otherwise: bb2];
+ nop;
+ switchInt(move _8) -> [3: bb2, otherwise: bb1];
_10 = const true;
goto -> bb2;
}
bb1: {

View File

@ -33,12 +33,10 @@
_3 = &_4;
_2 = move _3 as &[T] (PointerCoercion(Unsize));
StorageDead(_3);
_8 = Len((*_2));
_8 = const 3_usize;
_9 = const 3_usize;
- _10 = Eq(move _8, const 3_usize);
- switchInt(move _10) -> [0: bb1, otherwise: bb2];
+ nop;
+ switchInt(move _8) -> [3: bb2, otherwise: bb1];
_10 = const true;
goto -> bb2;
}
bb1: {