From d3cba0b9b57acfc80b5f054b20aeb3f99186d267 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 7 Sep 2026 19:55:35 +1000 Subject: [PATCH 1/2] Temporarily add a crashtest for instrumenting comptime functions This test demonstrates the existing crash, and will be migrated to a successful coverage test in a subsequent commit. Co-Authored-By: Rachel Barker --- tests/crashes/comptime.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/crashes/comptime.rs diff --git a/tests/crashes/comptime.rs b/tests/crashes/comptime.rs new file mode 100644 index 0000000000000..77fec9f509222 --- /dev/null +++ b/tests/crashes/comptime.rs @@ -0,0 +1,13 @@ +#![feature(rustc_attrs)] +//@ edition: 2024 +//@ compile-flags: -Cinstrument-coverage +//@ needs-profiler-runtime + +// 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 . + +#[rustc_comptime] +fn comptime_fn() {} + +fn main() {} From 0027ee161223389c9490ffef6d07f1cc00a75385 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 6 Sep 2026 16:47:24 +1000 Subject: [PATCH 2/2] Make comptime functions ineligible for coverage Compile-time-only functions don't generate code, so instrumenting them for coverage is useless. This also avoids an ICE when trying to get the function's symbol name for an unused-function record, which can occur when instrumenting `core`. Co-Authored-By: Rachel Barker --- .../rustc_mir_transform/src/coverage/query.rs | 17 +++++++++++++++-- tests/coverage/comptime.cov-map | 10 ++++++++++ tests/coverage/comptime.coverage | 12 ++++++++++++ tests/{crashes => coverage}/comptime.rs | 2 -- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/coverage/comptime.cov-map create mode 100644 tests/coverage/comptime.coverage rename tests/{crashes => coverage}/comptime.rs (82%) diff --git a/compiler/rustc_mir_transform/src/coverage/query.rs b/compiler/rustc_mir_transform/src/coverage/query.rs index 6ffb85d7b90a8..a4d39f09b724d 100644 --- a/compiler/rustc_mir_transform/src/coverage/query.rs +++ b/compiler/rustc_mir_transform/src/coverage/query.rs @@ -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::{ @@ -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 ). + // 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; diff --git a/tests/coverage/comptime.cov-map b/tests/coverage/comptime.cov-map new file mode 100644 index 0000000000000..30f91da050f6f --- /dev/null +++ b/tests/coverage/comptime.cov-map @@ -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 + diff --git a/tests/coverage/comptime.coverage b/tests/coverage/comptime.coverage new file mode 100644 index 0000000000000..1ff44169babb2 --- /dev/null +++ b/tests/coverage/comptime.coverage @@ -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 . + LL| | + LL| |#[rustc_comptime] + LL| |fn comptime_fn() {} + LL| | + LL| 1|fn main() {} + diff --git a/tests/crashes/comptime.rs b/tests/coverage/comptime.rs similarity index 82% rename from tests/crashes/comptime.rs rename to tests/coverage/comptime.rs index 77fec9f509222..4891051b0076d 100644 --- a/tests/crashes/comptime.rs +++ b/tests/coverage/comptime.rs @@ -1,7 +1,5 @@ #![feature(rustc_attrs)] //@ edition: 2024 -//@ compile-flags: -Cinstrument-coverage -//@ needs-profiler-runtime // 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.)