Skip to content

Commit fdf56d2

Browse files
Rollup merge of #156659 - Zalathar:syntax-context, r=oli-obk
coverage: Build the expansion tree around `SyntaxContext` The expansion tree was previously built around `ExpnId`, which was subtly incorrect in cases where multiple distinct syntax contexts share the same `outer_expn` ID. This can occur in the presence of nested macro expansions. This change turns out to be fairly straightforward, because in all cases we were obtaining `ExpnId` from `span.ctxt().outer_expn()` anyway, so we can just remove the call to `.outer_expn()` and use the context instead. As a result of this change, `context-mismatch-issue-147339.rs` no longer triggers a context mismatch in `extract_refined_covspans`, so this PR is a more principled fix for #147339.
2 parents 3c533ce + 03de21e commit fdf56d2

3 files changed

Lines changed: 51 additions & 48 deletions

File tree

compiler/rustc_mir_transform/src/coverage/expansion.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use itertools::Itertools;
22
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
33
use rustc_middle::mir;
44
use rustc_middle::mir::coverage::{BasicCoverageBlock, BranchSpan};
5-
use rustc_span::{ExpnId, ExpnKind, Span};
5+
use rustc_span::{ExpnKind, Span, SyntaxContext};
66

77
use crate::coverage::from_mir;
88
use crate::coverage::graph::CoverageGraph;
@@ -17,31 +17,31 @@ pub(crate) struct SpanWithBcb {
1717

1818
#[derive(Debug)]
1919
pub(crate) struct ExpnTree {
20-
nodes: FxIndexMap<ExpnId, ExpnNode>,
20+
nodes: FxIndexMap<SyntaxContext, ExpnNode>,
2121
}
2222

2323
impl ExpnTree {
24-
pub(crate) fn get(&self, expn_id: ExpnId) -> Option<&ExpnNode> {
25-
self.nodes.get(&expn_id)
24+
pub(crate) fn get(&self, context: SyntaxContext) -> Option<&ExpnNode> {
25+
self.nodes.get(&context)
2626
}
2727
}
2828

2929
#[derive(Debug)]
3030
pub(crate) struct ExpnNode {
31-
/// Storing the expansion ID in its own node is not strictly necessary,
31+
/// Storing the syntax context in its own node is not strictly necessary,
3232
/// but is helpful for debugging and might be useful later.
3333
#[expect(dead_code)]
34-
pub(crate) expn_id: ExpnId,
34+
pub(crate) context: SyntaxContext,
3535
/// Index of this node in a depth-first traversal from the root.
3636
pub(crate) dfs_rank: usize,
3737

3838
// Useful info extracted from `ExpnData`.
3939
pub(crate) expn_kind: ExpnKind,
4040
/// Non-dummy `ExpnData::call_site` span.
4141
pub(crate) call_site: Option<Span>,
42-
/// Expansion ID of `call_site`, if present.
42+
/// Syntax context of `call_site`, if present.
4343
/// This links an expansion node to its parent in the tree.
44-
pub(crate) call_site_expn_id: Option<ExpnId>,
44+
pub(crate) call_site_context: Option<SyntaxContext>,
4545

4646
/// Holds the function signature span, if it belongs to this expansion.
4747
/// Used by special-case code in span refinement.
@@ -53,7 +53,7 @@ pub(crate) struct ExpnNode {
5353
/// Spans (and their associated BCBs) belonging to this expansion.
5454
pub(crate) spans: Vec<SpanWithBcb>,
5555
/// Expansions whose call-site is in this expansion.
56-
pub(crate) child_expn_ids: FxIndexSet<ExpnId>,
56+
pub(crate) child_contexts: FxIndexSet<SyntaxContext>,
5757
/// The "minimum" and "maximum" BCBs (in dominator order) of ordinary spans
5858
/// belonging to this tree node and all of its descendants. Used when
5959
/// creating a single code mapping representing an entire child expansion.
@@ -68,25 +68,25 @@ pub(crate) struct ExpnNode {
6868
}
6969

7070
impl ExpnNode {
71-
fn new(expn_id: ExpnId) -> Self {
72-
let expn_data = expn_id.expn_data();
71+
fn for_context(context: SyntaxContext) -> Self {
72+
let expn_data = context.outer_expn_data();
7373

7474
let call_site = Some(expn_data.call_site).filter(|sp| !sp.is_dummy());
75-
let call_site_expn_id = try { call_site?.ctxt().outer_expn() };
75+
let call_site_context = try { call_site?.ctxt() };
7676

7777
Self {
78-
expn_id,
78+
context,
7979
dfs_rank: usize::MAX,
8080

8181
expn_kind: expn_data.kind,
8282
call_site,
83-
call_site_expn_id,
83+
call_site_context,
8484

8585
fn_sig_span: None,
8686
body_span: None,
8787

8888
spans: vec![],
89-
child_expn_ids: FxIndexSet::default(),
89+
child_contexts: FxIndexSet::default(),
9090
minmax_bcbs: None,
9191

9292
branch_spans: vec![],
@@ -106,32 +106,32 @@ pub(crate) fn build_expn_tree(
106106
let raw_spans = from_mir::extract_raw_spans_from_mir(mir_body, graph);
107107

108108
let mut nodes = FxIndexMap::default();
109-
let new_node = |&expn_id: &ExpnId| ExpnNode::new(expn_id);
109+
let new_node = |&context: &SyntaxContext| ExpnNode::for_context(context);
110110

111111
for from_mir::RawSpanFromMir { raw_span, bcb } in raw_spans {
112112
let span_with_bcb = SpanWithBcb { span: raw_span, bcb };
113113

114114
// Create a node for this span's enclosing expansion, and add the span to it.
115-
let expn_id = span_with_bcb.span.ctxt().outer_expn();
116-
let node = nodes.entry(expn_id).or_insert_with_key(new_node);
115+
let context = span_with_bcb.span.ctxt();
116+
let node = nodes.entry(context).or_insert_with_key(new_node);
117117
node.spans.push(span_with_bcb);
118118

119119
// Now walk up the expansion call-site chain, creating nodes and registering children.
120-
let mut prev = expn_id;
121-
let mut curr_expn_id = node.call_site_expn_id;
122-
while let Some(expn_id) = curr_expn_id {
123-
let entry = nodes.entry(expn_id);
120+
let mut prev = context;
121+
let mut curr_context = node.call_site_context;
122+
while let Some(context) = curr_context {
123+
let entry = nodes.entry(context);
124124
let node_existed = matches!(entry, IndexEntry::Occupied(_));
125125

126126
let node = entry.or_insert_with_key(new_node);
127-
node.child_expn_ids.insert(prev);
127+
node.child_contexts.insert(prev);
128128

129129
if node_existed {
130130
break;
131131
}
132132

133-
prev = expn_id;
134-
curr_expn_id = node.call_site_expn_id;
133+
prev = context;
134+
curr_context = node.call_site_context;
135135
}
136136
}
137137

@@ -152,30 +152,30 @@ pub(crate) fn build_expn_tree(
152152
// If we have a span for the function signature, associate it with the
153153
// corresponding expansion tree node.
154154
if let Some(fn_sig_span) = hir_info.fn_sig_span
155-
&& let Some(node) = nodes.get_mut(&fn_sig_span.ctxt().outer_expn())
155+
&& let Some(node) = nodes.get_mut(&fn_sig_span.ctxt())
156156
{
157157
node.fn_sig_span = Some(fn_sig_span);
158158
}
159159

160160
// Also associate the body span with its expansion tree node.
161161
let body_span = hir_info.body_span;
162-
if let Some(node) = nodes.get_mut(&body_span.ctxt().outer_expn()) {
162+
if let Some(node) = nodes.get_mut(&body_span.ctxt()) {
163163
node.body_span = Some(body_span);
164164
}
165165

166166
// Associate each hole span (extracted from HIR) with its corresponding
167167
// expansion tree node.
168168
for &hole_span in &hir_info.hole_spans {
169-
let expn_id = hole_span.ctxt().outer_expn();
170-
let Some(node) = nodes.get_mut(&expn_id) else { continue };
169+
let context = hole_span.ctxt();
170+
let Some(node) = nodes.get_mut(&context) else { continue };
171171
node.hole_spans.push(hole_span);
172172
}
173173

174174
// Associate each branch span (recorded during MIR building) with its
175175
// corresponding expansion tree node.
176176
if let Some(coverage_info_hi) = mir_body.coverage_info_hi.as_deref() {
177177
for branch_span in &coverage_info_hi.branch_spans {
178-
if let Some(node) = nodes.get_mut(&branch_span.span.ctxt().outer_expn()) {
178+
if let Some(node) = nodes.get_mut(&branch_span.span.ctxt()) {
179179
node.branch_spans.push(BranchSpan::clone(branch_span));
180180
}
181181
}
@@ -188,17 +188,19 @@ pub(crate) fn build_expn_tree(
188188
///
189189
/// This allows subsequent operations to iterate over all nodes, while assuming
190190
/// that every node occurs before all of its descendants.
191-
fn sort_nodes_depth_first(nodes: &mut FxIndexMap<ExpnId, ExpnNode>) -> Result<(), MappingsError> {
192-
let mut dfs_stack = vec![ExpnId::root()];
191+
fn sort_nodes_depth_first(
192+
nodes: &mut FxIndexMap<SyntaxContext, ExpnNode>,
193+
) -> Result<(), MappingsError> {
194+
let mut dfs_stack = vec![SyntaxContext::root()];
193195
let mut next_dfs_rank = 0usize;
194-
while let Some(expn_id) = dfs_stack.pop() {
195-
if let Some(node) = nodes.get_mut(&expn_id) {
196+
while let Some(context) = dfs_stack.pop() {
197+
if let Some(node) = nodes.get_mut(&context) {
196198
node.dfs_rank = next_dfs_rank;
197199
next_dfs_rank += 1;
198-
dfs_stack.extend(node.child_expn_ids.iter().rev().copied());
200+
dfs_stack.extend(node.child_contexts.iter().rev().copied());
199201
}
200202
}
201-
nodes.sort_by_key(|_expn_id, node| node.dfs_rank);
203+
nodes.sort_by_key(|_context, node| node.dfs_rank);
202204

203205
// Verify that the depth-first search visited each node exactly once.
204206
for (i, &ExpnNode { dfs_rank, .. }) in nodes.values().enumerate() {
@@ -222,12 +224,12 @@ pub(crate) struct MinMaxBcbs {
222224
/// and the min/max of its immediate children.
223225
fn minmax_bcbs_for_expn_tree_node(
224226
graph: &CoverageGraph,
225-
nodes: &FxIndexMap<ExpnId, ExpnNode>,
227+
nodes: &FxIndexMap<SyntaxContext, ExpnNode>,
226228
node: &ExpnNode,
227229
) -> Option<MinMaxBcbs> {
228230
let immediate_span_bcbs = node.spans.iter().map(|sp: &SpanWithBcb| sp.bcb);
229231
let child_minmax_bcbs = node
230-
.child_expn_ids
232+
.child_contexts
231233
.iter()
232234
.flat_map(|id| nodes.get(id))
233235
.flat_map(|child| child.minmax_bcbs)

compiler/rustc_mir_transform/src/coverage/mappings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn extract_branch_mappings(
8080

8181
// For now, ignore any branch span that was introduced by
8282
// expansion. This makes things like assert macros less noisy.
83-
let Some(node) = expn_tree.get(hir_info.body_span.ctxt().outer_expn()) else { return };
83+
let Some(node) = expn_tree.get(hir_info.body_span.ctxt()) else { return };
8484
if node.expn_kind != ExpnKind::Root {
8585
return;
8686
}

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_middle::mir::coverage::{Mapping, MappingKind, START_BCB};
22
use rustc_middle::ty::TyCtxt;
33
use rustc_span::source_map::SourceMap;
4-
use rustc_span::{BytePos, DesugaringKind, ExpnId, ExpnKind, MacroKind, Span};
4+
use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, SyntaxContext};
55
use tracing::instrument;
66

77
use crate::coverage::expansion::{ExpnTree, SpanWithBcb};
@@ -28,7 +28,7 @@ pub(super) fn extract_refined_covspans<'tcx>(
2828

2929
// If there somehow isn't an expansion tree node corresponding to the
3030
// body span, return now and don't create any mappings.
31-
let Some(node) = expn_tree.get(hir_info.body_span.ctxt().outer_expn()) else { return };
31+
let Some(node) = expn_tree.get(hir_info.body_span.ctxt()) else { return };
3232

3333
let mut covspans = vec![];
3434

@@ -38,8 +38,8 @@ pub(super) fn extract_refined_covspans<'tcx>(
3838

3939
// For each expansion with its call-site in the body span, try to
4040
// distill a corresponding covspan.
41-
for &child_expn_id in &node.child_expn_ids {
42-
if let Some(covspan) = single_covspan_for_child_expn(tcx, &expn_tree, child_expn_id) {
41+
for &child_context in &node.child_contexts {
42+
if let Some(covspan) = single_covspan_for_child_context(tcx, &expn_tree, child_context) {
4343
covspans.push(covspan);
4444
}
4545
}
@@ -57,8 +57,9 @@ pub(super) fn extract_refined_covspans<'tcx>(
5757
// Each pushed covspan should have the same context as the body span.
5858
// If it somehow doesn't, discard the covspan.
5959
if !body_span.eq_ctxt(covspan_span) {
60-
// FIXME(Zalathar): Investigate how and why this is triggered
61-
// by `tests/coverage/macros/context-mismatch-issue-147339.rs`.
60+
// There is currently no known way for this to happen, but if it
61+
// does happen then dropping the offending span is better than
62+
// having tricky macro expansions trigger an ICE.
6263
return false;
6364
}
6465

@@ -122,12 +123,12 @@ pub(super) fn extract_refined_covspans<'tcx>(
122123
}
123124

124125
/// For a single child expansion, try to distill it into a single span+BCB mapping.
125-
fn single_covspan_for_child_expn(
126+
fn single_covspan_for_child_context(
126127
tcx: TyCtxt<'_>,
127128
expn_tree: &ExpnTree,
128-
expn_id: ExpnId,
129+
child_context: SyntaxContext,
129130
) -> Option<Covspan> {
130-
let node = expn_tree.get(expn_id)?;
131+
let node = expn_tree.get(child_context)?;
131132
let minmax_bcbs = node.minmax_bcbs?;
132133

133134
let bcb = match node.expn_kind {

0 commit comments

Comments
 (0)