Skip to content

Commit 130359c

Browse files
committed
Auto merge of rust-lang#76010 - Aaron1011:fix/cfg-generic-param, r=petrochenkov
Run cfg-stripping on generic parameters before invoking derive macros Fixes rust-lang#75930 This changes the tokens seen by a proc-macro. However, ising a `#[cfg]` attribute on a generic paramter is unusual, and combining it with a proc-macro derive is probably even more unusual. I don't expect this to cause any breakage.
2 parents eb9e7c3 + a97dcfa commit 130359c

File tree

3 files changed

+258
-4
lines changed

3 files changed

+258
-4
lines changed

compiler/rustc_expand/src/config.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,6 @@ impl<'a> StripUnconfigured<'a> {
403403
items.flat_map_in_place(|item| self.configure(item));
404404
}
405405

406-
pub fn configure_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
407-
params.flat_map_in_place(|param| self.configure(param));
408-
}
409-
410406
fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
411407
match vdata {
412408
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) => {
@@ -496,6 +492,13 @@ impl<'a> MutVisitor for StripUnconfigured<'a> {
496492
Some(expr)
497493
}
498494

495+
fn flat_map_generic_param(
496+
&mut self,
497+
param: ast::GenericParam,
498+
) -> SmallVec<[ast::GenericParam; 1]> {
499+
noop_flat_map_generic_param(configure!(self, param), self)
500+
}
501+
499502
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
500503
noop_flat_map_stmt(configure!(self, stmt), self)
501504
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// check-pass
2+
// compile-flags: -Z span-debug
3+
// aux-build:test-macros.rs
4+
5+
// Regression test for issue #75930
6+
// Tests that we cfg-strip all targets before invoking
7+
// a derive macro
8+
9+
#[macro_use]
10+
extern crate test_macros;
11+
12+
#[derive(Print)]
13+
struct Foo<#[cfg(FALSE)] A, B> {
14+
#[cfg(FALSE)] first: String,
15+
second: bool,
16+
third: [u8; {
17+
#[cfg(FALSE)] struct Bar;
18+
#[cfg(not(FALSE))] struct Inner;
19+
#[cfg(FALSE)] let a = 25;
20+
match true {
21+
#[cfg(FALSE)] true => {},
22+
false => {},
23+
_ => {}
24+
};
25+
0
26+
}],
27+
fourth: B
28+
}
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
PRINT-DERIVE INPUT (DISPLAY): struct Foo < B >
2+
{
3+
second : bool, third :
4+
[u8 ;
5+
{
6+
#[cfg(not(FALSE))] struct Inner ; match true
7+
{ false => { } _ => { } } ; 0
8+
}], fourth : B,
9+
}
10+
PRINT-DERIVE INPUT (DEBUG): TokenStream [
11+
Ident {
12+
ident: "struct",
13+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
14+
},
15+
Ident {
16+
ident: "Foo",
17+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
18+
},
19+
Punct {
20+
ch: '<',
21+
spacing: Alone,
22+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
23+
},
24+
Ident {
25+
ident: "B",
26+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
27+
},
28+
Punct {
29+
ch: '>',
30+
spacing: Alone,
31+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
32+
},
33+
Group {
34+
delimiter: Brace,
35+
stream: TokenStream [
36+
Ident {
37+
ident: "second",
38+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
39+
},
40+
Punct {
41+
ch: ':',
42+
spacing: Alone,
43+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
44+
},
45+
Ident {
46+
ident: "bool",
47+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
48+
},
49+
Punct {
50+
ch: ',',
51+
spacing: Alone,
52+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
53+
},
54+
Ident {
55+
ident: "third",
56+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
57+
},
58+
Punct {
59+
ch: ':',
60+
spacing: Alone,
61+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
62+
},
63+
Group {
64+
delimiter: Bracket,
65+
stream: TokenStream [
66+
Ident {
67+
ident: "u8",
68+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
69+
},
70+
Punct {
71+
ch: ';',
72+
spacing: Alone,
73+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
74+
},
75+
Group {
76+
delimiter: Brace,
77+
stream: TokenStream [
78+
Punct {
79+
ch: '#',
80+
spacing: Alone,
81+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
82+
},
83+
Group {
84+
delimiter: Bracket,
85+
stream: TokenStream [
86+
Ident {
87+
ident: "cfg",
88+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
89+
},
90+
Group {
91+
delimiter: Parenthesis,
92+
stream: TokenStream [
93+
Ident {
94+
ident: "not",
95+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
96+
},
97+
Group {
98+
delimiter: Parenthesis,
99+
stream: TokenStream [
100+
Ident {
101+
ident: "FALSE",
102+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
103+
},
104+
],
105+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
106+
},
107+
],
108+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
109+
},
110+
],
111+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
112+
},
113+
Ident {
114+
ident: "struct",
115+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
116+
},
117+
Ident {
118+
ident: "Inner",
119+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
120+
},
121+
Punct {
122+
ch: ';',
123+
spacing: Alone,
124+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
125+
},
126+
Ident {
127+
ident: "match",
128+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
129+
},
130+
Ident {
131+
ident: "true",
132+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
133+
},
134+
Group {
135+
delimiter: Brace,
136+
stream: TokenStream [
137+
Ident {
138+
ident: "false",
139+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
140+
},
141+
Punct {
142+
ch: '=',
143+
spacing: Joint,
144+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
145+
},
146+
Punct {
147+
ch: '>',
148+
spacing: Alone,
149+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
150+
},
151+
Group {
152+
delimiter: Brace,
153+
stream: TokenStream [],
154+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
155+
},
156+
Ident {
157+
ident: "_",
158+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
159+
},
160+
Punct {
161+
ch: '=',
162+
spacing: Joint,
163+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
164+
},
165+
Punct {
166+
ch: '>',
167+
spacing: Alone,
168+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
169+
},
170+
Group {
171+
delimiter: Brace,
172+
stream: TokenStream [],
173+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
174+
},
175+
],
176+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
177+
},
178+
Punct {
179+
ch: ';',
180+
spacing: Alone,
181+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
182+
},
183+
Literal {
184+
kind: Integer,
185+
symbol: "0",
186+
suffix: None,
187+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
188+
},
189+
],
190+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
191+
},
192+
],
193+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
194+
},
195+
Punct {
196+
ch: ',',
197+
spacing: Alone,
198+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
199+
},
200+
Ident {
201+
ident: "fourth",
202+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
203+
},
204+
Punct {
205+
ch: ':',
206+
spacing: Alone,
207+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
208+
},
209+
Ident {
210+
ident: "B",
211+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
212+
},
213+
Punct {
214+
ch: ',',
215+
spacing: Alone,
216+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
217+
},
218+
],
219+
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
220+
},
221+
]

0 commit comments

Comments
 (0)