Skip to content

Commit 327a748

Browse files
committed
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 9c3064e + 2789588 commit 327a748

File tree

20 files changed

+469
-498
lines changed

20 files changed

+469
-498
lines changed

compiler/rustc_ast/src/visit.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ macro_rules! common_visitor_and_walkers {
527527
}
528528
}
529529

530-
fn visit_defaultness<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, defaultness: &$($lt)? $($mut)? Defaultness) -> V::Result {
530+
pub fn visit_defaultness<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, defaultness: &$($lt)? $($mut)? Defaultness) -> V::Result {
531531
match defaultness {
532532
Defaultness::Default(span) => visit_span(vis, span),
533533
Defaultness::Final => {
@@ -807,7 +807,7 @@ macro_rules! common_visitor_and_walkers {
807807
visit_foreign_items(vis, items)
808808
}
809809

810-
fn walk_define_opaques<$($lt,)? V: $Visitor$(<$lt>)?>(
810+
pub fn walk_define_opaques<$($lt,)? V: $Visitor$(<$lt>)?>(
811811
visitor: &mut V,
812812
define_opaque: &$($lt)? $($mut)? Option<ThinVec<(NodeId, Path)>>,
813813
) -> V::Result {
@@ -1225,6 +1225,27 @@ macro_rules! common_visitor_and_walkers {
12251225
V::Result::output()
12261226
}
12271227

1228+
pub fn walk_stmt<$($lt,)? V: $Visitor$(<$lt>)?>(
1229+
vis: &mut V,
1230+
statement: &$($lt)? $($mut)? Stmt,
1231+
) -> V::Result {
1232+
let Stmt { id, kind, span } = statement;
1233+
try_visit!(visit_id(vis, id));
1234+
match kind {
1235+
StmtKind::Let(local) => try_visit!(vis.visit_local(local)),
1236+
StmtKind::Item(item) => try_visit!(vis.visit_item(item)),
1237+
StmtKind::Expr(expr) | StmtKind::Semi(expr) => try_visit!(vis.visit_expr(expr)),
1238+
StmtKind::Empty => {}
1239+
StmtKind::MacCall(mac) => {
1240+
let MacCallStmt { mac, attrs, style: _, tokens: _ } = &$($mut)? **mac;
1241+
walk_list!(vis, visit_attribute, attrs);
1242+
try_visit!(vis.visit_mac_call(mac));
1243+
}
1244+
}
1245+
try_visit!(visit_span(vis, span));
1246+
V::Result::output()
1247+
}
1248+
12281249
pub fn walk_path<$($lt,)? V: $Visitor$(<$lt>)?>(
12291250
vis: &mut V,
12301251
path: &$($lt)? $($mut)? Path,
@@ -1944,20 +1965,3 @@ fn visit_nested_use_tree<'a, V: Visitor<'a>>(
19441965
) -> V::Result {
19451966
vis.visit_nested_use_tree(nested_tree, nested_id)
19461967
}
1947-
1948-
pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V::Result {
1949-
let Stmt { id, kind, span: _ } = statement;
1950-
try_visit!(visit_id(visitor, id));
1951-
match kind {
1952-
StmtKind::Let(local) => try_visit!(visitor.visit_local(local)),
1953-
StmtKind::Item(item) => try_visit!(visitor.visit_item(item)),
1954-
StmtKind::Expr(expr) | StmtKind::Semi(expr) => try_visit!(visitor.visit_expr(expr)),
1955-
StmtKind::Empty => {}
1956-
StmtKind::MacCall(mac) => {
1957-
let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;
1958-
walk_list!(visitor, visit_attribute, attrs);
1959-
try_visit!(visitor.visit_mac_call(mac));
1960-
}
1961-
}
1962-
V::Result::output()
1963-
}

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
237237
hir::InlineAsmOperand::Label { block: self.lower_block(block, false) }
238238
}
239239
};
240-
(op, self.lower_span(*op_sp))
240+
(op, *op_sp)
241241
})
242242
.collect();
243243

@@ -463,7 +463,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
463463
late: true,
464464
expr: None,
465465
},
466-
self.lower_span(abi_span),
466+
abi_span,
467467
));
468468
clobbered.insert(clobber);
469469
}
@@ -497,12 +497,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
497497
let operands = self.arena.alloc_from_iter(operands);
498498
let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
499499
let template_strs = self.arena.alloc_from_iter(
500-
asm.template_strs
501-
.iter()
502-
.map(|(sym, snippet, span)| (*sym, *snippet, self.lower_span(*span))),
500+
asm.template_strs.iter().map(|(sym, snippet, span)| (*sym, *snippet, *span)),
503501
);
504-
let line_spans =
505-
self.arena.alloc_from_iter(asm.line_spans.iter().map(|span| self.lower_span(*span)));
502+
let line_spans = self.arena.alloc_from_iter(asm.line_spans.iter().copied());
506503
let hir_asm = hir::InlineAsm {
507504
asm_macro: asm.asm_macro,
508505
template,

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2323
) -> hir::Block<'hir> {
2424
let (stmts, expr) = self.lower_stmts(&b.stmts);
2525
let rules = self.lower_block_check_mode(&b.rules);
26-
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
26+
hir::Block { hir_id, stmts, expr, rules, span: b.span, targeted_by_break }
2727
}
2828

2929
fn lower_stmts(
@@ -39,7 +39,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3939
let local = self.lower_local(local);
4040
self.alias_attrs(hir_id, local.hir_id);
4141
let kind = hir::StmtKind::Let(local);
42-
let span = self.lower_span(s.span);
42+
let span = s.span;
4343
stmts.push(hir::Stmt { hir_id, kind, span });
4444
}
4545
StmtKind::Item(it) => {
@@ -50,7 +50,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5050
_ => self.next_id(),
5151
};
5252
let kind = hir::StmtKind::Item(item_id);
53-
let span = self.lower_span(s.span);
53+
let span = s.span;
5454
hir::Stmt { hir_id, kind, span }
5555
},
5656
));
@@ -63,7 +63,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6363
let hir_id = self.lower_node_id(s.id);
6464
self.alias_attrs(hir_id, e.hir_id);
6565
let kind = hir::StmtKind::Expr(e);
66-
let span = self.lower_span(s.span);
66+
let span = s.span;
6767
stmts.push(hir::Stmt { hir_id, kind, span });
6868
}
6969
}
@@ -72,7 +72,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7272
let hir_id = self.lower_node_id(s.id);
7373
self.alias_attrs(hir_id, e.hir_id);
7474
let kind = hir::StmtKind::Semi(e);
75-
let span = self.lower_span(s.span);
75+
let span = s.span;
7676
stmts.push(hir::Stmt { hir_id, kind, span });
7777
}
7878
StmtKind::Empty => {}
@@ -107,7 +107,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
107107
} else {
108108
None
109109
};
110-
let span = self.lower_span(l.span);
110+
let span = l.span;
111111
let source = hir::LocalSource::Normal;
112112
self.lower_attrs(hir_id, &l.attrs, l.span);
113113
self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source })

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
8383
item_id: NodeId,
8484
is_in_trait_impl: bool,
8585
) -> DelegationResults<'hir> {
86-
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
86+
let span = delegation.path.segments.last().unwrap().ident.span;
8787
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
8888
match sig_id {
8989
Ok(sig_id) => {
@@ -92,9 +92,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
9292
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
9393
let sig = self.lower_delegation_sig(sig_id, decl, span);
9494
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
95-
let ident = self.lower_ident(delegation.ident);
9695
let generics = self.lower_delegation_generics(span);
97-
DelegationResults { body_id, sig, ident, generics }
96+
DelegationResults { body_id, sig, ident: delegation.ident, generics }
9897
}
9998
Err(err) => self.generate_delegation_error(err, span),
10099
}

0 commit comments

Comments
 (0)