Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 04122fb

Browse files
committedJul 2, 2024
Auto merge of #127241 - cjgillot:def-collector-span, r=<try>
Mark span parent in def_collector. The current device of marking spans with a parent def-id during lowering has been frustrating me for quite some time, as it's very easy to forget marking some spans. This PR moves such marking to the def_collector, which is responsible for creating def-ids on partially expanded AST. This is much more robust as long as visitors are exhaustive. r? ghost
2 parents 7d97c59 + 585fe45 commit 04122fb

File tree

30 files changed

+644
-486
lines changed

30 files changed

+644
-486
lines changed
 

‎compiler/rustc_ast/src/ast.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub enum GenericParamKind {
351351
Const {
352352
ty: P<Ty>,
353353
/// Span of the `const` keyword.
354-
kw_span: Span,
354+
span: Span,
355355
/// Optional default value for the const generic param
356356
default: Option<AnonConst>,
357357
},
@@ -375,10 +375,7 @@ impl GenericParam {
375375
self.ident.span
376376
}
377377
GenericParamKind::Type { default: Some(ty) } => self.ident.span.to(ty.span),
378-
GenericParamKind::Const { kw_span, default: Some(default), .. } => {
379-
kw_span.to(default.value.span)
380-
}
381-
GenericParamKind::Const { kw_span, default: None, ty } => kw_span.to(ty.span),
378+
GenericParamKind::Const { span, .. } => *span,
382379
}
383380
}
384381
}

‎compiler/rustc_ast/src/mut_visit.rs

+95-24
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ where
367367
}
368368

369369
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
370-
fn visit_attrs<T: MutVisitor>(attrs: &mut AttrVec, vis: &mut T) {
370+
pub fn visit_attrs<T: MutVisitor>(attrs: &mut AttrVec, vis: &mut T) {
371371
for attr in attrs.iter_mut() {
372372
vis.visit_attribute(attr);
373373
}
@@ -390,7 +390,7 @@ fn visit_bounds<T: MutVisitor>(bounds: &mut GenericBounds, vis: &mut T) {
390390
}
391391

392392
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
393-
fn visit_fn_sig<T: MutVisitor>(FnSig { header, decl, span }: &mut FnSig, vis: &mut T) {
393+
pub fn visit_fn_sig<T: MutVisitor>(FnSig { header, decl, span }: &mut FnSig, vis: &mut T) {
394394
vis.visit_fn_header(header);
395395
vis.visit_fn_decl(decl);
396396
vis.visit_span(span);
@@ -637,7 +637,7 @@ fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
637637
vis.visit_span(span);
638638
}
639639

640-
fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
640+
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
641641
let Attribute { kind, id: _, style: _, span } = attr;
642642
match kind {
643643
AttrKind::Normal(normal) => {
@@ -836,7 +836,7 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
836836
}
837837

838838
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
839-
fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T) {
839+
pub fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T) {
840840
match defaultness {
841841
Defaultness::Default(span) => vis.visit_span(span),
842842
Defaultness::Final => {}
@@ -871,7 +871,8 @@ fn visit_constness<T: MutVisitor>(constness: &mut Const, vis: &mut T) {
871871
fn noop_visit_closure_binder<T: MutVisitor>(binder: &mut ClosureBinder, vis: &mut T) {
872872
match binder {
873873
ClosureBinder::NotPresent => {}
874-
ClosureBinder::For { span: _, generic_params } => {
874+
ClosureBinder::For { span, generic_params } => {
875+
vis.visit_span(span);
875876
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
876877
}
877878
}
@@ -904,7 +905,10 @@ fn noop_visit_fn_ret_ty<T: MutVisitor>(fn_ret_ty: &mut FnRetTy, vis: &mut T) {
904905

905906
fn noop_visit_param_bound<T: MutVisitor>(pb: &mut GenericBound, vis: &mut T) {
906907
match pb {
907-
GenericBound::Trait(ty, _modifier) => vis.visit_poly_trait_ref(ty),
908+
GenericBound::Trait(ty, modifier) => {
909+
vis.visit_poly_trait_ref(ty);
910+
visit_trait_bound_modifier(modifier, vis);
911+
}
908912
GenericBound::Outlives(lifetime) => noop_visit_lifetime(lifetime, vis),
909913
GenericBound::Use(args, span) => {
910914
for arg in args {
@@ -915,6 +919,22 @@ fn noop_visit_param_bound<T: MutVisitor>(pb: &mut GenericBound, vis: &mut T) {
915919
}
916920
}
917921

922+
fn visit_trait_bound_modifier<T: MutVisitor>(tbm: &mut TraitBoundModifiers, vis: &mut T) {
923+
let TraitBoundModifiers { constness, asyncness, polarity } = tbm;
924+
match constness {
925+
BoundConstness::Never => {}
926+
BoundConstness::Always(span) | BoundConstness::Maybe(span) => vis.visit_span(span),
927+
}
928+
match asyncness {
929+
BoundAsyncness::Normal => {}
930+
BoundAsyncness::Async(span) => vis.visit_span(span),
931+
}
932+
match polarity {
933+
BoundPolarity::Positive => {}
934+
BoundPolarity::Negative(span) | BoundPolarity::Maybe(span) => vis.visit_span(span),
935+
}
936+
}
937+
918938
fn noop_visit_precise_capturing_arg<T: MutVisitor>(arg: &mut PreciseCapturingArg, vis: &mut T) {
919939
match arg {
920940
PreciseCapturingArg::Lifetime(lt) => {
@@ -941,8 +961,9 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
941961
GenericParamKind::Type { default } => {
942962
visit_opt(default, |default| vis.visit_ty(default));
943963
}
944-
GenericParamKind::Const { ty, kw_span: _, default } => {
964+
GenericParamKind::Const { ty, span, default } => {
945965
vis.visit_ty(ty);
966+
vis.visit_span(span);
946967
visit_opt(default, |default| vis.visit_anon_const(default));
947968
}
948969
}
@@ -1368,21 +1389,24 @@ pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
13681389
vis.visit_span(span);
13691390
}
13701391

1371-
fn noop_visit_anon_const<T: MutVisitor>(AnonConst { id, value }: &mut AnonConst, vis: &mut T) {
1392+
pub fn noop_visit_anon_const<T: MutVisitor>(AnonConst { id, value }: &mut AnonConst, vis: &mut T) {
13721393
vis.visit_id(id);
13731394
vis.visit_expr(value);
13741395
}
13751396

13761397
fn noop_visit_inline_asm<T: MutVisitor>(asm: &mut InlineAsm, vis: &mut T) {
1377-
// FIXME: Visit spans inside all this currently ignored stuff.
1378-
let InlineAsm {
1379-
template: _,
1380-
template_strs: _,
1381-
operands,
1382-
clobber_abis: _,
1383-
options: _,
1384-
line_spans: _,
1385-
} = asm;
1398+
let InlineAsm { template, template_strs, operands, clobber_abis, options: _, line_spans } = asm;
1399+
for piece in template.iter_mut() {
1400+
match piece {
1401+
InlineAsmTemplatePiece::String(_str) => {}
1402+
InlineAsmTemplatePiece::Placeholder { operand_idx: _, modifier: _, span } => {
1403+
vis.visit_span(span)
1404+
}
1405+
}
1406+
}
1407+
for (_s1, _s2, span) in template_strs.iter_mut() {
1408+
vis.visit_span(span)
1409+
}
13861410
for (op, span) in operands {
13871411
match op {
13881412
InlineAsmOperand::In { expr, reg: _ }
@@ -1401,6 +1425,12 @@ fn noop_visit_inline_asm<T: MutVisitor>(asm: &mut InlineAsm, vis: &mut T) {
14011425
}
14021426
vis.visit_span(span);
14031427
}
1428+
for (_s1, span) in clobber_abis.iter_mut() {
1429+
vis.visit_span(span)
1430+
}
1431+
for span in line_spans.iter_mut() {
1432+
vis.visit_span(span)
1433+
}
14041434
}
14051435

14061436
fn noop_visit_inline_asm_sym<T: MutVisitor>(
@@ -1413,8 +1443,7 @@ fn noop_visit_inline_asm_sym<T: MutVisitor>(
14131443
}
14141444

14151445
fn noop_visit_format_args<T: MutVisitor>(fmt: &mut FormatArgs, vis: &mut T) {
1416-
// FIXME: visit the template exhaustively.
1417-
let FormatArgs { span, template: _, arguments } = fmt;
1446+
let FormatArgs { span, template, arguments } = fmt;
14181447
for FormatArgument { kind, expr } in arguments.all_args_mut() {
14191448
match kind {
14201449
FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {
@@ -1424,9 +1453,48 @@ fn noop_visit_format_args<T: MutVisitor>(fmt: &mut FormatArgs, vis: &mut T) {
14241453
}
14251454
vis.visit_expr(expr);
14261455
}
1456+
for piece in template.iter_mut() {
1457+
match piece {
1458+
FormatArgsPiece::Literal(_symbol) => {}
1459+
FormatArgsPiece::Placeholder(placeholder) => visit_format_placeholder(placeholder, vis),
1460+
}
1461+
}
14271462
vis.visit_span(span);
14281463
}
14291464

1465+
fn visit_format_placeholder<T: MutVisitor>(
1466+
FormatPlaceholder { argument, span, format_options, format_trait: _ }: &mut FormatPlaceholder,
1467+
vis: &mut T,
1468+
) {
1469+
visit_opt(span, |span| vis.visit_span(span));
1470+
let FormatArgPosition { span, index: _, kind: _ } = argument;
1471+
visit_opt(span, |span| vis.visit_span(span));
1472+
let FormatOptions {
1473+
width,
1474+
precision,
1475+
alignment: _,
1476+
fill: _,
1477+
sign: _,
1478+
alternate: _,
1479+
zero_pad: _,
1480+
debug_hex: _,
1481+
} = format_options;
1482+
match width {
1483+
None => {}
1484+
Some(FormatCount::Literal(_)) => {}
1485+
Some(FormatCount::Argument(FormatArgPosition { span, index: _, kind: _ })) => {
1486+
visit_opt(span, |span| vis.visit_span(span))
1487+
}
1488+
}
1489+
match precision {
1490+
None => {}
1491+
Some(FormatCount::Literal(_)) => {}
1492+
Some(FormatCount::Argument(FormatArgPosition { span, index: _, kind: _ })) => {
1493+
visit_opt(span, |span| vis.visit_span(span))
1494+
}
1495+
}
1496+
}
1497+
14301498
pub fn noop_visit_expr<T: MutVisitor>(
14311499
Expr { kind, id, span, attrs, tokens }: &mut Expr,
14321500
vis: &mut T,
@@ -1460,7 +1528,8 @@ pub fn noop_visit_expr<T: MutVisitor>(
14601528
visit_thin_exprs(call_args, vis);
14611529
vis.visit_span(span);
14621530
}
1463-
ExprKind::Binary(_binop, lhs, rhs) => {
1531+
ExprKind::Binary(Spanned { node: _binop, span }, lhs, rhs) => {
1532+
vis.visit_span(span);
14641533
vis.visit_expr(lhs);
14651534
vis.visit_expr(rhs);
14661535
}
@@ -1528,9 +1597,10 @@ pub fn noop_visit_expr<T: MutVisitor>(
15281597
visit_opt(label, |label| vis.visit_label(label));
15291598
vis.visit_block(blk);
15301599
}
1531-
ExprKind::Gen(_capture_by, body, _kind, decl_span) => {
1600+
ExprKind::Gen(capture_clause, body, _kind, decl_span) => {
15321601
vis.visit_block(body);
15331602
vis.visit_span(decl_span);
1603+
vis.visit_capture_by(capture_clause);
15341604
}
15351605
ExprKind::Await(expr, await_kw_span) => {
15361606
vis.visit_expr(expr);
@@ -1541,7 +1611,8 @@ pub fn noop_visit_expr<T: MutVisitor>(
15411611
vis.visit_expr(er);
15421612
vis.visit_span(span);
15431613
}
1544-
ExprKind::AssignOp(_op, el, er) => {
1614+
ExprKind::AssignOp(Spanned { node: _binop, span }, el, er) => {
1615+
vis.visit_span(span);
15451616
vis.visit_expr(el);
15461617
vis.visit_expr(er);
15471618
}
@@ -1593,7 +1664,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
15931664
fields.flat_map_in_place(|field| vis.flat_map_expr_field(field));
15941665
match rest {
15951666
StructRest::Base(expr) => vis.visit_expr(expr),
1596-
StructRest::Rest(_span) => {}
1667+
StructRest::Rest(span) => vis.visit_span(span),
15971668
StructRest::None => {}
15981669
}
15991670
}
@@ -1626,6 +1697,7 @@ pub fn noop_flat_map_stmt<T: MutVisitor>(
16261697
vis: &mut T,
16271698
) -> SmallVec<[Stmt; 1]> {
16281699
vis.visit_id(&mut id);
1700+
vis.visit_span(&mut span);
16291701
let stmts: SmallVec<_> = noop_flat_map_stmt_kind(kind, vis)
16301702
.into_iter()
16311703
.map(|kind| Stmt { id, kind, span })
@@ -1636,7 +1708,6 @@ pub fn noop_flat_map_stmt<T: MutVisitor>(
16361708
the visitor should implement custom statement visiting"
16371709
);
16381710
}
1639-
vis.visit_span(&mut span);
16401711
stmts
16411712
}
16421713

‎compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(
758758
match kind {
759759
GenericParamKind::Lifetime => (),
760760
GenericParamKind::Type { default } => visit_opt!(visitor, visit_ty, default),
761-
GenericParamKind::Const { ty, default, kw_span: _ } => {
761+
GenericParamKind::Const { ty, default, span: _ } => {
762762
try_visit!(visitor.visit_ty(ty));
763763
visit_opt!(visitor, visit_anon_const, default);
764764
}

‎compiler/rustc_ast_lowering/src/asm.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
250250
hir::InlineAsmOperand::Label { block: self.lower_block(block, false) }
251251
}
252252
};
253-
(op, self.lower_span(*op_sp))
253+
(op, *op_sp)
254254
})
255255
.collect();
256256

@@ -458,7 +458,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
458458
late: true,
459459
expr: None,
460460
},
461-
self.lower_span(abi_span),
461+
abi_span,
462462
));
463463
clobbered.insert(clobber);
464464
}
@@ -468,12 +468,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
468468
let operands = self.arena.alloc_from_iter(operands);
469469
let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
470470
let template_strs = self.arena.alloc_from_iter(
471-
asm.template_strs
472-
.iter()
473-
.map(|(sym, snippet, span)| (*sym, *snippet, self.lower_span(*span))),
471+
asm.template_strs.iter().map(|(sym, snippet, span)| (*sym, *snippet, *span)),
474472
);
475-
let line_spans =
476-
self.arena.alloc_from_iter(asm.line_spans.iter().map(|span| self.lower_span(*span)));
473+
let line_spans = self.arena.alloc_from_iter(asm.line_spans.iter().copied());
477474
let hir_asm =
478475
hir::InlineAsm { template, template_strs, operands, options: asm.options, line_spans };
479476
self.arena.alloc(hir_asm)

‎compiler/rustc_ast_lowering/src/block.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2121
let (stmts, expr) = self.lower_stmts(&b.stmts);
2222
let rules = self.lower_block_check_mode(&b.rules);
2323
let hir_id = self.lower_node_id(b.id);
24-
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
24+
hir::Block { hir_id, stmts, expr, rules, span: b.span, targeted_by_break }
2525
}
2626

2727
fn lower_stmts(
@@ -37,7 +37,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3737
let local = self.lower_local(local);
3838
self.alias_attrs(hir_id, local.hir_id);
3939
let kind = hir::StmtKind::Let(local);
40-
let span = self.lower_span(s.span);
40+
let span = s.span;
4141
stmts.push(hir::Stmt { hir_id, kind, span });
4242
}
4343
StmtKind::Item(it) => {
@@ -48,7 +48,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4848
_ => self.next_id(),
4949
};
5050
let kind = hir::StmtKind::Item(item_id);
51-
let span = self.lower_span(s.span);
51+
let span = s.span;
5252
hir::Stmt { hir_id, kind, span }
5353
},
5454
));
@@ -61,7 +61,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6161
let hir_id = self.lower_node_id(s.id);
6262
self.alias_attrs(hir_id, e.hir_id);
6363
let kind = hir::StmtKind::Expr(e);
64-
let span = self.lower_span(s.span);
64+
let span = s.span;
6565
stmts.push(hir::Stmt { hir_id, kind, span });
6666
}
6767
}
@@ -70,7 +70,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7070
let hir_id = self.lower_node_id(s.id);
7171
self.alias_attrs(hir_id, e.hir_id);
7272
let kind = hir::StmtKind::Semi(e);
73-
let span = self.lower_span(s.span);
73+
let span = s.span;
7474
stmts.push(hir::Stmt { hir_id, kind, span });
7575
}
7676
StmtKind::Empty => {}
@@ -94,7 +94,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9494
} else {
9595
None
9696
};
97-
let span = self.lower_span(l.span);
97+
let span = l.span;
9898
let source = hir::LocalSource::Normal;
9999
self.lower_attrs(hir_id, &l.attrs);
100100
self.arena.alloc(hir::LetStmt { hir_id, ty, pat, init, els, span, source })

‎compiler/rustc_ast_lowering/src/delegation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
8888
delegation: &Delegation,
8989
item_id: NodeId,
9090
) -> DelegationResults<'hir> {
91-
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
91+
let span = delegation.path.segments.last().unwrap().ident.span;
9292
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span);
9393
match sig_id {
9494
Ok(sig_id) => {

0 commit comments

Comments
 (0)
Please sign in to comment.