Skip to content

Commit

Permalink
Avoid ICE in non-check builds with bad #[coverage(..)] attributes
Browse files Browse the repository at this point in the history
This code can sometimes witness malformed coverage attributes in builds that
are going to fail, so use `span_delayed_bug` to avoid an inappropriate ICE in
that case.
  • Loading branch information
Zalathar committed Oct 3, 2024
1 parent ad9c494 commit 8667a30
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/coverage/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
Some([item]) if item.has_name(sym::on) => return true,
Some(_) | None => {
// Other possibilities should have been rejected by `rustc_parse::validate_attr`.
tcx.dcx().span_bug(attr.span, "unexpected value of coverage attribute");
// Use `span_delayed_bug` to avoid an ICE in failing builds (#127880).
tcx.dcx().span_delayed_bug(attr.span, "unexpected value of coverage attribute");
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions tests/crashes/127880.rs

This file was deleted.

2 changes: 2 additions & 0 deletions tests/run-make/coverage-attr-ice/127880.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[coverage]
fn main() {}
19 changes: 19 additions & 0 deletions tests/run-make/coverage-attr-ice/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use run_make_support::rustc;

// Illegal `#[coverage(..)]` attributes should not cause an ICE in non-check
// builds with `-Cinstrument-coverage`.
// Regression test for <https://github.com/rust-lang/rust/issues/127880>.
//
// This currently can't be a UI test, because there's no way to force a
// non-check build of a UI test that would also fail a check build.

fn main() {
rustc()
.edition("2021")
.arg("-Cinstrument-coverage")
.input("127880.rs")
.run_fail()
// The build should fail cleanly, but not ICE.
.assert_exit_code(1)
.assert_stderr_contains("error: malformed `coverage` attribute input");
}

0 comments on commit 8667a30

Please sign in to comment.