1
Fork 0

Rework Attribute::get_tokens.

Returning `Vec<TokenTree>` works better for the call sites than
returning `TokenStream`.
This commit is contained in:
Nicholas Nethercote 2024-07-10 14:51:41 +10:00
parent 8a390bae06
commit fee152556f
3 changed files with 16 additions and 20 deletions

View file

@ -202,21 +202,18 @@ impl Attribute {
}
}
// Named `get_tokens` to distinguish it from the `<Attribute as HasTokens>::tokens` method.
pub fn get_tokens(&self) -> TokenStream {
match &self.kind {
AttrKind::Normal(normal) => TokenStream::new(
normal
.tokens
.as_ref()
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
.to_attr_token_stream()
.to_token_trees(),
),
&AttrKind::DocComment(comment_kind, data) => TokenStream::token_alone(
pub fn token_trees(&self) -> Vec<TokenTree> {
match self.kind {
AttrKind::Normal(ref normal) => normal
.tokens
.as_ref()
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
.to_attr_token_stream()
.to_token_trees(),
AttrKind::DocComment(comment_kind, data) => vec![TokenTree::token_alone(
token::DocComment(comment_kind, self.style, data),
self.span,
),
)],
}
}
}

View file

@ -225,11 +225,12 @@ impl AttrTokenStream {
// properly implemented - we always synthesize fake tokens,
// so we never reach this code.
let mut stream = TokenStream::default();
let mut tts = vec![];
for inner_attr in inner_attrs {
stream.push_stream(inner_attr.get_tokens());
tts.extend(inner_attr.token_trees());
}
stream.push_stream(delim_tokens.clone());
tts.extend(delim_tokens.0.iter().cloned());
let stream = TokenStream::new(tts);
*tree = TokenTree::Delimited(*span, *spacing, *delim, stream);
found = true;
break;
@ -242,7 +243,7 @@ impl AttrTokenStream {
);
}
for attr in outer_attrs {
res.extend(attr.get_tokens().0.iter().cloned());
res.extend(attr.token_trees());
}
res.extend(target_tokens);
}