Skip to content

Commit d1d7fb1

Browse files
committed
Only generate dummy assign when instrumenting coverage
And make the LocalDecl internal, to avoid needing to declare storage. (For multiple `continue` stateuemtns, it must also be mutable.)
1 parent 448e52d commit d1d7fb1

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

compiler/rustc_mir_build/src/build/scope.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -618,16 +618,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
618618
}
619619
} else {
620620
assert!(value.is_none(), "`return` and `break` should have a destination");
621-
// `continue` statements generate no MIR statement with the `continue` statement's Span,
622-
// and the `InstrumentCoverage` statement will have no way to generate a coverage
623-
// code region for the `continue` statement, unless we add a dummy `Assign` here:
624-
let mut local_decl = LocalDecl::new(self.tcx.mk_unit(), span);
625-
local_decl = local_decl.immutable();
626-
let temp = self.local_decls.push(local_decl);
627-
let temp_place = Place::from(temp);
628-
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
629-
self.cfg.push_assign_unit(block, source_info, temp_place, self.tcx);
630-
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageDead(temp) });
621+
if self.tcx.sess.instrument_coverage() {
622+
// Unlike `break` and `return`, which push an `Assign` statement to MIR, from which
623+
// a Coverage code region can be generated, `continue` needs no `Assign`; but
624+
// without one, the `InstrumentCoverage` MIR pass cannot generate a code region for
625+
// `continue`. Coverage will be missing unless we add a dummy `Assign` to MIR.
626+
self.add_dummy_assignment(&span, block, source_info);
627+
}
631628
}
632629

633630
let region_scope = self.scopes.breakable_scopes[break_index].region_scope;
@@ -653,6 +650,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
653650
self.cfg.start_new_block().unit()
654651
}
655652

653+
// Add a dummy `Assign` statement to the CFG, with the span for the source code's `continue`
654+
// statement.
655+
fn add_dummy_assignment(&mut self, span: &Span, block: BasicBlock, source_info: SourceInfo) {
656+
let local_decl = LocalDecl::new(self.tcx.mk_unit(), *span).internal();
657+
let temp_place = Place::from(self.local_decls.push(local_decl));
658+
self.cfg.push_assign_unit(block, source_info, temp_place, self.tcx);
659+
}
660+
656661
crate fn exit_top_scope(
657662
&mut self,
658663
mut block: BasicBlock,

src/test/mir-opt/loop_test.main.SimplifyCfg-promote-consts.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn main() -> () {
88
let mut _4: !; // in scope 0 at $DIR/loop_test.rs:13:5: 16:6
99
let mut _5: (); // in scope 0 at $DIR/loop_test.rs:6:1: 17:2
1010
let _6: i32; // in scope 0 at $DIR/loop_test.rs:14:13: 14:14
11-
let _7: (); // in scope 0 at $DIR/loop_test.rs:15:9: 15:17
1211
scope 1 {
1312
debug x => _6; // in scope 1 at $DIR/loop_test.rs:14:13: 14:14
1413
}
@@ -43,9 +42,6 @@ fn main() -> () {
4342
StorageLive(_6); // scope 0 at $DIR/loop_test.rs:14:13: 14:14
4443
_6 = const 1_i32; // scope 0 at $DIR/loop_test.rs:14:17: 14:18
4544
FakeRead(ForLet(None), _6); // scope 0 at $DIR/loop_test.rs:14:13: 14:14
46-
StorageLive(_7); // scope 1 at $DIR/loop_test.rs:15:9: 15:17
47-
_7 = const (); // scope 1 at $DIR/loop_test.rs:15:9: 15:17
48-
StorageDead(_7); // scope 1 at $DIR/loop_test.rs:15:9: 15:17
4945
StorageDead(_6); // scope 0 at $DIR/loop_test.rs:16:5: 16:6
5046
goto -> bb3; // scope 0 at $DIR/loop_test.rs:1:1: 1:1
5147
}

0 commit comments

Comments
 (0)