Skip to content

Commit 4854fa7

Browse files
authored
Rollup merge of #129686 - Zalathar:source-region, r=compiler-errors
coverage: Rename `CodeRegion` to `SourceRegion` LLVM uses the word "code" to refer to a particular kind of coverage mapping. This unrelated usage of the word is confusing, and makes it harder to introduce types whose names correspond to the LLVM classification of coverage kinds. No functional changes.
2 parents 27d7fb0 + 46e1b5b commit 4854fa7

File tree

7 files changed

+36
-43
lines changed

7 files changed

+36
-43
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::mir::coverage::{
2-
CodeRegion, ConditionInfo, CounterId, CovTerm, DecisionInfo, ExpressionId, MappingKind,
2+
ConditionInfo, CounterId, CovTerm, DecisionInfo, ExpressionId, MappingKind, SourceRegion,
33
};
44

55
/// Must match the layout of `LLVMRustCounterKind`.
@@ -236,9 +236,10 @@ impl CounterMappingRegion {
236236
pub(crate) fn from_mapping(
237237
mapping_kind: &MappingKind,
238238
local_file_id: u32,
239-
code_region: &CodeRegion,
239+
source_region: &SourceRegion,
240240
) -> Self {
241-
let &CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = code_region;
241+
let &SourceRegion { file_name: _, start_line, start_col, end_line, end_col } =
242+
source_region;
242243
match *mapping_kind {
243244
MappingKind::Code(term) => Self::code_region(
244245
Counter::from_term(term),

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_data_structures::captures::Captures;
22
use rustc_data_structures::fx::FxIndexSet;
33
use rustc_index::bit_set::BitSet;
44
use rustc_middle::mir::coverage::{
5-
CodeRegion, CounterId, CovTerm, Expression, ExpressionId, FunctionCoverageInfo, Mapping,
6-
MappingKind, Op,
5+
CounterId, CovTerm, Expression, ExpressionId, FunctionCoverageInfo, Mapping, MappingKind, Op,
6+
SourceRegion,
77
};
88
use rustc_middle::ty::Instance;
99
use rustc_span::Symbol;
@@ -201,7 +201,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
201201

202202
/// Returns an iterator over all filenames used by this function's mappings.
203203
pub(crate) fn all_file_names(&self) -> impl Iterator<Item = Symbol> + Captures<'_> {
204-
self.function_coverage_info.mappings.iter().map(|mapping| mapping.code_region.file_name)
204+
self.function_coverage_info.mappings.iter().map(|mapping| mapping.source_region.file_name)
205205
}
206206

207207
/// Convert this function's coverage expression data into a form that can be
@@ -230,12 +230,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
230230
/// that will be used by `mapgen` when preparing for FFI.
231231
pub(crate) fn counter_regions(
232232
&self,
233-
) -> impl Iterator<Item = (MappingKind, &CodeRegion)> + ExactSizeIterator {
233+
) -> impl Iterator<Item = (MappingKind, &SourceRegion)> + ExactSizeIterator {
234234
self.function_coverage_info.mappings.iter().map(move |mapping| {
235-
let Mapping { kind, code_region } = mapping;
235+
let Mapping { kind, source_region } = mapping;
236236
let kind =
237237
kind.map_terms(|term| if self.is_zero_term(term) { CovTerm::Zero } else { term });
238-
(kind, code_region)
238+
(kind, source_region)
239239
})
240240
}
241241

compiler/rustc_middle/src/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ macro_rules! arena_types {
3535
)>,
3636
[] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,
3737
[] resolutions: rustc_middle::ty::ResolverGlobalCtxt,
38-
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
3938
[] const_allocs: rustc_middle::mir::interpret::Allocation,
4039
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,
4140
// Required for the incremental on-disk cache

compiler/rustc_middle/src/mir/coverage.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ impl Debug for CoverageKind {
163163

164164
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, Eq, PartialOrd, Ord)]
165165
#[derive(TypeFoldable, TypeVisitable)]
166-
pub struct CodeRegion {
166+
pub struct SourceRegion {
167167
pub file_name: Symbol,
168168
pub start_line: u32,
169169
pub start_col: u32,
170170
pub end_line: u32,
171171
pub end_col: u32,
172172
}
173173

174-
impl Debug for CodeRegion {
174+
impl Debug for SourceRegion {
175175
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
176176
write!(
177177
fmt,
@@ -242,7 +242,7 @@ impl MappingKind {
242242
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
243243
pub struct Mapping {
244244
pub kind: MappingKind,
245-
pub code_region: CodeRegion,
245+
pub source_region: SourceRegion,
246246
}
247247

248248
/// Stores per-function coverage information attached to a `mir::Body`,

compiler/rustc_middle/src/mir/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ fn write_function_coverage_info(
547547
for (id, expression) in expressions.iter_enumerated() {
548548
writeln!(w, "{INDENT}coverage {id:?} => {expression:?};")?;
549549
}
550-
for coverage::Mapping { kind, code_region } in mappings {
551-
writeln!(w, "{INDENT}coverage {kind:?} => {code_region:?};")?;
550+
for coverage::Mapping { kind, source_region } in mappings {
551+
writeln!(w, "{INDENT}coverage {kind:?} => {source_region:?};")?;
552552
}
553553
writeln!(w)?;
554554

compiler/rustc_middle/src/ty/codec.rs

-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ impl_decodable_via_ref! {
462462
&'tcx traits::ImplSource<'tcx, ()>,
463463
&'tcx mir::Body<'tcx>,
464464
&'tcx mir::BorrowCheckResult<'tcx>,
465-
&'tcx mir::coverage::CodeRegion,
466465
&'tcx ty::List<ty::BoundVariableKind>,
467466
&'tcx ty::ListWithCachedTypeInfo<ty::Clause<'tcx>>,
468467
&'tcx ty::List<FieldIdx>,

compiler/rustc_mir_transform/src/coverage/mod.rs

+21-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::intravisit::{walk_expr, Visitor};
1313
use rustc_middle::hir::map::Map;
1414
use rustc_middle::hir::nested_filter;
1515
use rustc_middle::mir::coverage::{
16-
CodeRegion, CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind,
16+
CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, SourceRegion,
1717
};
1818
use rustc_middle::mir::{
1919
self, BasicBlock, BasicBlockData, SourceInfo, Statement, StatementKind, Terminator,
@@ -159,7 +159,7 @@ fn create_mappings<'tcx>(
159159
.expect("all BCBs with spans were given counters")
160160
.as_term()
161161
};
162-
let region_for_span = |span: Span| make_code_region(source_map, file_name, span, body_span);
162+
let region_for_span = |span: Span| make_source_region(source_map, file_name, span, body_span);
163163

164164
// Fully destructure the mappings struct to make sure we don't miss any kinds.
165165
let ExtractedMappings {
@@ -175,9 +175,9 @@ fn create_mappings<'tcx>(
175175
mappings.extend(code_mappings.iter().filter_map(
176176
// Ordinary code mappings are the simplest kind.
177177
|&mappings::CodeMapping { span, bcb }| {
178-
let code_region = region_for_span(span)?;
178+
let source_region = region_for_span(span)?;
179179
let kind = MappingKind::Code(term_for_bcb(bcb));
180-
Some(Mapping { kind, code_region })
180+
Some(Mapping { kind, source_region })
181181
},
182182
));
183183

@@ -186,29 +186,29 @@ fn create_mappings<'tcx>(
186186
let true_term = term_for_bcb(true_bcb);
187187
let false_term = term_for_bcb(false_bcb);
188188
let kind = MappingKind::Branch { true_term, false_term };
189-
let code_region = region_for_span(span)?;
190-
Some(Mapping { kind, code_region })
189+
let source_region = region_for_span(span)?;
190+
Some(Mapping { kind, source_region })
191191
},
192192
));
193193

194194
mappings.extend(mcdc_branches.iter().filter_map(
195195
|&mappings::MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth: _ }| {
196-
let code_region = region_for_span(span)?;
196+
let source_region = region_for_span(span)?;
197197
let true_term = term_for_bcb(true_bcb);
198198
let false_term = term_for_bcb(false_bcb);
199199
let kind = match condition_info {
200200
Some(mcdc_params) => MappingKind::MCDCBranch { true_term, false_term, mcdc_params },
201201
None => MappingKind::Branch { true_term, false_term },
202202
};
203-
Some(Mapping { kind, code_region })
203+
Some(Mapping { kind, source_region })
204204
},
205205
));
206206

207207
mappings.extend(mcdc_decisions.iter().filter_map(
208208
|&mappings::MCDCDecision { span, bitmap_idx, num_conditions, .. }| {
209-
let code_region = region_for_span(span)?;
209+
let source_region = region_for_span(span)?;
210210
let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, num_conditions });
211-
Some(Mapping { kind, code_region })
211+
Some(Mapping { kind, source_region })
212212
},
213213
));
214214

@@ -362,19 +362,13 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
362362
/// but it's hard to rule out entirely (especially in the presence of complex macros
363363
/// or other expansions), and if it does happen then skipping a span or function is
364364
/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid.
365-
fn make_code_region(
365+
#[instrument(level = "debug", skip(source_map))]
366+
fn make_source_region(
366367
source_map: &SourceMap,
367368
file_name: Symbol,
368369
span: Span,
369370
body_span: Span,
370-
) -> Option<CodeRegion> {
371-
debug!(
372-
"Called make_code_region(file_name={}, span={}, body_span={})",
373-
file_name,
374-
source_map.span_to_diagnostic_string(span),
375-
source_map.span_to_diagnostic_string(body_span)
376-
);
377-
371+
) -> Option<SourceRegion> {
378372
let lo = span.lo();
379373
let hi = span.hi();
380374

@@ -424,7 +418,7 @@ fn make_code_region(
424418
start_line = source_map.doctest_offset_line(&file.name, start_line);
425419
end_line = source_map.doctest_offset_line(&file.name, end_line);
426420

427-
check_code_region(CodeRegion {
421+
check_source_region(SourceRegion {
428422
file_name,
429423
start_line: start_line as u32,
430424
start_col: start_col as u32,
@@ -433,12 +427,12 @@ fn make_code_region(
433427
})
434428
}
435429

436-
/// If `llvm-cov` sees a code region that is improperly ordered (end < start),
430+
/// If `llvm-cov` sees a source region that is improperly ordered (end < start),
437431
/// it will immediately exit with a fatal error. To prevent that from happening,
438432
/// discard regions that are improperly ordered, or might be interpreted in a
439433
/// way that makes them improperly ordered.
440-
fn check_code_region(code_region: CodeRegion) -> Option<CodeRegion> {
441-
let CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = code_region;
434+
fn check_source_region(source_region: SourceRegion) -> Option<SourceRegion> {
435+
let SourceRegion { file_name: _, start_line, start_col, end_line, end_col } = source_region;
442436

443437
// Line/column coordinates are supposed to be 1-based. If we ever emit
444438
// coordinates of 0, `llvm-cov` might misinterpret them.
@@ -451,17 +445,17 @@ fn check_code_region(code_region: CodeRegion) -> Option<CodeRegion> {
451445
let is_ordered = (start_line, start_col) <= (end_line, end_col);
452446

453447
if all_nonzero && end_col_has_high_bit_unset && is_ordered {
454-
Some(code_region)
448+
Some(source_region)
455449
} else {
456450
debug!(
457-
?code_region,
451+
?source_region,
458452
?all_nonzero,
459453
?end_col_has_high_bit_unset,
460454
?is_ordered,
461-
"Skipping code region that would be misinterpreted or rejected by LLVM"
455+
"Skipping source region that would be misinterpreted or rejected by LLVM"
462456
);
463457
// If this happens in a debug build, ICE to make it easier to notice.
464-
debug_assert!(false, "Improper code region: {code_region:?}");
458+
debug_assert!(false, "Improper source region: {source_region:?}");
465459
None
466460
}
467461
}

0 commit comments

Comments
 (0)