Skip to content

Commit fee1525

Browse files
committed
Rework Attribute::get_tokens.
Returning `Vec<TokenTree>` works better for the call sites than returning `TokenStream`.
1 parent 8a390ba commit fee1525

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,18 @@ impl Attribute {
202202
}
203203
}
204204

205-
// Named `get_tokens` to distinguish it from the `<Attribute as HasTokens>::tokens` method.
206-
pub fn get_tokens(&self) -> TokenStream {
207-
match &self.kind {
208-
AttrKind::Normal(normal) => TokenStream::new(
209-
normal
210-
.tokens
211-
.as_ref()
212-
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
213-
.to_attr_token_stream()
214-
.to_token_trees(),
215-
),
216-
&AttrKind::DocComment(comment_kind, data) => TokenStream::token_alone(
205+
pub fn token_trees(&self) -> Vec<TokenTree> {
206+
match self.kind {
207+
AttrKind::Normal(ref normal) => normal
208+
.tokens
209+
.as_ref()
210+
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
211+
.to_attr_token_stream()
212+
.to_token_trees(),
213+
AttrKind::DocComment(comment_kind, data) => vec![TokenTree::token_alone(
217214
token::DocComment(comment_kind, self.style, data),
218215
self.span,
219-
),
216+
)],
220217
}
221218
}
222219
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ impl AttrTokenStream {
225225
// properly implemented - we always synthesize fake tokens,
226226
// so we never reach this code.
227227

228-
let mut stream = TokenStream::default();
228+
let mut tts = vec![];
229229
for inner_attr in inner_attrs {
230-
stream.push_stream(inner_attr.get_tokens());
230+
tts.extend(inner_attr.token_trees());
231231
}
232-
stream.push_stream(delim_tokens.clone());
232+
tts.extend(delim_tokens.0.iter().cloned());
233+
let stream = TokenStream::new(tts);
233234
*tree = TokenTree::Delimited(*span, *spacing, *delim, stream);
234235
found = true;
235236
break;
@@ -242,7 +243,7 @@ impl AttrTokenStream {
242243
);
243244
}
244245
for attr in outer_attrs {
245-
res.extend(attr.get_tokens().0.iter().cloned());
246+
res.extend(attr.token_trees());
246247
}
247248
res.extend(target_tokens);
248249
}

compiler/rustc_expand/src/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ impl<'a> StripUnconfigured<'a> {
292292
attr: &Attribute,
293293
(item, item_span): (ast::AttrItem, Span),
294294
) -> Attribute {
295-
let orig_tokens = attr.get_tokens();
296-
297295
// We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
298296
// and producing an attribute of the form `#[attr]`. We
299297
// have captured tokens for `attr` itself, but we need to
@@ -302,7 +300,7 @@ impl<'a> StripUnconfigured<'a> {
302300

303301
// Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
304302
// for `attr` when we expand it to `#[attr]`
305-
let mut orig_trees = orig_tokens.trees();
303+
let mut orig_trees = attr.token_trees().into_iter();
306304
let TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _) =
307305
orig_trees.next().unwrap().clone()
308306
else {

0 commit comments

Comments
 (0)