Skip to content

Commit 1706777

Browse files
authored
Unrolled build for rust-lang#128133
Rollup merge of rust-lang#128133 - nnethercote:fix-cfg_attr-spans, r=petrochenkov Improve spans on evaluated `cfg_attr`s. When converting something like `#![cfg_attr(cond, attr)]` into `#![attr]`, we currently duplicate the `#` token and the `!` token. But weirdly, there is also this comment: // We don't really have a good span to use for the synthesized `[]` // in `#[attr]`, so just use the span of the `#` token. Maybe that comment used to be true? But now it is false: we can duplicate the existing delimiters (and their spans and spacing), much like we do for the `#` and `!`. This commit does that, thus removing the incorrect comment, and improving the spans on `Group`s in a few proc-macro tests. `@petrochenkov`
2 parents 2ccafed + ac26b88 commit 1706777

File tree

7 files changed

+41
-41
lines changed

7 files changed

+41
-41
lines changed

compiler/rustc_expand/src/config.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::errors::{
66
};
77
use rustc_ast::ptr::P;
88
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};
1010
use rustc_ast::tokenstream::{LazyAttrTokenStream, TokenTree};
1111
use rustc_ast::NodeId;
1212
use rustc_ast::{self as ast, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem};
@@ -298,47 +298,47 @@ impl<'a> StripUnconfigured<'a> {
298298
cfg_attr: &Attribute,
299299
(item, item_span): (ast::AttrItem, Span),
300300
) -> 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]`.
309304
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()
312307
else {
313308
panic!("Bad tokens for attribute {cfg_attr:?}");
314309
};
315310

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()
331315
else {
332316
panic!("Bad tokens for attribute {cfg_attr:?}");
333317
};
334318
vec![
335319
AttrTokenTree::Token(pound_token, Spacing::Joint),
336320
AttrTokenTree::Token(bang_token, Spacing::JointHidden),
337-
bracket_group,
338321
]
339322
} else {
340-
vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden), bracket_group]
323+
vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
341324
};
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+
342342
let tokens = Some(LazyAttrTokenStream::new(AttrTokenStream::new(trees)));
343343
let attr = attr::mk_attr_from_item(
344344
&self.sess.psess.attr_id_generator,

tests/ui/proc-macro/cfg-eval-inner.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
7373
span: $DIR/cfg-eval-inner.rs:19:40: 19:54 (#0),
7474
},
7575
],
76-
span: $DIR/cfg-eval-inner.rs:19:5: 19:6 (#0),
76+
span: $DIR/cfg-eval-inner.rs:19:7: 19:56 (#0),
7777
},
7878
Punct {
7979
ch: '#',
@@ -168,7 +168,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
168168
span: $DIR/cfg-eval-inner.rs:23:48: 23:70 (#0),
169169
},
170170
],
171-
span: $DIR/cfg-eval-inner.rs:23:13: 23:14 (#0),
171+
span: $DIR/cfg-eval-inner.rs:23:15: 23:72 (#0),
172172
},
173173
Literal {
174174
kind: Integer,
@@ -233,7 +233,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
233233
span: $DIR/cfg-eval-inner.rs:32:40: 32:56 (#0),
234234
},
235235
],
236-
span: $DIR/cfg-eval-inner.rs:32:5: 32:6 (#0),
236+
span: $DIR/cfg-eval-inner.rs:32:7: 32:58 (#0),
237237
},
238238
Ident {
239239
ident: "fn",

tests/ui/proc-macro/cfg-eval.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
6060
span: $DIR/cfg-eval.rs:22:36: 22:38 (#0),
6161
},
6262
],
63-
span: $DIR/cfg-eval.rs:22:5: 22:6 (#0),
63+
span: $DIR/cfg-eval.rs:22:6: 22:40 (#0),
6464
},
6565
Ident {
6666
ident: "field_true",
@@ -99,7 +99,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
9999
span: $DIR/cfg-eval.rs:35:62: 35:73 (#0),
100100
},
101101
],
102-
span: $DIR/cfg-eval.rs:35:39: 35:40 (#0),
102+
span: $DIR/cfg-eval.rs:35:40: 35:75 (#0),
103103
},
104104
Group {
105105
delimiter: Parenthesis,

tests/ui/proc-macro/expand-to-derive.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
5757
span: $DIR/expand-to-derive.rs:27:28: 27:39 (#0),
5858
},
5959
],
60-
span: $DIR/expand-to-derive.rs:27:5: 27:6 (#0),
60+
span: $DIR/expand-to-derive.rs:27:6: 27:41 (#0),
6161
},
6262
Ident {
6363
ident: "struct",

tests/ui/proc-macro/inner-attrs.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
674674
span: $DIR/inner-attrs.rs:41:52: 41:59 (#0),
675675
},
676676
],
677-
span: $DIR/inner-attrs.rs:41:17: 41:18 (#0),
677+
span: $DIR/inner-attrs.rs:41:19: 41:61 (#0),
678678
},
679679
Ident {
680680
ident: "true",

tests/ui/proc-macro/issue-75930-derive-cfg.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
119119
span: $DIR/issue-75930-derive-cfg.rs:50:29: 50:40 (#0),
120120
},
121121
],
122-
span: $DIR/issue-75930-derive-cfg.rs:50:1: 50:2 (#0),
122+
span: $DIR/issue-75930-derive-cfg.rs:50:2: 50:42 (#0),
123123
},
124124
Punct {
125125
ch: '#',
@@ -1395,7 +1395,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
13951395
span: $DIR/issue-75930-derive-cfg.rs:50:29: 50:40 (#0),
13961396
},
13971397
],
1398-
span: $DIR/issue-75930-derive-cfg.rs:50:1: 50:2 (#0),
1398+
span: $DIR/issue-75930-derive-cfg.rs:50:2: 50:42 (#0),
13991399
},
14001400
Punct {
14011401
ch: '#',
@@ -1571,7 +1571,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
15711571
span: $DIR/issue-75930-derive-cfg.rs:63:41: 63:51 (#0),
15721572
},
15731573
],
1574-
span: $DIR/issue-75930-derive-cfg.rs:63:13: 63:14 (#0),
1574+
span: $DIR/issue-75930-derive-cfg.rs:63:14: 63:53 (#0),
15751575
},
15761576
Ident {
15771577
ident: "false",

tests/ui/proc-macro/macro-rules-derive-cfg.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
8888
span: $DIR/macro-rules-derive-cfg.rs:19:59: 19:66 (#3),
8989
},
9090
],
91-
span: $DIR/macro-rules-derive-cfg.rs:19:25: 19:26 (#3),
91+
span: $DIR/macro-rules-derive-cfg.rs:19:26: 19:68 (#3),
9292
},
9393
Punct {
9494
ch: '#',
@@ -113,7 +113,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
113113
span: $DIR/macro-rules-derive-cfg.rs:26:47: 26:55 (#0),
114114
},
115115
],
116-
span: $DIR/macro-rules-derive-cfg.rs:26:13: 26:14 (#0),
116+
span: $DIR/macro-rules-derive-cfg.rs:26:14: 26:57 (#0),
117117
},
118118
Group {
119119
delimiter: Brace,
@@ -146,7 +146,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
146146
span: $DIR/macro-rules-derive-cfg.rs:27:34: 27:42 (#0),
147147
},
148148
],
149-
span: $DIR/macro-rules-derive-cfg.rs:27:5: 27:6 (#0),
149+
span: $DIR/macro-rules-derive-cfg.rs:27:7: 27:44 (#0),
150150
},
151151
Literal {
152152
kind: Integer,

0 commit comments

Comments
 (0)