summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2024-07-10 14:51:41 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2024-07-10 14:51:41 +1000
commitfee152556fda2260a17fa24cea21d18b57c8460d (patch)
tree941096aa66c3942d40c74a23a067564e23f0ce11
parent8a390bae06dbb5686b581797b8f46cb0353dc255 (diff)
Rework `Attribute::get_tokens`.
Returning `Vec<TokenTree>` works better for the call sites than returning `TokenStream`.
-rw-r--r--compiler/rustc_ast/src/attr/mod.rs23
-rw-r--r--compiler/rustc_ast/src/tokenstream.rs9
-rw-r--r--compiler/rustc_expand/src/config.rs4
3 files changed, 16 insertions, 20 deletions
diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs
index 088ae9ba441..d2c7b1c0753 100644
--- a/compiler/rustc_ast/src/attr/mod.rs
+++ b/compiler/rustc_ast/src/attr/mod.rs
@@ -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,
- ),
+ )],
}
}
}
diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs
index ee068f19332..1f2adde2570 100644
--- a/compiler/rustc_ast/src/tokenstream.rs
+++ b/compiler/rustc_ast/src/tokenstream.rs
@@ -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);
}
diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs
index 40e16b45115..8d796500dcc 100644
--- a/compiler/rustc_expand/src/config.rs
+++ b/compiler/rustc_expand/src/config.rs
@@ -292,8 +292,6 @@ impl<'a> StripUnconfigured<'a> {
attr: &Attribute,
(item, item_span): (ast::AttrItem, Span),
) -> Attribute {
- let orig_tokens = attr.get_tokens();
-
// We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
// and producing an attribute of the form `#[attr]`. We
// have captured tokens for `attr` itself, but we need to
@@ -302,7 +300,7 @@ impl<'a> StripUnconfigured<'a> {
// Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
// for `attr` when we expand it to `#[attr]`
- let mut orig_trees = orig_tokens.trees();
+ let mut orig_trees = attr.token_trees().into_iter();
let TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _) =
orig_trees.next().unwrap().clone()
else {