Skip to content

Commit 62da38d

Browse files
committed
Auto merge of #72287 - Aaron1011:feature/min-token-collect, r=petrochenkov
Store tokens inside `ast::Expr` This is a smaller version of #70091. We now store captured tokens inside `ast::Expr`, which allows us to avoid some reparsing in `nt_to_tokenstream`. To try to mitigate the performance impact, we only collect tokens when we've seen an outer attribute. This makes progress towards solving #43081. There are still many things left to do: * Collect tokens for other AST items. * Come up with a way to handle inner attributes (we need to be collecting tokens by the time we encounter them) * Avoid re-parsing when a `#[cfg]` attr is used. However, this is enough to fix spans for a simple example, which I've included as a test case.
2 parents 46e85b4 + 14382c6 commit 62da38d

File tree

16 files changed

+97
-18
lines changed

16 files changed

+97
-18
lines changed

src/librustc_ast/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,12 @@ pub struct Expr {
10061006
pub kind: ExprKind,
10071007
pub span: Span,
10081008
pub attrs: AttrVec,
1009+
pub tokens: Option<TokenStream>,
10091010
}
10101011

10111012
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
10121013
#[cfg(target_arch = "x86_64")]
1013-
rustc_data_structures::static_assert_size!(Expr, 96);
1014+
rustc_data_structures::static_assert_size!(Expr, 104);
10141015

10151016
impl Expr {
10161017
/// Returns `true` if this expression would be valid somewhere that expects a value;

src/librustc_ast/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,10 @@ pub fn noop_visit_anon_const<T: MutVisitor>(AnonConst { id, value }: &mut AnonCo
10951095
vis.visit_expr(value);
10961096
}
10971097

1098-
pub fn noop_visit_expr<T: MutVisitor>(Expr { kind, id, span, attrs }: &mut Expr, vis: &mut T) {
1098+
pub fn noop_visit_expr<T: MutVisitor>(
1099+
Expr { kind, id, span, attrs, tokens: _ }: &mut Expr,
1100+
vis: &mut T,
1101+
) {
10991102
match kind {
11001103
ExprKind::Box(expr) => vis.visit_expr(expr),
11011104
ExprKind::Array(exprs) => visit_exprs(exprs, vis),

src/librustc_ast_lowering/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11261126
kind: ExprKind::Path(qself.clone(), path.clone()),
11271127
span: ty.span,
11281128
attrs: AttrVec::new(),
1129+
tokens: None,
11291130
};
11301131

11311132
let ct = self.with_new_scopes(|this| hir::AnonConst {

src/librustc_builtin_macros/asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
519519
kind: ast::ExprKind::InlineAsm(inline_asm),
520520
span: sp,
521521
attrs: ast::AttrVec::new(),
522+
tokens: None,
522523
})
523524
}
524525

src/librustc_builtin_macros/concat_idents.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub fn expand_concat_idents<'cx>(
5252
kind: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
5353
span: self.ident.span,
5454
attrs: ast::AttrVec::new(),
55+
tokens: None,
5556
}))
5657
}
5758

src/librustc_builtin_macros/llvm_asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub fn expand_llvm_asm<'cx>(
6161
kind: ast::ExprKind::LlvmInlineAsm(P(inline_asm)),
6262
span: cx.with_def_site_ctxt(sp),
6363
attrs: ast::AttrVec::new(),
64+
tokens: None,
6465
}))
6566
}
6667

src/librustc_expand/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ impl DummyResult {
594594
kind: if is_error { ast::ExprKind::Err } else { ast::ExprKind::Tup(Vec::new()) },
595595
span: sp,
596596
attrs: ast::AttrVec::new(),
597+
tokens: None,
597598
})
598599
}
599600

src/librustc_expand/build.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ impl<'a> ExtCtxt<'a> {
7070
pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
7171
ast::AnonConst {
7272
id: ast::DUMMY_NODE_ID,
73-
value: P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new() }),
73+
value: P(ast::Expr {
74+
id: ast::DUMMY_NODE_ID,
75+
kind,
76+
span,
77+
attrs: AttrVec::new(),
78+
tokens: None,
79+
}),
7480
}
7581
}
7682

@@ -205,7 +211,7 @@ impl<'a> ExtCtxt<'a> {
205211
}
206212

207213
pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
208-
P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new() })
214+
P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new(), tokens: None })
209215
}
210216

211217
pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {

src/librustc_expand/placeholders.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub fn placeholder(
3434
span,
3535
attrs: ast::AttrVec::new(),
3636
kind: ast::ExprKind::MacCall(mac_placeholder()),
37+
tokens: None,
3738
})
3839
};
3940
let ty = || P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span });

src/librustc_interface/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
713713
kind: ast::ExprKind::Block(P(b), None),
714714
span: rustc_span::DUMMY_SP,
715715
attrs: AttrVec::new(),
716+
tokens: None,
716717
});
717718

718719
ast::Stmt {
@@ -728,6 +729,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
728729
id: self.resolver.next_node_id(),
729730
span: rustc_span::DUMMY_SP,
730731
attrs: AttrVec::new(),
732+
tokens: None,
731733
});
732734

733735
let loop_stmt = ast::Stmt {

0 commit comments

Comments
 (0)