@@ -6,7 +6,7 @@ use crate::errors::{
6
6
} ;
7
7
use rustc_ast:: ptr:: P ;
8
8
use rustc_ast:: token:: { Delimiter , Token , TokenKind } ;
9
- use rustc_ast:: tokenstream:: { AttrTokenStream , AttrTokenTree , DelimSpacing , DelimSpan , Spacing } ;
9
+ use rustc_ast:: tokenstream:: { AttrTokenStream , AttrTokenTree , Spacing } ;
10
10
use rustc_ast:: tokenstream:: { LazyAttrTokenStream , TokenTree } ;
11
11
use rustc_ast:: NodeId ;
12
12
use rustc_ast:: { self as ast, AttrStyle , Attribute , HasAttrs , HasTokens , MetaItem } ;
@@ -298,47 +298,47 @@ impl<'a> StripUnconfigured<'a> {
298
298
cfg_attr : & Attribute ,
299
299
( item, item_span) : ( ast:: AttrItem , Span ) ,
300
300
) -> Attribute {
301
- // We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
302
- // and producing an attribute of the form `#[attr]`. We
303
- // have captured tokens for `attr` itself, but we need to
304
- // synthesize tokens for the wrapper `#` and `[]`, which
305
- // we do below.
306
-
307
- // Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
308
- // for `attr` when we expand it to `#[attr]`
301
+ // Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
302
+
303
+ // Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
309
304
let mut orig_trees = cfg_attr. token_trees ( ) . into_iter ( ) ;
310
- let TokenTree :: Token ( pound_token @ Token { kind : TokenKind :: Pound , .. } , _) =
311
- orig_trees. next ( ) . unwrap ( ) . clone ( )
305
+ let Some ( TokenTree :: Token ( pound_token @ Token { kind : TokenKind :: Pound , .. } , _) ) =
306
+ orig_trees. next ( )
312
307
else {
313
308
panic ! ( "Bad tokens for attribute {cfg_attr:?}" ) ;
314
309
} ;
315
310
316
- // We don't really have a good span to use for the synthesized `[]`
317
- // in `#[attr]`, so just use the span of the `#` token.
318
- let bracket_group = AttrTokenTree :: Delimited (
319
- DelimSpan :: from_single ( pound_token. span ) ,
320
- DelimSpacing :: new ( Spacing :: JointHidden , Spacing :: Alone ) ,
321
- Delimiter :: Bracket ,
322
- item. tokens
323
- . as_ref ( )
324
- . unwrap_or_else ( || panic ! ( "Missing tokens for {item:?}" ) )
325
- . to_attr_token_stream ( ) ,
326
- ) ;
327
- let trees = if cfg_attr. style == AttrStyle :: Inner {
328
- // For inner attributes, we do the same thing for the `!` in `#![some_attr]`
329
- let TokenTree :: Token ( bang_token @ Token { kind : TokenKind :: Not , .. } , _) =
330
- orig_trees. next ( ) . unwrap ( ) . clone ( )
311
+ // For inner attributes, we do the same thing for the `!` in `#![attr]`.
312
+ let mut trees = if cfg_attr. style == AttrStyle :: Inner {
313
+ let Some ( TokenTree :: Token ( bang_token @ Token { kind : TokenKind :: Not , .. } , _) ) =
314
+ orig_trees. next ( )
331
315
else {
332
316
panic ! ( "Bad tokens for attribute {cfg_attr:?}" ) ;
333
317
} ;
334
318
vec ! [
335
319
AttrTokenTree :: Token ( pound_token, Spacing :: Joint ) ,
336
320
AttrTokenTree :: Token ( bang_token, Spacing :: JointHidden ) ,
337
- bracket_group,
338
321
]
339
322
} else {
340
- vec ! [ AttrTokenTree :: Token ( pound_token, Spacing :: JointHidden ) , bracket_group ]
323
+ vec ! [ AttrTokenTree :: Token ( pound_token, Spacing :: JointHidden ) ]
341
324
} ;
325
+
326
+ // And the same thing for the `[`/`]` delimiters in `#[attr]`.
327
+ let Some ( TokenTree :: Delimited ( delim_span, delim_spacing, Delimiter :: Bracket , _) ) =
328
+ orig_trees. next ( )
329
+ else {
330
+ panic ! ( "Bad tokens for attribute {cfg_attr:?}" ) ;
331
+ } ;
332
+ trees. push ( AttrTokenTree :: Delimited (
333
+ delim_span,
334
+ delim_spacing,
335
+ Delimiter :: Bracket ,
336
+ item. tokens
337
+ . as_ref ( )
338
+ . unwrap_or_else ( || panic ! ( "Missing tokens for {item:?}" ) )
339
+ . to_attr_token_stream ( ) ,
340
+ ) ) ;
341
+
342
342
let tokens = Some ( LazyAttrTokenStream :: new ( AttrTokenStream :: new ( trees) ) ) ;
343
343
let attr = attr:: mk_attr_from_item (
344
344
& self . sess . psess . attr_id_generator ,
0 commit comments