Skip to content

Commit 069e612

Browse files
committed
rustc_ast: Replace AstLike::finalize_tokens with a getter tokens_mut
1 parent 51748a8 commit 069e612

File tree

5 files changed

+60
-78
lines changed

5 files changed

+60
-78
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -915,16 +915,6 @@ impl Stmt {
915915
}
916916
}
917917

918-
pub fn tokens_mut(&mut self) -> Option<&mut LazyTokenStream> {
919-
match self.kind {
920-
StmtKind::Local(ref mut local) => local.tokens.as_mut(),
921-
StmtKind::Item(ref mut item) => item.tokens.as_mut(),
922-
StmtKind::Expr(ref mut expr) | StmtKind::Semi(ref mut expr) => expr.tokens.as_mut(),
923-
StmtKind::Empty => None,
924-
StmtKind::MacCall(ref mut mac) => mac.tokens.as_mut(),
925-
}
926-
}
927-
928918
pub fn has_trailing_semicolon(&self) -> bool {
929919
match &self.kind {
930920
StmtKind::Semi(_) => true,

compiler/rustc_ast/src/ast_like.rs

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ use super::{AttrVec, Attribute, Stmt, StmtKind};
1111
pub trait AstLike: Sized {
1212
fn attrs(&self) -> &[Attribute];
1313
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>));
14-
/// Called by `Parser::collect_tokens` to store the collected
15-
/// tokens inside an AST node
16-
fn finalize_tokens(&mut self, _tokens: LazyTokenStream) {
17-
// This default impl makes this trait easier to implement
18-
// in tools like `rust-analyzer`
19-
panic!("`finalize_tokens` is not supported!")
20-
}
14+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>>;
2115
}
2216

2317
impl<T: AstLike + 'static> AstLike for P<T> {
@@ -27,8 +21,8 @@ impl<T: AstLike + 'static> AstLike for P<T> {
2721
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
2822
(**self).visit_attrs(f);
2923
}
30-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
31-
(**self).finalize_tokens(tokens)
24+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
25+
(**self).tokens_mut()
3226
}
3327
}
3428

@@ -42,12 +36,12 @@ fn visit_attrvec(attrs: &mut AttrVec, f: impl FnOnce(&mut Vec<Attribute>)) {
4236

4337
impl AstLike for StmtKind {
4438
fn attrs(&self) -> &[Attribute] {
45-
match *self {
46-
StmtKind::Local(ref local) => local.attrs(),
47-
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
48-
StmtKind::Item(ref item) => item.attrs(),
39+
match self {
40+
StmtKind::Local(local) => local.attrs(),
41+
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.attrs(),
42+
StmtKind::Item(item) => item.attrs(),
4943
StmtKind::Empty => &[],
50-
StmtKind::MacCall(ref mac) => &*mac.attrs,
44+
StmtKind::MacCall(mac) => &mac.attrs,
5145
}
5246
}
5347

@@ -60,17 +54,14 @@ impl AstLike for StmtKind {
6054
StmtKind::MacCall(mac) => visit_attrvec(&mut mac.attrs, f),
6155
}
6256
}
63-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
64-
let stmt_tokens = match self {
65-
StmtKind::Local(ref mut local) => &mut local.tokens,
66-
StmtKind::Item(ref mut item) => &mut item.tokens,
67-
StmtKind::Expr(ref mut expr) | StmtKind::Semi(ref mut expr) => &mut expr.tokens,
68-
StmtKind::Empty => return,
69-
StmtKind::MacCall(ref mut mac) => &mut mac.tokens,
70-
};
71-
if stmt_tokens.is_none() {
72-
*stmt_tokens = Some(tokens);
73-
}
57+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
58+
Some(match self {
59+
StmtKind::Local(local) => &mut local.tokens,
60+
StmtKind::Item(item) => &mut item.tokens,
61+
StmtKind::Expr(expr) | StmtKind::Semi(expr) => &mut expr.tokens,
62+
StmtKind::Empty => return None,
63+
StmtKind::MacCall(mac) => &mut mac.tokens,
64+
})
7465
}
7566
}
7667

@@ -82,8 +73,8 @@ impl AstLike for Stmt {
8273
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
8374
self.kind.visit_attrs(f);
8475
}
85-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
86-
self.kind.finalize_tokens(tokens)
76+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
77+
self.kind.tokens_mut()
8778
}
8879
}
8980

@@ -92,17 +83,13 @@ impl AstLike for Attribute {
9283
&[]
9384
}
9485
fn visit_attrs(&mut self, _f: impl FnOnce(&mut Vec<Attribute>)) {}
95-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
96-
match &mut self.kind {
97-
AttrKind::Normal(_, attr_tokens) => {
98-
if attr_tokens.is_none() {
99-
*attr_tokens = Some(tokens);
100-
}
86+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
87+
Some(match &mut self.kind {
88+
AttrKind::Normal(_, tokens) => tokens,
89+
kind @ AttrKind::DocComment(..) => {
90+
panic!("Called tokens_mut on doc comment attr {:?}", kind)
10191
}
102-
AttrKind::DocComment(..) => {
103-
panic!("Called finalize_tokens on doc comment attr {:?}", self)
104-
}
105-
}
92+
})
10693
}
10794
}
10895

@@ -115,10 +102,8 @@ impl<T: AstLike> AstLike for Option<T> {
115102
inner.visit_attrs(f);
116103
}
117104
}
118-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
119-
if let Some(inner) = self {
120-
inner.finalize_tokens(tokens);
121-
}
105+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
106+
self.as_mut().and_then(|inner| inner.tokens_mut())
122107
}
123108
}
124109

@@ -152,11 +137,8 @@ macro_rules! derive_has_tokens_and_attrs {
152137
VecOrAttrVec::visit(&mut self.attrs, f)
153138
}
154139

155-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
156-
if self.tokens.is_none() {
157-
self.tokens = Some(tokens);
158-
}
159-
140+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
141+
Some(&mut self.tokens)
160142
}
161143
}
162144
)* }
@@ -173,7 +155,9 @@ macro_rules! derive_has_attrs_no_tokens {
173155
VecOrAttrVec::visit(&mut self.attrs, f)
174156
}
175157

176-
fn finalize_tokens(&mut self, _tokens: LazyTokenStream) {}
158+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
159+
None
160+
}
177161
}
178162
)* }
179163
}
@@ -185,14 +169,10 @@ macro_rules! derive_has_tokens_no_attrs {
185169
&[]
186170
}
187171

188-
fn visit_attrs(&mut self, _f: impl FnOnce(&mut Vec<Attribute>)) {
189-
}
190-
191-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
192-
if self.tokens.is_none() {
193-
self.tokens = Some(tokens);
194-
}
172+
fn visit_attrs(&mut self, _f: impl FnOnce(&mut Vec<Attribute>)) {}
195173

174+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
175+
Some(&mut self.tokens)
196176
}
197177
}
198178
)* }

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{self as ast, token, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
1+
use rustc_ast::{self as ast, token, AstLike, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
22
use rustc_errors::{struct_span_err, Applicability};
33
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
44
use rustc_expand::config::StripUnconfigured;
@@ -59,15 +59,9 @@ impl MultiItemModifier for Expander {
5959
// Erase the tokens if cfg-stripping modified the item
6060
// This will cause us to synthesize fake tokens
6161
// when `nt_to_tokenstream` is called on this item.
62-
match &mut item {
63-
Annotatable::Item(item) => item,
64-
Annotatable::Stmt(stmt) => match &mut stmt.kind {
65-
StmtKind::Item(item) => item,
66-
_ => unreachable!(),
67-
},
68-
_ => unreachable!(),
62+
if let Some(tokens) = item.tokens_mut() {
63+
*tokens = None;
6964
}
70-
.tokens = None;
7165
}
7266
ExpandResult::Ready(vec![item])
7367
}

compiler/rustc_expand/src/base.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,22 @@ impl AstLike for Annotatable {
8181
}
8282
}
8383

84-
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
85-
panic!("Called finalize_tokens on an Annotatable: {:?}", tokens);
84+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
85+
match self {
86+
Annotatable::Item(item) => item.tokens_mut(),
87+
Annotatable::TraitItem(trait_item) => trait_item.tokens_mut(),
88+
Annotatable::ImplItem(impl_item) => impl_item.tokens_mut(),
89+
Annotatable::ForeignItem(foreign_item) => foreign_item.tokens_mut(),
90+
Annotatable::Stmt(stmt) => stmt.tokens_mut(),
91+
Annotatable::Expr(expr) => expr.tokens_mut(),
92+
Annotatable::Arm(arm) => arm.tokens_mut(),
93+
Annotatable::Field(field) => field.tokens_mut(),
94+
Annotatable::FieldPat(fp) => fp.tokens_mut(),
95+
Annotatable::GenericParam(gp) => gp.tokens_mut(),
96+
Annotatable::Param(p) => p.tokens_mut(),
97+
Annotatable::StructField(sf) => sf.tokens_mut(),
98+
Annotatable::Variant(v) => v.tokens_mut(),
99+
}
86100
}
87101
}
88102

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ impl<'a> Parser<'a> {
7272
let cursor_snapshot = self.token_cursor.clone();
7373

7474
let (mut ret, trailing_token) = f(self, attrs.attrs)?;
75+
let tokens = match ret.tokens_mut() {
76+
Some(tokens) if tokens.is_none() => tokens,
77+
_ => return Ok(ret),
78+
};
7579

7680
// Produces a `TokenStream` on-demand. Using `cursor_snapshot`
7781
// and `num_calls`, we can reconstruct the `TokenStream` seen
@@ -128,14 +132,14 @@ impl<'a> Parser<'a> {
128132
}
129133
}
130134

131-
let lazy_impl = LazyTokenStreamImpl {
135+
*tokens = Some(LazyTokenStream::new(LazyTokenStreamImpl {
132136
start_token,
133137
num_calls,
134138
cursor_snapshot,
135139
desugar_doc_comments: self.desugar_doc_comments,
136140
append_unglued_token: self.token_cursor.append_unglued_token.clone(),
137-
};
138-
ret.finalize_tokens(LazyTokenStream::new(lazy_impl));
141+
}));
142+
139143
Ok(ret)
140144
}
141145
}

0 commit comments

Comments
 (0)