diff options
| author | Zalathar <Zalathar@users.noreply.github.com> | 2023-06-29 12:36:19 +1000 |
|---|---|---|
| committer | Zalathar <Zalathar@users.noreply.github.com> | 2023-08-01 11:29:55 +1000 |
| commit | 3920e07f0bd97d9815a037eaeea197266424cd56 (patch) | |
| tree | 097f5c579cbd053041efc198705d45467fde8c2b /compiler/rustc_mir_transform/src | |
| parent | f103db894fdcf94822d57cf28e30bc498c042631 (diff) | |
| download | rust-3920e07f0bd97d9815a037eaeea197266424cd56.tar.gz rust-3920e07f0bd97d9815a037eaeea197266424cd56.zip | |
Make coverage counter IDs count up from 0, not 1
Operand types are now tracked explicitly, so there is no need to reserve ID 0 for the special always-zero counter. As part of the renumbering, this change fixes an off-by-one error in the way counters were counted by the `coverageinfo` query. As a result, functions should now have exactly the number of counters they actually need, instead of always having an extra counter that is never used.
Diffstat (limited to 'compiler/rustc_mir_transform/src')
| -rw-r--r-- | compiler/rustc_mir_transform/src/coverage/counters.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/coverage/query.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/coverage/tests.rs | 4 |
3 files changed, 10 insertions, 13 deletions
diff --git a/compiler/rustc_mir_transform/src/coverage/counters.rs b/compiler/rustc_mir_transform/src/coverage/counters.rs index cd2539a5e66..97bdb878ab1 100644 --- a/compiler/rustc_mir_transform/src/coverage/counters.rs +++ b/compiler/rustc_mir_transform/src/coverage/counters.rs @@ -16,7 +16,7 @@ use rustc_middle::mir::coverage::*; /// `Coverage` statements. pub(super) struct CoverageCounters { function_source_hash: u64, - next_counter_id: u32, + next_counter_id: CounterId, next_expression_id: ExpressionId, pub debug_counters: DebugCounters, } @@ -25,7 +25,7 @@ impl CoverageCounters { pub fn new(function_source_hash: u64) -> Self { Self { function_source_hash, - next_counter_id: CounterValueReference::START.as_u32(), + next_counter_id: CounterId::START, next_expression_id: ExpressionId::START, debug_counters: DebugCounters::new(), } @@ -93,10 +93,10 @@ impl CoverageCounters { } /// Counter IDs start from one and go up. - fn next_counter(&mut self) -> CounterValueReference { + fn next_counter(&mut self) -> CounterId { let next = self.next_counter_id; - self.next_counter_id += 1; - CounterValueReference::from(next) + self.next_counter_id = next.next_id(); + next } /// Expression IDs start from 0 and go up. diff --git a/compiler/rustc_mir_transform/src/coverage/query.rs b/compiler/rustc_mir_transform/src/coverage/query.rs index c1a896dbd34..aa205655f9d 100644 --- a/compiler/rustc_mir_transform/src/coverage/query.rs +++ b/compiler/rustc_mir_transform/src/coverage/query.rs @@ -43,11 +43,9 @@ struct CoverageVisitor { } impl CoverageVisitor { - /// Updates `num_counters` to the maximum encountered zero-based counter_id plus 1. Note the - /// final computed number of counters should be the number of all `CoverageKind::Counter` - /// statements in the MIR *plus one* for the implicit `ZERO` counter. + /// Updates `num_counters` to the maximum encountered counter ID plus 1. #[inline(always)] - fn update_num_counters(&mut self, counter_id: CounterValueReference) { + fn update_num_counters(&mut self, counter_id: CounterId) { let counter_id = counter_id.as_u32(); self.info.num_counters = std::cmp::max(self.info.num_counters, counter_id + 1); } @@ -103,8 +101,7 @@ fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> let mir_body = tcx.instance_mir(instance_def); let mut coverage_visitor = CoverageVisitor { - // num_counters always has at least the `ZERO` counter. - info: CoverageInfo { num_counters: 1, num_expressions: 0 }, + info: CoverageInfo { num_counters: 0, num_expressions: 0 }, add_missing_operands: false, }; diff --git a/compiler/rustc_mir_transform/src/coverage/tests.rs b/compiler/rustc_mir_transform/src/coverage/tests.rs index 25891d3ca0f..248a192f8f5 100644 --- a/compiler/rustc_mir_transform/src/coverage/tests.rs +++ b/compiler/rustc_mir_transform/src/coverage/tests.rs @@ -683,7 +683,7 @@ fn test_make_bcb_counters() { let_bcb!(1); assert_eq!( - 1, // coincidentally, bcb1 has a `Counter` with id = 1 + 0, // bcb1 has a `Counter` with id = 0 match basic_coverage_blocks[bcb1].counter().expect("should have a counter") { CoverageKind::Counter { id, .. } => id, _ => panic!("expected a Counter"), @@ -693,7 +693,7 @@ fn test_make_bcb_counters() { let_bcb!(2); assert_eq!( - 2, // coincidentally, bcb2 has a `Counter` with id = 2 + 1, // bcb2 has a `Counter` with id = 1 match basic_coverage_blocks[bcb2].counter().expect("should have a counter") { CoverageKind::Counter { id, .. } => id, _ => panic!("expected a Counter"), |
