Skip to content

Commit 9766ee0

Browse files
committed
Fix struct field tracking and add tests for it
1 parent bfbca6c commit 9766ee0

File tree

6 files changed

+145
-10
lines changed

6 files changed

+145
-10
lines changed

compiler/rustc_mir_dataflow/src/value_analysis.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ pub trait ValueAnalysis<'tcx> {
151151
) -> ValueOrPlace<Self::Value> {
152152
match rvalue {
153153
Rvalue::Use(operand) => self.handle_operand(operand, state),
154-
Rvalue::CopyForDeref(place) => {
155-
self.handle_operand(&Operand::Copy(*place), state)
156-
}
154+
Rvalue::CopyForDeref(place) => self.handle_operand(&Operand::Copy(*place), state),
157155
Rvalue::Ref(..) | Rvalue::AddressOf(..) => {
158156
// We don't track such places.
159157
ValueOrPlace::top()
@@ -638,9 +636,7 @@ impl Map {
638636
return;
639637
}
640638
projection.push(PlaceElem::Field(field, ty));
641-
self.register_with_filter_rec(
642-
tcx, local, projection, ty, filter, exclude,
643-
);
639+
self.register_with_filter_rec(tcx, local, projection, ty, filter, exclude);
644640
projection.pop();
645641
});
646642
}
@@ -842,13 +838,17 @@ fn iter_fields<'tcx>(
842838
}
843839
}
844840
ty::Adt(def, substs) => {
841+
if def.is_union() {
842+
return;
843+
}
845844
for (v_index, v_def) in def.variants().iter_enumerated() {
845+
let variant = if def.is_struct() { None } else { Some(v_index) };
846846
for (f_index, f_def) in v_def.fields.iter().enumerate() {
847847
let field_ty = f_def.ty(tcx, substs);
848848
let field_ty = tcx
849849
.try_normalize_erasing_regions(ty::ParamEnv::reveal_all(), field_ty)
850850
.unwrap_or(field_ty);
851-
f(Some(v_index), f_index.into(), field_ty);
851+
f(variant, f_index.into(), field_ty);
852852
}
853853
}
854854
}

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use rustc_data_structures::fx::FxHashMap;
77
use rustc_middle::mir::visit::{MutVisitor, Visitor};
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::{self, Ty, TyCtxt};
10-
use rustc_mir_dataflow::value_analysis::{
11-
Map, State, TrackElem, ValueAnalysis, ValueOrPlace,
12-
};
10+
use rustc_mir_dataflow::value_analysis::{Map, State, TrackElem, ValueAnalysis, ValueOrPlace};
1311
use rustc_mir_dataflow::{lattice::FlatSet, Analysis, ResultsVisitor, SwitchIntEdgeEffects};
1412
use rustc_span::DUMMY_SP;
1513

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/enum.rs:+0:11: +0:11
6+
let _1: E; // in scope 0 at $DIR/enum.rs:+1:9: +1:10
7+
let mut _3: isize; // in scope 0 at $DIR/enum.rs:+2:23: +2:31
8+
scope 1 {
9+
debug e => _1; // in scope 1 at $DIR/enum.rs:+1:9: +1:10
10+
let _2: i32; // in scope 1 at $DIR/enum.rs:+2:9: +2:10
11+
let _4: i32; // in scope 1 at $DIR/enum.rs:+2:29: +2:30
12+
let _5: i32; // in scope 1 at $DIR/enum.rs:+2:44: +2:45
13+
scope 2 {
14+
debug x => _2; // in scope 2 at $DIR/enum.rs:+2:9: +2:10
15+
}
16+
scope 3 {
17+
debug x => _4; // in scope 3 at $DIR/enum.rs:+2:29: +2:30
18+
}
19+
scope 4 {
20+
debug x => _5; // in scope 4 at $DIR/enum.rs:+2:44: +2:45
21+
}
22+
}
23+
24+
bb0: {
25+
StorageLive(_1); // scope 0 at $DIR/enum.rs:+1:9: +1:10
26+
Deinit(_1); // scope 0 at $DIR/enum.rs:+1:13: +1:21
27+
((_1 as V1).0: i32) = const 0_i32; // scope 0 at $DIR/enum.rs:+1:13: +1:21
28+
discriminant(_1) = 0; // scope 0 at $DIR/enum.rs:+1:13: +1:21
29+
StorageLive(_2); // scope 1 at $DIR/enum.rs:+2:9: +2:10
30+
_3 = discriminant(_1); // scope 1 at $DIR/enum.rs:+2:19: +2:20
31+
switchInt(move _3) -> [0_isize: bb3, 1_isize: bb1, otherwise: bb2]; // scope 1 at $DIR/enum.rs:+2:13: +2:20
32+
}
33+
34+
bb1: {
35+
StorageLive(_5); // scope 1 at $DIR/enum.rs:+2:44: +2:45
36+
_5 = ((_1 as V2).0: i32); // scope 1 at $DIR/enum.rs:+2:44: +2:45
37+
_2 = _5; // scope 4 at $DIR/enum.rs:+2:50: +2:51
38+
StorageDead(_5); // scope 1 at $DIR/enum.rs:+2:50: +2:51
39+
goto -> bb4; // scope 1 at $DIR/enum.rs:+2:50: +2:51
40+
}
41+
42+
bb2: {
43+
unreachable; // scope 1 at $DIR/enum.rs:+2:19: +2:20
44+
}
45+
46+
bb3: {
47+
StorageLive(_4); // scope 1 at $DIR/enum.rs:+2:29: +2:30
48+
_4 = ((_1 as V1).0: i32); // scope 1 at $DIR/enum.rs:+2:29: +2:30
49+
_2 = _4; // scope 3 at $DIR/enum.rs:+2:35: +2:36
50+
StorageDead(_4); // scope 1 at $DIR/enum.rs:+2:35: +2:36
51+
goto -> bb4; // scope 1 at $DIR/enum.rs:+2:35: +2:36
52+
}
53+
54+
bb4: {
55+
_0 = const (); // scope 0 at $DIR/enum.rs:+0:11: +3:2
56+
StorageDead(_2); // scope 1 at $DIR/enum.rs:+3:1: +3:2
57+
StorageDead(_1); // scope 0 at $DIR/enum.rs:+3:1: +3:2
58+
return; // scope 0 at $DIR/enum.rs:+3:2: +3:2
59+
}
60+
}
61+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// unit-test: DataflowConstProp
2+
3+
// Not trackable, because variants could be aliased.
4+
enum E {
5+
V1(i32),
6+
V2(i32)
7+
}
8+
9+
// EMIT_MIR enum.main.DataflowConstProp.diff
10+
fn main() {
11+
let e = E::V1(0);
12+
let x = match e { E::V1(x) => x, E::V2(x) => x };
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- // MIR for `main` before DataflowConstProp
2+
+ // MIR for `main` after DataflowConstProp
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/struct.rs:+0:11: +0:11
6+
let mut _1: S; // in scope 0 at $DIR/struct.rs:+1:9: +1:14
7+
let mut _3: i32; // in scope 0 at $DIR/struct.rs:+2:13: +2:16
8+
let mut _5: i32; // in scope 0 at $DIR/struct.rs:+4:13: +4:14
9+
let mut _6: i32; // in scope 0 at $DIR/struct.rs:+4:17: +4:20
10+
scope 1 {
11+
debug s => _1; // in scope 1 at $DIR/struct.rs:+1:9: +1:14
12+
let _2: i32; // in scope 1 at $DIR/struct.rs:+2:9: +2:10
13+
scope 2 {
14+
debug a => _2; // in scope 2 at $DIR/struct.rs:+2:9: +2:10
15+
let _4: i32; // in scope 2 at $DIR/struct.rs:+4:9: +4:10
16+
scope 3 {
17+
debug b => _4; // in scope 3 at $DIR/struct.rs:+4:9: +4:10
18+
}
19+
}
20+
}
21+
22+
bb0: {
23+
StorageLive(_1); // scope 0 at $DIR/struct.rs:+1:9: +1:14
24+
Deinit(_1); // scope 0 at $DIR/struct.rs:+1:17: +1:21
25+
(_1.0: i32) = const 1_i32; // scope 0 at $DIR/struct.rs:+1:17: +1:21
26+
StorageLive(_2); // scope 1 at $DIR/struct.rs:+2:9: +2:10
27+
StorageLive(_3); // scope 1 at $DIR/struct.rs:+2:13: +2:16
28+
- _3 = (_1.0: i32); // scope 1 at $DIR/struct.rs:+2:13: +2:16
29+
- _2 = Add(move _3, const 2_i32); // scope 1 at $DIR/struct.rs:+2:13: +2:20
30+
+ _3 = const 1_i32; // scope 1 at $DIR/struct.rs:+2:13: +2:16
31+
+ _2 = const 3_i32; // scope 1 at $DIR/struct.rs:+2:13: +2:20
32+
StorageDead(_3); // scope 1 at $DIR/struct.rs:+2:19: +2:20
33+
(_1.0: i32) = const 3_i32; // scope 2 at $DIR/struct.rs:+3:5: +3:12
34+
StorageLive(_4); // scope 2 at $DIR/struct.rs:+4:9: +4:10
35+
StorageLive(_5); // scope 2 at $DIR/struct.rs:+4:13: +4:14
36+
- _5 = _2; // scope 2 at $DIR/struct.rs:+4:13: +4:14
37+
+ _5 = const 3_i32; // scope 2 at $DIR/struct.rs:+4:13: +4:14
38+
StorageLive(_6); // scope 2 at $DIR/struct.rs:+4:17: +4:20
39+
- _6 = (_1.0: i32); // scope 2 at $DIR/struct.rs:+4:17: +4:20
40+
- _4 = Add(move _5, move _6); // scope 2 at $DIR/struct.rs:+4:13: +4:20
41+
+ _6 = const 3_i32; // scope 2 at $DIR/struct.rs:+4:17: +4:20
42+
+ _4 = const 6_i32; // scope 2 at $DIR/struct.rs:+4:13: +4:20
43+
StorageDead(_6); // scope 2 at $DIR/struct.rs:+4:19: +4:20
44+
StorageDead(_5); // scope 2 at $DIR/struct.rs:+4:19: +4:20
45+
_0 = const (); // scope 0 at $DIR/struct.rs:+0:11: +5:2
46+
StorageDead(_4); // scope 2 at $DIR/struct.rs:+5:1: +5:2
47+
StorageDead(_2); // scope 1 at $DIR/struct.rs:+5:1: +5:2
48+
StorageDead(_1); // scope 0 at $DIR/struct.rs:+5:1: +5:2
49+
return; // scope 0 at $DIR/struct.rs:+5:2: +5:2
50+
}
51+
}
52+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// unit-test: DataflowConstProp
2+
3+
struct S(i32);
4+
5+
// EMIT_MIR struct.main.DataflowConstProp.diff
6+
fn main() {
7+
let mut s = S(1);
8+
let a = s.0 + 2;
9+
s.0 = 3;
10+
let b = a + s.0;
11+
}

0 commit comments

Comments
 (0)