Skip to content

Commit ebfd98b

Browse files
committed
fix token issue
1 parent 743533b commit ebfd98b

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,15 @@ impl Attribute {
323323
}
324324
}
325325

326-
pub fn token_trees(&self) -> Vec<TokenTree> {
326+
pub fn token_trees(&self) -> Option<Vec<TokenTree>> {
327327
match self.kind {
328-
AttrKind::Normal(ref normal) => normal
329-
.tokens
330-
.as_ref()
331-
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
332-
.to_attr_token_stream()
333-
.to_token_trees(),
334-
AttrKind::DocComment(comment_kind, data) => vec![TokenTree::token_alone(
328+
AttrKind::Normal(ref normal) => {
329+
Some(normal.tokens.as_ref()?.to_attr_token_stream().to_token_trees())
330+
}
331+
AttrKind::DocComment(comment_kind, data) => Some(vec![TokenTree::token_alone(
335332
token::DocComment(comment_kind, self.style, data),
336333
self.span,
337-
)],
334+
)]),
338335
}
339336
}
340337
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,7 @@ fn attrs_and_tokens_to_token_trees(
455455
let (outer_attrs, inner_attrs) = attrs.split_at(idx);
456456

457457
// Add outer attribute tokens.
458-
for attr in outer_attrs {
459-
res.extend(attr.token_trees());
460-
}
458+
res.extend(outer_attrs.iter().filter_map(Attribute::token_trees).flatten());
461459

462460
// Add target AST node tokens.
463461
res.extend(target_tokens.to_attr_token_stream().to_token_trees());
@@ -485,10 +483,12 @@ fn attrs_and_tokens_to_token_trees(
485483
for tree in tts.iter_mut().rev() {
486484
if let TokenTree::Delimited(span, spacing, Delimiter::Brace, stream) = tree {
487485
// Found it: the rightmost, outermost braced group.
488-
let mut tts = vec![];
489-
for inner_attr in inner_attrs {
490-
tts.extend(inner_attr.token_trees());
491-
}
486+
let mut tts = inner_attrs
487+
.iter()
488+
.filter_map(Attribute::token_trees)
489+
.flatten()
490+
.collect::<Vec<_>>();
491+
492492
tts.extend(stream.0.iter().cloned());
493493
let stream = TokenStream::new(tts);
494494
*tree = TokenTree::Delimited(*span, *spacing, Delimiter::Brace, stream);

compiler/rustc_expand/src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ impl<'a> StripUnconfigured<'a> {
336336
// Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
337337

338338
// Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
339-
let mut orig_trees = cfg_attr.token_trees().into_iter();
339+
let mut orig_trees = cfg_attr
340+
.token_trees()
341+
.unwrap_or_else(|| panic!("#[cfg_attr] should always have token_trees"))
342+
.into_iter();
340343
let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
341344
orig_trees.next()
342345
else {

0 commit comments

Comments
 (0)