Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove #[cfg] attributes during cfg-expansion #84110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl<'a> StripUnconfigured<'a> {
pub fn configure<T: AstLike>(&mut self, mut node: T) -> Option<T> {
self.process_cfg_attrs(&mut node);
if self.in_cfg(node.attrs()) {
node.visit_attrs(|attrs| remove_cfg(attrs));
self.try_configure_tokens(&mut node);
Some(node)
} else {
Expand All @@ -267,7 +268,12 @@ impl<'a> StripUnconfigured<'a> {
mut attrs: Vec<ast::Attribute>,
) -> Option<Vec<ast::Attribute>> {
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
if self.in_cfg(&attrs) { Some(attrs) } else { None }
if self.in_cfg(&attrs) {
remove_cfg(&mut attrs);
Some(attrs)
} else {
None
}
}

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

if self.in_cfg(&data.attrs) {
if self.in_cfg(&attrs) {
remove_cfg(&mut attrs);
data.attrs = attrs.into();
data.tokens = LazyTokenStream::new(
self.configure_tokens(&data.tokens.create_token_stream()),
);
Expand Down Expand Up @@ -535,3 +542,7 @@ impl<'a> StripUnconfigured<'a> {
fn is_cfg(sess: &Session, attr: &Attribute) -> bool {
sess.check_name(attr, sym::cfg)
}

fn remove_cfg(attrs: &mut Vec<Attribute>) {
attrs.retain(|a| !a.has_name(sym::cfg));
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ macro_rules! generate_s10 {
($expr: expr) => {
#[cfg(feature = $expr)]
//~^ ERROR expected unsuffixed literal or identifier, found `concat!("nonexistent")`
//~| ERROR expected unsuffixed literal or identifier, found `concat!("nonexistent")`
struct S10;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,7 @@ LL | generate_s10!(concat!("nonexistent"));
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected unsuffixed literal or identifier, found `concat!("nonexistent")`
--> $DIR/cfg-attr-syntax-validation.rs:30:25
|
LL | #[cfg(feature = $expr)]
| ^^^^^
...
LL | generate_s10!(concat!("nonexistent"));
| -------------------------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 11 previous errors
error: aborting due to 10 previous errors

Some errors have detailed explanations: E0537, E0565.
For more information about an error, try `rustc --explain E0537`.
64 changes: 2 additions & 62 deletions src/test/ui/proc-macro/cfg-eval.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-ATTR INPUT (DISPLAY): struct S1 { #[cfg(all())] #[allow()] field_true : u8, }
PRINT-ATTR INPUT (DISPLAY): struct S1 { #[allow()] field_true : u8, }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand All @@ -11,36 +11,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/cfg-eval.rs:20:5: 20:6 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "cfg",
span: $DIR/cfg-eval.rs:20:7: 20:10 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "all",
span: $DIR/cfg-eval.rs:20:11: 20:14 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
span: $DIR/cfg-eval.rs:20:14: 20:24 (#0),
},
],
span: $DIR/cfg-eval.rs:20:10: 20:25 (#0),
},
],
span: $DIR/cfg-eval.rs:20:6: 20:26 (#0),
},
Punct {
ch: '#',
spacing: Alone,
Expand Down Expand Up @@ -83,7 +53,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/cfg-eval.rs:17:11: 24:2 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] (#[cfg(all())] 1,)
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] (1,)
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand All @@ -103,36 +73,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/cfg-eval.rs:36:23: 36:24 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "cfg",
span: $DIR/cfg-eval.rs:36:25: 36:28 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "all",
span: $DIR/cfg-eval.rs:36:29: 36:32 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
span: $DIR/cfg-eval.rs:36:32: 36:42 (#0),
},
],
span: $DIR/cfg-eval.rs:36:28: 36:43 (#0),
},
],
span: $DIR/cfg-eval.rs:36:24: 36:44 (#0),
},
Literal {
kind: Integer,
symbol: "1",
Expand Down
Loading