Skip to content

Commit 51ddc74

Browse files
committed
Auto merge of #116304 - Zalathar:unreachable, r=cjgillot
coverage: Regression test for functions with unreachable bodies This is a regression test for the coverage issue that was addressed temporarily by #116166, and is tracked by #116171. --- If we instrument a function for coverage, but all of its counter-increment statements are removed by MIR optimizations, LLVM will think it isn't instrumented and it will disappear from coverage maps and coverage reports. Most MIR opts won't cause this because they tend not to remove statements from bb0, but `UnreachablePropagation` can do so if it sees that bb0 ends with `TerminatorKind::Unreachable`. Currently we have worked around this by turning off `UnreachablePropagation` when coverage instrumentation is enabled, which is why this test is able to pass. --- `@rustbot` label +A-code-coverage
2 parents 0e1dd17 + 3de0824 commit 51ddc74

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Function name: unreachable::UNREACHABLE_CLOSURE::{closure#0}
2+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 0f, 27, 00, 49]
3+
Number of files: 1
4+
- file 0 => global file 1
5+
Number of expressions: 0
6+
Number of file 0 mappings: 1
7+
- Code(Counter(0)) at (prev + 15, 39) to (start + 0, 73)
8+
9+
Function name: unreachable::unreachable_function
10+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 11, 01, 02, 02]
11+
Number of files: 1
12+
- file 0 => global file 1
13+
Number of expressions: 0
14+
Number of file 0 mappings: 1
15+
- Code(Counter(0)) at (prev + 17, 1) to (start + 2, 2)
16+
17+
Function name: unreachable::unreachable_intrinsic
18+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 16, 01, 02, 02]
19+
Number of files: 1
20+
- file 0 => global file 1
21+
Number of expressions: 0
22+
Number of file 0 mappings: 1
23+
- Code(Counter(0)) at (prev + 22, 1) to (start + 2, 2)
24+

tests/coverage-map/unreachable.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(coverage_attribute)]
3+
// compile-flags: --edition=2021
4+
5+
// <https://github.com/rust-lang/rust/issues/116171>
6+
// If we instrument a function for coverage, but all of its counter-increment
7+
// statements are removed by MIR optimizations, LLVM will think it isn't
8+
// instrumented and it will disappear from coverage maps and coverage reports.
9+
// Most MIR opts won't cause this because they tend not to remove statements
10+
// from bb0, but `UnreachablePropagation` can do so if it sees that bb0 ends
11+
// with `TerminatorKind::Unreachable`.
12+
13+
use std::hint::{black_box, unreachable_unchecked};
14+
15+
static UNREACHABLE_CLOSURE: fn() = || unsafe { unreachable_unchecked() };
16+
17+
fn unreachable_function() {
18+
unsafe { unreachable_unchecked() }
19+
}
20+
21+
// Use an intrinsic to more reliably trigger unreachable-propagation.
22+
fn unreachable_intrinsic() {
23+
unsafe { std::intrinsics::unreachable() }
24+
}
25+
26+
#[coverage(off)]
27+
fn main() {
28+
if black_box(false) {
29+
UNREACHABLE_CLOSURE();
30+
}
31+
if black_box(false) {
32+
unreachable_function();
33+
}
34+
if black_box(false) {
35+
unreachable_intrinsic();
36+
}
37+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
LL| |#![feature(core_intrinsics)]
2+
LL| |#![feature(coverage_attribute)]
3+
LL| |// compile-flags: --edition=2021
4+
LL| |
5+
LL| |// <https://github.com/rust-lang/rust/issues/116171>
6+
LL| |// If we instrument a function for coverage, but all of its counter-increment
7+
LL| |// statements are removed by MIR optimizations, LLVM will think it isn't
8+
LL| |// instrumented and it will disappear from coverage maps and coverage reports.
9+
LL| |// Most MIR opts won't cause this because they tend not to remove statements
10+
LL| |// from bb0, but `UnreachablePropagation` can do so if it sees that bb0 ends
11+
LL| |// with `TerminatorKind::Unreachable`.
12+
LL| |
13+
LL| |use std::hint::{black_box, unreachable_unchecked};
14+
LL| |
15+
LL| 0|static UNREACHABLE_CLOSURE: fn() = || unsafe { unreachable_unchecked() };
16+
LL| |
17+
LL| 0|fn unreachable_function() {
18+
LL| 0| unsafe { unreachable_unchecked() }
19+
LL| 0|}
20+
LL| |
21+
LL| |// Use an intrinsic to more reliably trigger unreachable-propagation.
22+
LL| 0|fn unreachable_intrinsic() {
23+
LL| 0| unsafe { std::intrinsics::unreachable() }
24+
LL| 0|}
25+
LL| |
26+
LL| |#[coverage(off)]
27+
LL| |fn main() {
28+
LL| | if black_box(false) {
29+
LL| | UNREACHABLE_CLOSURE();
30+
LL| | }
31+
LL| | if black_box(false) {
32+
LL| | unreachable_function();
33+
LL| | }
34+
LL| | if black_box(false) {
35+
LL| | unreachable_intrinsic();
36+
LL| | }
37+
LL| |}
38+

tests/run-coverage/unreachable.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(coverage_attribute)]
3+
// compile-flags: --edition=2021
4+
5+
// <https://github.com/rust-lang/rust/issues/116171>
6+
// If we instrument a function for coverage, but all of its counter-increment
7+
// statements are removed by MIR optimizations, LLVM will think it isn't
8+
// instrumented and it will disappear from coverage maps and coverage reports.
9+
// Most MIR opts won't cause this because they tend not to remove statements
10+
// from bb0, but `UnreachablePropagation` can do so if it sees that bb0 ends
11+
// with `TerminatorKind::Unreachable`.
12+
13+
use std::hint::{black_box, unreachable_unchecked};
14+
15+
static UNREACHABLE_CLOSURE: fn() = || unsafe { unreachable_unchecked() };
16+
17+
fn unreachable_function() {
18+
unsafe { unreachable_unchecked() }
19+
}
20+
21+
// Use an intrinsic to more reliably trigger unreachable-propagation.
22+
fn unreachable_intrinsic() {
23+
unsafe { std::intrinsics::unreachable() }
24+
}
25+
26+
#[coverage(off)]
27+
fn main() {
28+
if black_box(false) {
29+
UNREACHABLE_CLOSURE();
30+
}
31+
if black_box(false) {
32+
unreachable_function();
33+
}
34+
if black_box(false) {
35+
unreachable_intrinsic();
36+
}
37+
}

0 commit comments

Comments
 (0)