Skip to content

Commit f852568

Browse files
committed
Change AttrTokenStream::to_tokenstream to to_token_trees.
I.e. change the return type from `TokenStream` to `Vec<TokenTree>`. Most of the callsites require a `TokenStream`, but the recursive call used to create `target_tokens` requires a `Vec<TokenTree>`. It's easy to convert a `Vec<TokenTree>` to a `TokenStream` (just call `TokenStream::new`) but it's harder to convert a `TokenStream` to a `Vec<TokenTree>` (either iterate/clone/collect, or use `Lrc::into_inner` if appropriate). So this commit changes the return value to simplify that `target_tokens` call site.
1 parent d6c0b81 commit f852568

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ impl Attribute {
204204

205205
pub fn tokens(&self) -> TokenStream {
206206
match &self.kind {
207-
AttrKind::Normal(normal) => normal
208-
.tokens
209-
.as_ref()
210-
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
211-
.to_attr_token_stream()
212-
.to_tokenstream(),
207+
AttrKind::Normal(normal) => TokenStream::new(
208+
normal
209+
.tokens
210+
.as_ref()
211+
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
212+
.to_attr_token_stream()
213+
.to_token_trees(),
214+
),
213215
&AttrKind::DocComment(comment_kind, data) => TokenStream::token_alone(
214216
token::DocComment(comment_kind, self.style, data),
215217
self.span,

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,13 @@ impl AttrTokenStream {
180180
AttrTokenStream(Lrc::new(tokens))
181181
}
182182

183-
/// Converts this `AttrTokenStream` to a plain `TokenStream`.
183+
/// Converts this `AttrTokenStream` to a plain `Vec<TokenTree>`.
184184
/// During conversion, `AttrTokenTree::Attributes` get 'flattened'
185185
/// back to a `TokenStream` of the form `outer_attr attr_target`.
186186
/// If there are inner attributes, they are inserted into the proper
187187
/// place in the attribute target tokens.
188-
pub fn to_tokenstream(&self) -> TokenStream {
189-
let trees: Vec<_> = self
190-
.0
188+
pub fn to_token_trees(&self) -> Vec<TokenTree> {
189+
self.0
191190
.iter()
192191
.flat_map(|tree| match &tree {
193192
AttrTokenTree::Token(inner, spacing) => {
@@ -198,7 +197,7 @@ impl AttrTokenStream {
198197
*span,
199198
*spacing,
200199
*delim,
201-
stream.to_tokenstream()
200+
TokenStream::new(stream.to_token_trees())
202201
),]
203202
.into_iter()
204203
}
@@ -208,14 +207,7 @@ impl AttrTokenStream {
208207
.partition_point(|attr| matches!(attr.style, crate::AttrStyle::Outer));
209208
let (outer_attrs, inner_attrs) = data.attrs.split_at(idx);
210209

211-
let mut target_tokens: Vec<_> = data
212-
.tokens
213-
.to_attr_token_stream()
214-
.to_tokenstream()
215-
.0
216-
.iter()
217-
.cloned()
218-
.collect();
210+
let mut target_tokens = data.tokens.to_attr_token_stream().to_token_trees();
219211
if !inner_attrs.is_empty() {
220212
let mut found = false;
221213
// Check the last two trees (to account for a trailing semi)
@@ -260,8 +252,7 @@ impl AttrTokenStream {
260252
flat.into_iter()
261253
}
262254
})
263-
.collect();
264-
TokenStream::new(trees)
255+
.collect()
265256
}
266257
}
267258

@@ -461,7 +452,7 @@ impl TokenStream {
461452
AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
462453
AttrTokenStream::new(vec![AttrTokenTree::Attributes(attr_data)])
463454
};
464-
attr_stream.to_tokenstream()
455+
TokenStream::new(attr_stream.to_token_trees())
465456
}
466457

467458
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {

0 commit comments

Comments
 (0)