Skip to content

Commit b17a82d

Browse files
committed
Remove #[cfg] attributes during cfg-expansion
Currently, we don't remove `#[cfg]` attributes from a target when the predicates pass. This PR removes all 'passing' `#[cfg]` attributes during cfg-expansion, which causes derive proc-macros to never see any `#[cfg]` attributes in the input token stream. With #82608 merged, we can now do this without losing spans.
1 parent 7953910 commit b17a82d

8 files changed

+27
-272
lines changed

compiler/rustc_expand/src/config.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl<'a> StripUnconfigured<'a> {
246246
pub fn configure<T: AstLike>(&mut self, mut node: T) -> Option<T> {
247247
self.process_cfg_attrs(&mut node);
248248
if self.in_cfg(node.attrs()) {
249+
node.visit_attrs(|attrs| remove_cfg(attrs));
249250
self.try_configure_tokens(&mut node);
250251
Some(node)
251252
} else {
@@ -267,7 +268,12 @@ impl<'a> StripUnconfigured<'a> {
267268
mut attrs: Vec<ast::Attribute>,
268269
) -> Option<Vec<ast::Attribute>> {
269270
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
270-
if self.in_cfg(&attrs) { Some(attrs) } else { None }
271+
if self.in_cfg(&attrs) {
272+
remove_cfg(&mut attrs);
273+
Some(attrs)
274+
} else {
275+
None
276+
}
271277
}
272278

273279
/// Performs cfg-expansion on `stream`, producing a new `AttrAnnotatedTokenStream`.
@@ -294,9 +300,10 @@ impl<'a> StripUnconfigured<'a> {
294300
AttrAnnotatedTokenTree::Attributes(mut data) => {
295301
let mut attrs: Vec<_> = std::mem::take(&mut data.attrs).into();
296302
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
297-
data.attrs = attrs.into();
298303

299-
if self.in_cfg(&data.attrs) {
304+
if self.in_cfg(&attrs) {
305+
remove_cfg(&mut attrs);
306+
data.attrs = attrs.into();
300307
data.tokens = LazyTokenStream::new(
301308
self.configure_tokens(&data.tokens.create_token_stream()),
302309
);
@@ -535,3 +542,7 @@ impl<'a> StripUnconfigured<'a> {
535542
fn is_cfg(sess: &Session, attr: &Attribute) -> bool {
536543
sess.check_name(attr, sym::cfg)
537544
}
545+
546+
fn remove_cfg(attrs: &mut Vec<Attribute>) {
547+
attrs.retain(|a| !a.has_name(sym::cfg));
548+
}

src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ macro_rules! generate_s10 {
2929
($expr: expr) => {
3030
#[cfg(feature = $expr)]
3131
//~^ ERROR expected unsuffixed literal or identifier, found `concat!("nonexistent")`
32-
//~| ERROR expected unsuffixed literal or identifier, found `concat!("nonexistent")`
3332
struct S10;
3433
}
3534
}

src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,7 @@ LL | generate_s10!(concat!("nonexistent"));
6363
|
6464
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
6565

66-
error: expected unsuffixed literal or identifier, found `concat!("nonexistent")`
67-
--> $DIR/cfg-attr-syntax-validation.rs:30:25
68-
|
69-
LL | #[cfg(feature = $expr)]
70-
| ^^^^^
71-
...
72-
LL | generate_s10!(concat!("nonexistent"));
73-
| -------------------------------------- in this macro invocation
74-
|
75-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
76-
77-
error: aborting due to 11 previous errors
66+
error: aborting due to 10 previous errors
7867

7968
Some errors have detailed explanations: E0537, E0565.
8069
For more information about an error, try `rustc --explain E0537`.

src/test/ui/proc-macro/cfg-eval.stdout

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): struct S1 { #[cfg(all())] #[allow()] field_true : u8, }
1+
PRINT-ATTR INPUT (DISPLAY): struct S1 { #[allow()] field_true : u8, }
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -11,36 +11,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
1111
Group {
1212
delimiter: Brace,
1313
stream: TokenStream [
14-
Punct {
15-
ch: '#',
16-
spacing: Alone,
17-
span: $DIR/cfg-eval.rs:20:5: 20:6 (#0),
18-
},
19-
Group {
20-
delimiter: Bracket,
21-
stream: TokenStream [
22-
Ident {
23-
ident: "cfg",
24-
span: $DIR/cfg-eval.rs:20:7: 20:10 (#0),
25-
},
26-
Group {
27-
delimiter: Parenthesis,
28-
stream: TokenStream [
29-
Ident {
30-
ident: "all",
31-
span: $DIR/cfg-eval.rs:20:11: 20:14 (#0),
32-
},
33-
Group {
34-
delimiter: Parenthesis,
35-
stream: TokenStream [],
36-
span: $DIR/cfg-eval.rs:20:14: 20:24 (#0),
37-
},
38-
],
39-
span: $DIR/cfg-eval.rs:20:10: 20:25 (#0),
40-
},
41-
],
42-
span: $DIR/cfg-eval.rs:20:6: 20:26 (#0),
43-
},
4414
Punct {
4515
ch: '#',
4616
spacing: Alone,
@@ -83,7 +53,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
8353
span: $DIR/cfg-eval.rs:17:11: 24:2 (#0),
8454
},
8555
]
86-
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] (#[cfg(all())] 1,)
56+
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] (1,)
8757
PRINT-ATTR INPUT (DEBUG): TokenStream [
8858
Punct {
8959
ch: '#',
@@ -103,36 +73,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
10373
Group {
10474
delimiter: Parenthesis,
10575
stream: TokenStream [
106-
Punct {
107-
ch: '#',
108-
spacing: Alone,
109-
span: $DIR/cfg-eval.rs:36:23: 36:24 (#0),
110-
},
111-
Group {
112-
delimiter: Bracket,
113-
stream: TokenStream [
114-
Ident {
115-
ident: "cfg",
116-
span: $DIR/cfg-eval.rs:36:25: 36:28 (#0),
117-
},
118-
Group {
119-
delimiter: Parenthesis,
120-
stream: TokenStream [
121-
Ident {
122-
ident: "all",
123-
span: $DIR/cfg-eval.rs:36:29: 36:32 (#0),
124-
},
125-
Group {
126-
delimiter: Parenthesis,
127-
stream: TokenStream [],
128-
span: $DIR/cfg-eval.rs:36:32: 36:42 (#0),
129-
},
130-
],
131-
span: $DIR/cfg-eval.rs:36:28: 36:43 (#0),
132-
},
133-
],
134-
span: $DIR/cfg-eval.rs:36:24: 36:44 (#0),
135-
},
13676
Literal {
13777
kind: Integer,
13878
symbol: "1",

0 commit comments

Comments
 (0)