-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
coverage: Use SpanMarker
to improve coverage spans for if !
expressions
#118198
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d2d742c
coverage: Add a dedicated test for coverage of `if !`
Zalathar 44b47aa
coverage: Add `CoverageKind::SpanMarker` for including extra spans in…
Zalathar 9816635
coverage: Use `SpanMarker` to mark `continue` expressions.
Zalathar d90fd02
coverage: Use `SpanMarker` to mark the full condition of `if !`
Zalathar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,6 @@ use rustc_index::{IndexSlice, IndexVec}; | |
use rustc_middle::middle::region; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::thir::{Expr, LintLevel}; | ||
use rustc_middle::ty::Ty; | ||
use rustc_session::lint::Level; | ||
use rustc_span::{Span, DUMMY_SP}; | ||
|
||
|
@@ -660,14 +659,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
(None, Some(_)) => { | ||
panic!("`return`, `become` and `break` with value and must have a destination") | ||
} | ||
(None, None) if self.tcx.sess.instrument_coverage() => { | ||
// Unlike `break` and `return`, which push an `Assign` statement to MIR, from which | ||
// a Coverage code region can be generated, `continue` needs no `Assign`; but | ||
// without one, the `InstrumentCoverage` MIR pass cannot generate a code region for | ||
// `continue`. Coverage will be missing unless we add a dummy `Assign` to MIR. | ||
self.add_dummy_assignment(span, block, source_info); | ||
(None, None) => { | ||
if self.tcx.sess.instrument_coverage() { | ||
// Normally we wouldn't build any MIR in this case, but that makes it | ||
// harder for coverage instrumentation to extract a relevant span for | ||
// `continue` expressions. So here we inject a dummy statement with the | ||
// desired span. | ||
self.cfg.push_coverage_span_marker(block, source_info); | ||
} | ||
} | ||
(None, None) => {} | ||
} | ||
|
||
let region_scope = self.scopes.breakable_scopes[break_index].region_scope; | ||
|
@@ -723,14 +723,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
self.cfg.terminate(block, source_info, TerminatorKind::UnwindResume); | ||
} | ||
|
||
// Add a dummy `Assign` statement to the CFG, with the span for the source code's `continue` | ||
// statement. | ||
fn add_dummy_assignment(&mut self, span: Span, block: BasicBlock, source_info: SourceInfo) { | ||
let local_decl = LocalDecl::new(Ty::new_unit(self.tcx), span); | ||
let temp_place = Place::from(self.local_decls.push(local_decl)); | ||
self.cfg.push_assign_unit(block, source_info, temp_place, self.tcx); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 🎉 |
||
fn leave_top_scope(&mut self, block: BasicBlock) -> BasicBlock { | ||
// If we are emitting a `drop` statement, we need to have the cached | ||
// diverge cleanup pads ready in case that drop panics. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Function name: if_not::if_not | ||
Raw bytes (86): 0x[01, 01, 10, 01, 05, 05, 02, 3f, 09, 05, 02, 09, 3a, 3f, 09, 05, 02, 37, 0d, 09, 3a, 3f, 09, 05, 02, 0d, 32, 37, 0d, 09, 3a, 3f, 09, 05, 02, 0a, 01, 04, 01, 03, 0d, 02, 04, 05, 02, 06, 05, 02, 06, 00, 07, 3f, 03, 09, 01, 0d, 3a, 02, 05, 02, 06, 09, 02, 06, 00, 07, 37, 03, 09, 01, 0d, 32, 02, 05, 02, 06, 0d, 02, 0c, 02, 06, 2f, 03, 01, 00, 02] | ||
Number of files: 1 | ||
- file 0 => global file 1 | ||
Number of expressions: 16 | ||
- expression 0 operands: lhs = Counter(0), rhs = Counter(1) | ||
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub) | ||
- expression 2 operands: lhs = Expression(15, Add), rhs = Counter(2) | ||
- expression 3 operands: lhs = Counter(1), rhs = Expression(0, Sub) | ||
- expression 4 operands: lhs = Counter(2), rhs = Expression(14, Sub) | ||
- expression 5 operands: lhs = Expression(15, Add), rhs = Counter(2) | ||
- expression 6 operands: lhs = Counter(1), rhs = Expression(0, Sub) | ||
- expression 7 operands: lhs = Expression(13, Add), rhs = Counter(3) | ||
- expression 8 operands: lhs = Counter(2), rhs = Expression(14, Sub) | ||
- expression 9 operands: lhs = Expression(15, Add), rhs = Counter(2) | ||
- expression 10 operands: lhs = Counter(1), rhs = Expression(0, Sub) | ||
- expression 11 operands: lhs = Counter(3), rhs = Expression(12, Sub) | ||
- expression 12 operands: lhs = Expression(13, Add), rhs = Counter(3) | ||
- expression 13 operands: lhs = Counter(2), rhs = Expression(14, Sub) | ||
- expression 14 operands: lhs = Expression(15, Add), rhs = Counter(2) | ||
- expression 15 operands: lhs = Counter(1), rhs = Expression(0, Sub) | ||
Number of file 0 mappings: 10 | ||
- Code(Counter(0)) at (prev + 4, 1) to (start + 3, 13) | ||
- Code(Expression(0, Sub)) at (prev + 4, 5) to (start + 2, 6) | ||
= (c0 - c1) | ||
- Code(Counter(1)) at (prev + 2, 6) to (start + 0, 7) | ||
- Code(Expression(15, Add)) at (prev + 3, 9) to (start + 1, 13) | ||
= (c1 + (c0 - c1)) | ||
- Code(Expression(14, Sub)) at (prev + 2, 5) to (start + 2, 6) | ||
= ((c1 + (c0 - c1)) - c2) | ||
- Code(Counter(2)) at (prev + 2, 6) to (start + 0, 7) | ||
- Code(Expression(13, Add)) at (prev + 3, 9) to (start + 1, 13) | ||
= (c2 + ((c1 + (c0 - c1)) - c2)) | ||
- Code(Expression(12, Sub)) at (prev + 2, 5) to (start + 2, 6) | ||
= ((c2 + ((c1 + (c0 - c1)) - c2)) - c3) | ||
- Code(Counter(3)) at (prev + 2, 12) to (start + 2, 6) | ||
- Code(Expression(11, Add)) at (prev + 3, 1) to (start + 0, 2) | ||
= (c3 + ((c2 + ((c1 + (c0 - c1)) - c2)) - c3)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
LL| |#![feature(coverage_attribute)] | ||
LL| |// edition: 2021 | ||
LL| | | ||
LL| 12|fn if_not(cond: bool) { | ||
LL| 12| if | ||
LL| 12| ! | ||
LL| 12| cond | ||
LL| 4| { | ||
LL| 4| println!("cond was false"); | ||
LL| 8| } | ||
LL| | | ||
LL| | if | ||
LL| 12| ! | ||
LL| 12| cond | ||
LL| 4| { | ||
LL| 4| println!("cond was false"); | ||
LL| 8| } | ||
LL| | | ||
LL| | if | ||
LL| 12| ! | ||
LL| 12| cond | ||
LL| 4| { | ||
LL| 4| println!("cond was false"); | ||
LL| 8| } else { | ||
LL| 8| println!("cond was true"); | ||
LL| 8| } | ||
LL| 12|} | ||
LL| | | ||
LL| |#[coverage(off)] | ||
LL| |fn main() { | ||
LL| | for _ in 0..8 { | ||
LL| | if_not(std::hint::black_box(true)); | ||
LL| | } | ||
LL| | for _ in 0..4 { | ||
LL| | if_not(std::hint::black_box(false)); | ||
LL| | } | ||
LL| |} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![feature(coverage_attribute)] | ||
// edition: 2021 | ||
|
||
fn if_not(cond: bool) { | ||
if | ||
! | ||
cond | ||
{ | ||
println!("cond was false"); | ||
} | ||
|
||
if | ||
! | ||
cond | ||
{ | ||
println!("cond was false"); | ||
} | ||
|
||
if | ||
! | ||
cond | ||
{ | ||
println!("cond was false"); | ||
} else { | ||
println!("cond was true"); | ||
} | ||
} | ||
|
||
#[coverage(off)] | ||
fn main() { | ||
for _ in 0..8 { | ||
if_not(std::hint::black_box(true)); | ||
} | ||
for _ in 0..4 { | ||
if_not(std::hint::black_box(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
^0 | ||
LL| | | ||
LL| | if | ||
LL| | ! | ||
LL| 1| ! | ||
LL| 1| is_true | ||
LL| 0| { | ||
LL| 0| a = 2 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this test happen inside
push_coverage_span_marker
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
push_coverage_span_marker
is a method onCFG
, which doesn't have its own copy oftcx
orsess
.So the only way to perform the test would be to also pass tcx/sess as an additional argument, which seems backwards to me. Better to perform the test in the outer code that already has access to the necessary information.
Having the test outside also makes it easier to see from outside that this code has no effect on the CFG in non-coverage builds.