Skip to content

Commit f0fd0da

Browse files
committed
coverage: Make query coverage_ids_info return an Option
This reflects the fact that we can't compute meaningful info for a function that wasn't instrumented and therefore doesn't have `function_coverage_info`.
1 parent b351f6b commit f0fd0da

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
5151
is_used: bool,
5252
) -> Option<CovfunRecord<'tcx>> {
5353
let fn_cov_info = tcx.instance_mir(instance.def).function_coverage_info.as_deref()?;
54-
let ids_info = tcx.coverage_ids_info(instance.def);
54+
let ids_info = tcx.coverage_ids_info(instance.def).as_ref()?;
5555

5656
let expressions = prepare_expressions(fn_cov_info, ids_info, is_used);
5757

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_codegen_ssa::traits::{
88
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
99
use rustc_middle::mir::coverage::CoverageKind;
1010
use rustc_middle::ty::Instance;
11-
use rustc_middle::ty::layout::HasTyCtxt;
1211
use tracing::{debug, instrument};
1312

1413
use crate::builder::Builder;
@@ -147,6 +146,10 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
147146
debug!("function has a coverage statement but no coverage info");
148147
return;
149148
};
149+
let Some(ids_info) = bx.tcx.coverage_ids_info(instance.def) else {
150+
debug!("function has a coverage statement but no IDs info");
151+
return;
152+
};
150153

151154
// Mark the instance as used in this CGU, for coverage purposes.
152155
// This includes functions that were not partitioned into this CGU,
@@ -162,8 +165,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
162165
// be smaller than the number originally inserted by the instrumentor,
163166
// if some high-numbered counters were removed by MIR optimizations.
164167
// If so, LLVM's profiler runtime will use fewer physical counters.
165-
let num_counters =
166-
bx.tcx().coverage_ids_info(instance.def).num_counters_after_mir_opts();
168+
let num_counters = ids_info.num_counters_after_mir_opts();
167169
assert!(
168170
num_counters as usize <= function_coverage_info.num_counters,
169171
"num_counters disagreement: query says {num_counters} but function info only has {}",

compiler/rustc_middle/src/query/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ rustc_queries! {
618618
/// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass
619619
/// (for compiler option `-Cinstrument-coverage`), after MIR optimizations
620620
/// have had a chance to potentially remove some of them.
621-
query coverage_ids_info(key: ty::InstanceKind<'tcx>) -> &'tcx mir::coverage::CoverageIdsInfo {
621+
///
622+
/// Returns `None` for functions that were not instrumented.
623+
query coverage_ids_info(key: ty::InstanceKind<'tcx>) -> &'tcx Option<mir::coverage::CoverageIdsInfo> {
622624
desc { |tcx| "retrieving coverage IDs info from MIR for `{}`", tcx.def_path_str(key.def_id()) }
623625
arena_cache
624626
}

compiler/rustc_mir_transform/src/coverage/query.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,9 @@ fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
8787
fn coverage_ids_info<'tcx>(
8888
tcx: TyCtxt<'tcx>,
8989
instance_def: ty::InstanceKind<'tcx>,
90-
) -> CoverageIdsInfo {
90+
) -> Option<CoverageIdsInfo> {
9191
let mir_body = tcx.instance_mir(instance_def);
92-
93-
let Some(fn_cov_info) = mir_body.function_coverage_info.as_deref() else {
94-
return CoverageIdsInfo {
95-
counters_seen: DenseBitSet::new_empty(0),
96-
zero_expressions: DenseBitSet::new_empty(0),
97-
};
98-
};
92+
let fn_cov_info = mir_body.function_coverage_info.as_deref()?;
9993

10094
let mut counters_seen = DenseBitSet::new_empty(fn_cov_info.num_counters);
10195
let mut expressions_seen = DenseBitSet::new_filled(fn_cov_info.expressions.len());
@@ -129,7 +123,7 @@ fn coverage_ids_info<'tcx>(
129123
let zero_expressions =
130124
identify_zero_expressions(fn_cov_info, &counters_seen, &expressions_seen);
131125

132-
CoverageIdsInfo { counters_seen, zero_expressions }
126+
Some(CoverageIdsInfo { counters_seen, zero_expressions })
133127
}
134128

135129
fn all_coverage_in_mir_body<'a, 'tcx>(

0 commit comments

Comments
 (0)