Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b17a82d

Browse files
committedApr 11, 2021
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

File tree

8 files changed

+27
-272
lines changed

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",

‎src/test/ui/proc-macro/issue-75930-derive-cfg.stdout

Lines changed: 4 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,12 +1278,10 @@ PRINT-DERIVE INPUT (DISPLAY): #[print_helper(a)] #[allow(dead_code)] #[print_hel
12781278
second : bool, third :
12791279
[u8 ;
12801280
{
1281-
#[cfg(not(FALSE))] struct Inner ; match true
1282-
{ #[allow(warnings)] false => { }, _ => { } } ; #[print_helper(c)]
1283-
#[cfg(not(FALSE))] fn kept_fn()
1284-
{ # ! [cfg(not(FALSE))] let my_val = true ; } enum TupleEnum
1285-
{ Foo(#[cfg(not(FALSE))] i32, u8) } struct
1286-
TupleStruct(#[cfg(not(FALSE))] i32, u8) ; 0
1281+
struct Inner ; match true
1282+
{ #[allow(warnings)] false => { }, _ => { } } ; #[print_helper(c)] fn
1283+
kept_fn() { let my_val = true ; } enum TupleEnum { Foo(i32, u8) }
1284+
struct TupleStruct(i32, u8) ; 0
12871285
}], #[print_helper(d)] fourth : B
12881286
}
12891287
PRINT-DERIVE INPUT (DEBUG): TokenStream [
@@ -1429,41 +1427,6 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
14291427
Group {
14301428
delimiter: Brace,
14311429
stream: TokenStream [
1432-
Punct {
1433-
ch: '#',
1434-
spacing: Alone,
1435-
span: $DIR/issue-75930-derive-cfg.rs:30:9: 30:10 (#0),
1436-
},
1437-
Group {
1438-
delimiter: Bracket,
1439-
stream: TokenStream [
1440-
Ident {
1441-
ident: "cfg",
1442-
span: $DIR/issue-75930-derive-cfg.rs:30:11: 30:14 (#0),
1443-
},
1444-
Group {
1445-
delimiter: Parenthesis,
1446-
stream: TokenStream [
1447-
Ident {
1448-
ident: "not",
1449-
span: $DIR/issue-75930-derive-cfg.rs:30:15: 30:18 (#0),
1450-
},
1451-
Group {
1452-
delimiter: Parenthesis,
1453-
stream: TokenStream [
1454-
Ident {
1455-
ident: "FALSE",
1456-
span: $DIR/issue-75930-derive-cfg.rs:30:19: 30:24 (#0),
1457-
},
1458-
],
1459-
span: $DIR/issue-75930-derive-cfg.rs:30:18: 30:25 (#0),
1460-
},
1461-
],
1462-
span: $DIR/issue-75930-derive-cfg.rs:30:14: 30:26 (#0),
1463-
},
1464-
],
1465-
span: $DIR/issue-75930-derive-cfg.rs:30:10: 30:27 (#0),
1466-
},
14671430
Ident {
14681431
ident: "struct",
14691432
span: $DIR/issue-75930-derive-cfg.rs:30:28: 30:34 (#0),
@@ -1589,41 +1552,6 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
15891552
],
15901553
span: $DIR/issue-75930-derive-cfg.rs:43:10: 43:27 (#0),
15911554
},
1592-
Punct {
1593-
ch: '#',
1594-
spacing: Alone,
1595-
span: $DIR/issue-75930-derive-cfg.rs:43:28: 43:29 (#0),
1596-
},
1597-
Group {
1598-
delimiter: Bracket,
1599-
stream: TokenStream [
1600-
Ident {
1601-
ident: "cfg",
1602-
span: $DIR/issue-75930-derive-cfg.rs:43:30: 43:33 (#0),
1603-
},
1604-
Group {
1605-
delimiter: Parenthesis,
1606-
stream: TokenStream [
1607-
Ident {
1608-
ident: "not",
1609-
span: $DIR/issue-75930-derive-cfg.rs:43:34: 43:37 (#0),
1610-
},
1611-
Group {
1612-
delimiter: Parenthesis,
1613-
stream: TokenStream [
1614-
Ident {
1615-
ident: "FALSE",
1616-
span: $DIR/issue-75930-derive-cfg.rs:43:38: 43:43 (#0),
1617-
},
1618-
],
1619-
span: $DIR/issue-75930-derive-cfg.rs:43:37: 43:44 (#0),
1620-
},
1621-
],
1622-
span: $DIR/issue-75930-derive-cfg.rs:43:33: 43:45 (#0),
1623-
},
1624-
],
1625-
span: $DIR/issue-75930-derive-cfg.rs:43:29: 43:46 (#0),
1626-
},
16271555
Ident {
16281556
ident: "fn",
16291557
span: $DIR/issue-75930-derive-cfg.rs:43:47: 43:49 (#0),
@@ -1640,46 +1568,6 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
16401568
Group {
16411569
delimiter: Brace,
16421570
stream: TokenStream [
1643-
Punct {
1644-
ch: '#',
1645-
spacing: Joint,
1646-
span: $DIR/issue-75930-derive-cfg.rs:44:13: 44:14 (#0),
1647-
},
1648-
Punct {
1649-
ch: '!',
1650-
spacing: Alone,
1651-
span: $DIR/issue-75930-derive-cfg.rs:44:14: 44:15 (#0),
1652-
},
1653-
Group {
1654-
delimiter: Bracket,
1655-
stream: TokenStream [
1656-
Ident {
1657-
ident: "cfg",
1658-
span: $DIR/issue-75930-derive-cfg.rs:44:16: 44:19 (#0),
1659-
},
1660-
Group {
1661-
delimiter: Parenthesis,
1662-
stream: TokenStream [
1663-
Ident {
1664-
ident: "not",
1665-
span: $DIR/issue-75930-derive-cfg.rs:44:20: 44:23 (#0),
1666-
},
1667-
Group {
1668-
delimiter: Parenthesis,
1669-
stream: TokenStream [
1670-
Ident {
1671-
ident: "FALSE",
1672-
span: $DIR/issue-75930-derive-cfg.rs:44:24: 44:29 (#0),
1673-
},
1674-
],
1675-
span: $DIR/issue-75930-derive-cfg.rs:44:23: 44:30 (#0),
1676-
},
1677-
],
1678-
span: $DIR/issue-75930-derive-cfg.rs:44:19: 44:31 (#0),
1679-
},
1680-
],
1681-
span: $DIR/issue-75930-derive-cfg.rs:44:15: 44:32 (#0),
1682-
},
16831571
Ident {
16841572
ident: "let",
16851573
span: $DIR/issue-75930-derive-cfg.rs:45:13: 45:16 (#0),
@@ -1723,41 +1611,6 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
17231611
Group {
17241612
delimiter: Parenthesis,
17251613
stream: TokenStream [
1726-
Punct {
1727-
ch: '#',
1728-
spacing: Alone,
1729-
span: $DIR/issue-75930-derive-cfg.rs:52:17: 52:18 (#0),
1730-
},
1731-
Group {
1732-
delimiter: Bracket,
1733-
stream: TokenStream [
1734-
Ident {
1735-
ident: "cfg",
1736-
span: $DIR/issue-75930-derive-cfg.rs:52:19: 52:22 (#0),
1737-
},
1738-
Group {
1739-
delimiter: Parenthesis,
1740-
stream: TokenStream [
1741-
Ident {
1742-
ident: "not",
1743-
span: $DIR/issue-75930-derive-cfg.rs:52:23: 52:26 (#0),
1744-
},
1745-
Group {
1746-
delimiter: Parenthesis,
1747-
stream: TokenStream [
1748-
Ident {
1749-
ident: "FALSE",
1750-
span: $DIR/issue-75930-derive-cfg.rs:52:27: 52:32 (#0),
1751-
},
1752-
],
1753-
span: $DIR/issue-75930-derive-cfg.rs:52:26: 52:33 (#0),
1754-
},
1755-
],
1756-
span: $DIR/issue-75930-derive-cfg.rs:52:22: 52:34 (#0),
1757-
},
1758-
],
1759-
span: $DIR/issue-75930-derive-cfg.rs:52:18: 52:35 (#0),
1760-
},
17611614
Ident {
17621615
ident: "i32",
17631616
span: $DIR/issue-75930-derive-cfg.rs:52:36: 52:39 (#0),
@@ -1788,41 +1641,6 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
17881641
Group {
17891642
delimiter: Parenthesis,
17901643
stream: TokenStream [
1791-
Punct {
1792-
ch: '#',
1793-
spacing: Alone,
1794-
span: $DIR/issue-75930-derive-cfg.rs:59:13: 59:14 (#0),
1795-
},
1796-
Group {
1797-
delimiter: Bracket,
1798-
stream: TokenStream [
1799-
Ident {
1800-
ident: "cfg",
1801-
span: $DIR/issue-75930-derive-cfg.rs:59:15: 59:18 (#0),
1802-
},
1803-
Group {
1804-
delimiter: Parenthesis,
1805-
stream: TokenStream [
1806-
Ident {
1807-
ident: "not",
1808-
span: $DIR/issue-75930-derive-cfg.rs:59:19: 59:22 (#0),
1809-
},
1810-
Group {
1811-
delimiter: Parenthesis,
1812-
stream: TokenStream [
1813-
Ident {
1814-
ident: "FALSE",
1815-
span: $DIR/issue-75930-derive-cfg.rs:59:23: 59:28 (#0),
1816-
},
1817-
],
1818-
span: $DIR/issue-75930-derive-cfg.rs:59:22: 59:29 (#0),
1819-
},
1820-
],
1821-
span: $DIR/issue-75930-derive-cfg.rs:59:18: 59:30 (#0),
1822-
},
1823-
],
1824-
span: $DIR/issue-75930-derive-cfg.rs:59:14: 59:31 (#0),
1825-
},
18261644
Ident {
18271645
ident: "i32",
18281646
span: $DIR/issue-75930-derive-cfg.rs:59:32: 59:35 (#0),

‎src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
// The suggestion span should include the attribute.
1010

11-
11+
#[cfg(blandiloquence)]
12+
//~ HELP remove it
1213
//~^ ERROR unused extern crate
1314

1415
fn main() {}

‎src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
// The suggestion span should include the attribute.
1010

11-
#[cfg(blandiloquence)] //~ HELP remove it
12-
extern crate edition_lint_paths;
11+
#[cfg(blandiloquence)]
12+
extern crate edition_lint_paths; //~ HELP remove it
1313
//~^ ERROR unused extern crate
1414

1515
fn main() {}

‎src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error: unused extern crate
22
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:12:1
33
|
4-
LL | / #[cfg(blandiloquence)]
5-
LL | | extern crate edition_lint_paths;
6-
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
7-
| |________________________________|
8-
| help: remove it
4+
LL | extern crate edition_lint_paths;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
96
|
107
note: the lint level is defined here
118
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:6:9

0 commit comments

Comments
 (0)
Please sign in to comment.