Skip to content

Commit 52d3782

Browse files
committed
simplify-locals: Remove unused set-discriminant statements
Update affected ui & incremental tests to use a user declared variable bindings instead of temporaries. The former are preserved because of debuginfo, the latter are not.
1 parent e1e48ae commit 52d3782

8 files changed

+20
-27
lines changed

compiler/rustc_mir/src/transform/simplify.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ impl Visitor<'_> for UsedLocals {
427427
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
428428
match statement.kind {
429429
StatementKind::LlvmInlineAsm(..)
430-
| StatementKind::SetDiscriminant { .. } // FIXME: Try to remove those as well.
431430
| StatementKind::Retag(..)
432431
| StatementKind::Coverage(..)
433432
| StatementKind::FakeRead(..)
@@ -467,6 +466,10 @@ impl Visitor<'_> for UsedLocals {
467466
}
468467
self.visit_rvalue(rvalue, location);
469468
}
469+
470+
StatementKind::SetDiscriminant { ref place, variant_index: _ } => {
471+
self.visit_lhs(place, location);
472+
}
470473
}
471474
}
472475

@@ -481,10 +484,7 @@ impl Visitor<'_> for UsedLocals {
481484
}
482485

483486
/// Removes unused definitions. Updates the used locals to reflect the changes made.
484-
fn remove_unused_definitions<'a, 'tcx>(
485-
used_locals: &'a mut UsedLocals,
486-
body: &mut Body<'tcx>,
487-
) {
487+
fn remove_unused_definitions<'a, 'tcx>(used_locals: &'a mut UsedLocals, body: &mut Body<'tcx>) {
488488
// The use counts are updated as we remove the statements. A local might become unused
489489
// during the retain operation, leading to a temporary inconsistency (storage statements or
490490
// definitions referencing the local might remain). For correctness it is crucial that this
@@ -502,6 +502,10 @@ fn remove_unused_definitions<'a, 'tcx>(
502502
used_locals.is_used(*local)
503503
}
504504
StatementKind::Assign(box (place, _)) => used_locals.is_used(place.local),
505+
506+
StatementKind::SetDiscriminant { ref place, .. } => {
507+
used_locals.is_used(place.local)
508+
}
505509
_ => true,
506510
};
507511

src/test/incremental/hashes/enum_constructors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// build-pass (FIXME(62277): could be check-pass?)
99
// revisions: cfail1 cfail2 cfail3
10-
// compile-flags: -Z query-dep-graph -Zincremental-ignore-spans
10+
// compile-flags: -Z query-dep-graph -Zincremental-ignore-spans -Zmir-opt-level=0
1111

1212
#![allow(warnings)]
1313
#![feature(rustc_attrs)]

src/test/mir-opt/simplify_locals.d1.SimplifyLocals.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
fn d1() -> () {
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:20:9: 20:9
6-
let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
6+
- let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
77
scope 1 {
88
}
99

1010
bb0: {
11-
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
12-
discriminant(_1) = 0; // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
13-
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:22:17: 22:18
11+
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
12+
- discriminant(_1) = 0; // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
13+
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:22:17: 22:18
1414
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:20:9: 23:2
1515
return; // scope 0 at $DIR/simplify-locals.rs:23:2: 23:2
1616
}

src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
fn d2() -> () {
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:26:9: 26:9
6-
let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
6+
- let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
77
- let mut _2: (i32, E); // in scope 0 at $DIR/simplify-locals.rs:28:5: 28:17
88
- let mut _3: E; // in scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
9-
+ let mut _2: E; // in scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
109

1110
bb0: {
12-
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
13-
discriminant(_1) = 1; // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
11+
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
12+
- discriminant(_1) = 1; // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
1413
- StorageLive(_2); // scope 0 at $DIR/simplify-locals.rs:28:5: 28:17
1514
- StorageLive(_3); // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
1615
- discriminant(_3) = 0; // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
@@ -30,10 +29,7 @@
3029
- // mir::Constant
3130
- // + span: $DIR/simplify-locals.rs:28:5: 28:26
3231
- // + literal: Const { ty: E, val: Value(Scalar(0x01)) }
33-
+ StorageLive(_2); // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
34-
+ discriminant(_2) = 0; // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
35-
+ StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:28:15: 28:16
36-
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:28:25: 28:26
32+
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:28:25: 28:26
3733
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:28:26: 28:27
3834
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:26:9: 29:2
3935
return; // scope 0 at $DIR/simplify-locals.rs:29:2: 29:2

src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.32bit.mir

-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
fn change_loop_body() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/while_let_loops.rs:5:27: 5:27
55
let mut _1: i32; // in scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
6-
let mut _2: std::option::Option<u32>; // in scope 0 at $DIR/while_let_loops.rs:7:28: 7:32
76
scope 1 {
87
debug _x => _1; // in scope 1 at $DIR/while_let_loops.rs:6:9: 6:15
98
}
109

1110
bb0: {
1211
StorageLive(_1); // scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
1312
_1 = const 0_i32; // scope 0 at $DIR/while_let_loops.rs:6:18: 6:19
14-
StorageLive(_2); // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
15-
discriminant(_2) = 0; // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
1613
_0 = const (); // scope 1 at $DIR/while_let_loops.rs:7:5: 10:6
17-
StorageDead(_2); // scope 1 at $DIR/while_let_loops.rs:10:5: 10:6
1814
StorageDead(_1); // scope 0 at $DIR/while_let_loops.rs:11:1: 11:2
1915
return; // scope 0 at $DIR/while_let_loops.rs:11:2: 11:2
2016
}

src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.64bit.mir

-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
fn change_loop_body() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/while_let_loops.rs:5:27: 5:27
55
let mut _1: i32; // in scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
6-
let mut _2: std::option::Option<u32>; // in scope 0 at $DIR/while_let_loops.rs:7:28: 7:32
76
scope 1 {
87
debug _x => _1; // in scope 1 at $DIR/while_let_loops.rs:6:9: 6:15
98
}
109

1110
bb0: {
1211
StorageLive(_1); // scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
1312
_1 = const 0_i32; // scope 0 at $DIR/while_let_loops.rs:6:18: 6:19
14-
StorageLive(_2); // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
15-
discriminant(_2) = 0; // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
1613
_0 = const (); // scope 1 at $DIR/while_let_loops.rs:7:5: 10:6
17-
StorageDead(_2); // scope 1 at $DIR/while_let_loops.rs:10:5: 10:6
1814
StorageDead(_1); // scope 0 at $DIR/while_let_loops.rs:11:1: 11:2
1915
return; // scope 0 at $DIR/while_let_loops.rs:11:2: 11:2
2016
}

src/test/ui/lint/issue-69485-var-size-diffs-too-large.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// build-fail
22
// only-x86_64
3+
// compile-flags: -Zmir-opt-level=0
34

45
fn main() {
56
Bug::V([0; !0]); //~ ERROR is too big for the current

src/test/ui/lint/issue-69485-var-size-diffs-too-large.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the type `[u8; 18446744073709551615]` is too big for the current architecture
2-
--> $DIR/issue-69485-var-size-diffs-too-large.rs:5:12
2+
--> $DIR/issue-69485-var-size-diffs-too-large.rs:6:12
33
|
44
LL | Bug::V([0; !0]);
55
| ^^^^^^^

0 commit comments

Comments
 (0)