Skip to content

Commit fc63543

Browse files
committed
Support array length.
1 parent 22986b7 commit fc63543

File tree

32 files changed

+782
-26
lines changed

32 files changed

+782
-26
lines changed

compiler/rustc_middle/src/mir/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,13 @@ impl<'tcx> PlaceRef<'tcx> {
17511751
}
17521752
}
17531753

1754+
impl From<Local> for PlaceRef<'_> {
1755+
#[inline]
1756+
fn from(local: Local) -> Self {
1757+
PlaceRef { local, projection: &[] }
1758+
}
1759+
}
1760+
17541761
impl Debug for Place<'_> {
17551762
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
17561763
for elem in self.projection.iter().rev() {

compiler/rustc_mir_dataflow/src/value_analysis.rs

+36
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
581581
}
582582
}
583583

584+
/// Retrieve the value stored for a place, or ⊤ if it is not tracked.
585+
pub fn get_len(&self, place: PlaceRef<'_>, map: &Map) -> V {
586+
match map.find_len(place) {
587+
Some(place) => self.get_idx(place, map),
588+
None => V::TOP,
589+
}
590+
}
591+
584592
/// Retrieve the value stored for a place index, or ⊤ if it is not tracked.
585593
pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V {
586594
match &self.0 {
@@ -750,6 +758,24 @@ impl Map {
750758
self.value_count += 1;
751759
}
752760

761+
if let Some(ref_ty) = ty.builtin_deref(true) && let ty::Slice(..) = ref_ty.ty.kind() {
762+
assert!(self.places[place].value_index.is_none(), "slices are not scalars");
763+
764+
// Prepend new child to the linked list.
765+
let len = self.places.push(PlaceInfo::new(Some(TrackElem::DerefLen)));
766+
self.places[len].next_sibling = self.places[place].first_child;
767+
self.places[place].first_child = Some(len);
768+
769+
let old = self.projections.insert((place, TrackElem::DerefLen), len);
770+
assert!(old.is_none());
771+
772+
// Allocate a value slot if it doesn't have one.
773+
if self.places[len].value_index.is_none() {
774+
self.places[len].value_index = Some(self.value_count.into());
775+
self.value_count += 1;
776+
}
777+
}
778+
753779
// Recurse with all fields of this place.
754780
iter_fields(ty, tcx, param_env, |variant, field, ty| {
755781
worklist.push_back((
@@ -818,6 +844,11 @@ impl Map {
818844
self.find_extra(place, [TrackElem::Discriminant])
819845
}
820846

847+
/// Locates the given place and applies `DerefLen`, if it exists in the tree.
848+
pub fn find_len(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
849+
self.find_extra(place, [TrackElem::DerefLen])
850+
}
851+
821852
/// Iterate over all direct children.
822853
pub fn children(&self, parent: PlaceIndex) -> impl Iterator<Item = PlaceIndex> + '_ {
823854
Children::new(self, parent)
@@ -969,6 +1000,8 @@ pub enum TrackElem {
9691000
Field(FieldIdx),
9701001
Variant(VariantIdx),
9711002
Discriminant,
1003+
// Length of a slice.
1004+
DerefLen,
9721005
}
9731006

9741007
impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
@@ -1108,6 +1141,9 @@ fn debug_with_context_rec<V: Debug + Eq>(
11081141
format!("{}.{}", place_str, field.index())
11091142
}
11101143
}
1144+
TrackElem::DerefLen => {
1145+
format!("Len(*{})", place_str)
1146+
}
11111147
};
11121148
debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
11131149
}

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+30
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
184184
}
185185
}
186186
}
187+
Rvalue::Cast(
188+
CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize),
189+
operand,
190+
_,
191+
) => {
192+
let pointer = self.handle_operand(operand, state);
193+
state.assign(target.as_ref(), pointer, self.map());
194+
195+
if let Some(target_len) = self.map().find_len(target.as_ref())
196+
&& let operand_ty = operand.ty(self.local_decls, self.tcx)
197+
&& let Some(operand_ty) = operand_ty.builtin_deref(true)
198+
&& let ty::Array(_, len) = operand_ty.ty.kind()
199+
&& let Some(len) = ConstantKind::Ty(*len).eval(self.tcx, self.param_env).try_to_scalar_int()
200+
{
201+
state.insert_value_idx(target_len, FlatSet::Elem(len), self.map());
202+
}
203+
}
187204
_ => self.super_assign(target, rvalue, state),
188205
}
189206
}
@@ -194,6 +211,19 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
194211
state: &mut State<Self::Value>,
195212
) -> ValueOrPlace<Self::Value> {
196213
let val = match rvalue {
214+
Rvalue::Len(place) => {
215+
let place_ty = place.ty(self.local_decls, self.tcx);
216+
if let ty::Array(_, len) = place_ty.ty.kind() {
217+
ConstantKind::Ty(*len)
218+
.eval(self.tcx, self.param_env)
219+
.try_to_scalar_int()
220+
.map_or(FlatSet::Top, FlatSet::Elem)
221+
} else if let [ProjectionElem::Deref] = place.projection[..] {
222+
state.get_len(place.local.into(), self.map())
223+
} else {
224+
FlatSet::Top
225+
}
226+
}
197227
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
198228
match self.eval_operand(operand, state) {
199229
FlatSet::Elem(op) => self

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.panic-abort.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
StorageLive(_5);
3535
StorageLive(_6);
3636
_6 = const 3_usize;
37-
_7 = const 3_usize;
37+
_7 = Len((*_1));
3838
- _8 = Lt(_6, _7);
3939
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
40-
+ _8 = const false;
41-
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind unreachable];
40+
+ _8 = Lt(const 3_usize, _7);
41+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind unreachable];
4242
}
4343

4444
bb1: {

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.panic-unwind.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
StorageLive(_5);
3535
StorageLive(_6);
3636
_6 = const 3_usize;
37-
_7 = const 3_usize;
37+
_7 = Len((*_1));
3838
- _8 = Lt(_6, _7);
3939
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
40-
+ _8 = const false;
41-
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind continue];
40+
+ _8 = Lt(const 3_usize, _7);
41+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind continue];
4242
}
4343

4444
bb1: {

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.panic-abort.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
StorageLive(_5);
3535
StorageLive(_6);
3636
_6 = const 3_usize;
37-
_7 = const 3_usize;
37+
_7 = Len((*_1));
3838
- _8 = Lt(_6, _7);
3939
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind unreachable];
40-
+ _8 = const false;
41-
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind unreachable];
40+
+ _8 = Lt(const 3_usize, _7);
41+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind unreachable];
4242
}
4343

4444
bb1: {

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.panic-unwind.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
StorageLive(_5);
3535
StorageLive(_6);
3636
_6 = const 3_usize;
37-
_7 = const 3_usize;
37+
_7 = Len((*_1));
3838
- _8 = Lt(_6, _7);
3939
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> [success: bb1, unwind continue];
40-
+ _8 = const false;
41-
+ assert(const false, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 3_usize) -> [success: bb1, unwind continue];
40+
+ _8 = Lt(const 3_usize, _7);
41+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> [success: bb1, unwind continue];
4242
}
4343

4444
bb1: {

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// unit-test: ConstProp
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3-
// compile-flags: -Zmir-enable-passes=+NormalizeArrayLen
4-
53
// EMIT_MIR_FOR_EACH_BIT_WIDTH
4+
65
// EMIT_MIR bad_op_unsafe_oob_for_slices.main.ConstProp.diff
76
#[allow(unconditional_panic)]
87
fn main() {

tests/mir-opt/const_prop/large_array_index.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// unit-test: ConstProp
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3-
// compile-flags: -Zmir-enable-passes=+NormalizeArrayLen
43
// EMIT_MIR_FOR_EACH_BIT_WIDTH
54

65
// EMIT_MIR large_array_index.main.ConstProp.diff

tests/mir-opt/const_prop/repeat.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// unit-test: ConstProp
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3-
// compile-flags: -Zmir-enable-passes=+NormalizeArrayLen
43
// EMIT_MIR_FOR_EACH_BIT_WIDTH
54

65
// EMIT_MIR repeat.main.ConstProp.diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: ();
6+
let _1: u32;
7+
let mut _2: [u32; 4];
8+
let _3: usize;
9+
let mut _4: usize;
10+
let mut _5: bool;
11+
scope 1 {
12+
debug x => _1;
13+
}
14+
15+
bb0: {
16+
StorageLive(_1);
17+
StorageLive(_2);
18+
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
19+
StorageLive(_3);
20+
_3 = const 2_usize;
21+
- _4 = Len(_2);
22+
- _5 = Lt(_3, _4);
23+
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
27+
}
28+
29+
bb1: {
30+
_1 = _2[_3];
31+
StorageDead(_3);
32+
StorageDead(_2);
33+
_0 = const ();
34+
StorageDead(_1);
35+
return;
36+
}
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: ();
6+
let _1: u32;
7+
let mut _2: [u32; 4];
8+
let _3: usize;
9+
let mut _4: usize;
10+
let mut _5: bool;
11+
scope 1 {
12+
debug x => _1;
13+
}
14+
15+
bb0: {
16+
StorageLive(_1);
17+
StorageLive(_2);
18+
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
19+
StorageLive(_3);
20+
_3 = const 2_usize;
21+
- _4 = Len(_2);
22+
- _5 = Lt(_3, _4);
23+
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
27+
}
28+
29+
bb1: {
30+
_1 = _2[_3];
31+
StorageDead(_3);
32+
StorageDead(_2);
33+
_0 = const ();
34+
StorageDead(_1);
35+
return;
36+
}
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: ();
6+
let _1: u32;
7+
let mut _2: [u32; 4];
8+
let _3: usize;
9+
let mut _4: usize;
10+
let mut _5: bool;
11+
scope 1 {
12+
debug x => _1;
13+
}
14+
15+
bb0: {
16+
StorageLive(_1);
17+
StorageLive(_2);
18+
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
19+
StorageLive(_3);
20+
_3 = const 2_usize;
21+
- _4 = Len(_2);
22+
- _5 = Lt(_3, _4);
23+
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
27+
}
28+
29+
bb1: {
30+
_1 = _2[_3];
31+
StorageDead(_3);
32+
StorageDead(_2);
33+
_0 = const ();
34+
StorageDead(_1);
35+
return;
36+
}
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: ();
6+
let _1: u32;
7+
let mut _2: [u32; 4];
8+
let _3: usize;
9+
let mut _4: usize;
10+
let mut _5: bool;
11+
scope 1 {
12+
debug x => _1;
13+
}
14+
15+
bb0: {
16+
StorageLive(_1);
17+
StorageLive(_2);
18+
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
19+
StorageLive(_3);
20+
_3 = const 2_usize;
21+
- _4 = Len(_2);
22+
- _5 = Lt(_3, _4);
23+
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
27+
}
28+
29+
bb1: {
30+
_1 = _2[_3];
31+
StorageDead(_3);
32+
StorageDead(_2);
33+
_0 = const ();
34+
StorageDead(_1);
35+
return;
36+
}
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
2+
// unit-test: DataflowConstProp
3+
// EMIT_MIR_FOR_EACH_BIT_WIDTH
4+
5+
// EMIT_MIR array_index.main.DataflowConstProp.diff
6+
fn main() {
7+
let x: u32 = [0, 1, 2, 3][2];
8+
}

0 commit comments

Comments
 (0)