Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_hir::attrs::CoverageAttrKind;
use rustc_hir::find_attr;
use rustc_hir::def::DefKind;
use rustc_hir::{self as hir, find_attr};
use rustc_index::bit_set::DenseBitSet;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::coverage::{
Expand Down Expand Up @@ -30,11 +31,23 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
// expressions from coverage spans in enclosing MIR's, like we do for closures. (That might
// be tricky if const expressions have no corresponding statements in the enclosing MIR.
// Closures are carved out by their initial `Assign` statement.)
if !tcx.def_kind(def_id).is_fn_like() {
let def_kind = tcx.def_kind(def_id);
if !def_kind.is_fn_like() {
trace!("InstrumentCoverage skipped for {def_id:?} (not an fn-like)");
return false;
}

// Comptime functions can't exist at runtime, so instrumenting them is useless.
// This also avoids an ICE when getting the symbol name for an unused-function record
// (due to <https://github.com/rust-lang/rust/pull/159777>).
// We check `def_kind` first to avoid any unexpected panics from merely asking for constness.
if matches!(def_kind, DefKind::Fn | DefKind::AssocFn)
&& matches!(tcx.constness(def_id), hir::Constness::Const { always: true })
{
trace!("InstrumentCoverage skipped for {def_id:?} (comptime)");
return false;
}

if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NAKED) {
trace!("InstrumentCoverage skipped for {def_id:?} (`#[naked]`)");
return false;
Expand Down
10 changes: 10 additions & 0 deletions tests/coverage/comptime.cov-map
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Function name: comptime::main
Raw bytes (14): 0x[01, 01, 00, 02, 01, 0b, 01, 00, 0a, 01, 00, 0c, 00, 0d]
Number of files: 1
- file 0 => $DIR/comptime.rs
Number of expressions: 0
Number of file 0 mappings: 2
- Code(Counter(0)) at (prev + 11, 1) to (start + 0, 10)
- Code(Counter(0)) at (prev + 0, 12) to (start + 0, 13)
Highest counter ID seen: c0

12 changes: 12 additions & 0 deletions tests/coverage/comptime.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LL| |#![feature(rustc_attrs)]
LL| |//@ edition: 2024
LL| |
LL| |// Check that instrumenting a crate with a comptime function doesn't ICE.
LL| |// (The function itself doesn't need to be instrumented, and probably shouldn't be.)
LL| |// Regression test for <https://github.com/rust-lang/rust/pull/161808>.
LL| |
LL| |#[rustc_comptime]
LL| |fn comptime_fn() {}
LL| |
LL| 1|fn main() {}

11 changes: 11 additions & 0 deletions tests/coverage/comptime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(rustc_attrs)]
//@ edition: 2024

// Check that instrumenting a crate with a comptime function doesn't ICE.
// (The function itself doesn't need to be instrumented, and probably shouldn't be.)
// Regression test for <https://github.com/rust-lang/rust/pull/161808>.

#[rustc_comptime]
fn comptime_fn() {}

fn main() {}
Loading