about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2023-12-12 13:49:19 +1100
committerZalathar <Zalathar@users.noreply.github.com>2023-12-16 11:10:11 +1100
commitc57f28bbf75572e10c6165c33a2aef922315fdb4 (patch)
tree094c8efbb0617de8212d6a160091e4b246b67353
parentdfa6441354181ad829baca363b619d0aeb295798 (diff)
downloadrust-c57f28bbf75572e10c6165c33a2aef922315fdb4.tar.gz
rust-c57f28bbf75572e10c6165c33a2aef922315fdb4.zip
coverage: Avoid creating `func_coverage` for marker statements
Coverage marker statements should have no effect on codegen, but in some cases
they could have the side-effect of creating a `func_coverage` entry for their
enclosing function. That can lead to an ICE for functions that don't actually
have any coverage spans.
-rw-r--r--compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs14
1 files changed, 11 insertions, 3 deletions
diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
index 8386f067baf..0befbb5a39b 100644
--- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
+++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
@@ -85,6 +85,14 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
 
         let bx = self;
 
+        match coverage.kind {
+            // Marker statements have no effect during codegen,
+            // so return early and don't create `func_coverage`.
+            CoverageKind::SpanMarker => return,
+            // Match exhaustively to ensure that newly-added kinds are classified correctly.
+            CoverageKind::CounterIncrement { .. } | CoverageKind::ExpressionUsed { .. } => {}
+        }
+
         let Some(function_coverage_info) =
             bx.tcx.instance_mir(instance.def).function_coverage_info.as_deref()
         else {
@@ -100,9 +108,9 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
 
         let Coverage { kind } = coverage;
         match *kind {
-            // Span markers are only meaningful during MIR instrumentation,
-            // and have no effect during codegen.
-            CoverageKind::SpanMarker => {}
+            CoverageKind::SpanMarker => unreachable!(
+                "unexpected marker statement {kind:?} should have caused an early return"
+            ),
             CoverageKind::CounterIncrement { id } => {
                 func_coverage.mark_counter_id_seen(id);
                 // We need to explicitly drop the `RefMut` before calling into `instrprof_increment`,