Skip to content

Run cfg-stripping on generic parameters before invoking derive macros #76010

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

Merged
merged 1 commit into from
Sep 1, 2020
Merged
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
11 changes: 7 additions & 4 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,6 @@ impl<'a> StripUnconfigured<'a> {
items.flat_map_in_place(|item| self.configure(item));
}

pub fn configure_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
params.flat_map_in_place(|param| self.configure(param));
}

fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
match vdata {
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) => {
Expand Down Expand Up @@ -496,6 +492,13 @@ impl<'a> MutVisitor for StripUnconfigured<'a> {
Some(expr)
}

fn flat_map_generic_param(
&mut self,
param: ast::GenericParam,
) -> SmallVec<[ast::GenericParam; 1]> {
noop_flat_map_generic_param(configure!(self, param), self)
}

fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
noop_flat_map_stmt(configure!(self, stmt), self)
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/proc-macro/issue-75930-derive-cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// check-pass
// compile-flags: -Z span-debug
// aux-build:test-macros.rs

// Regression test for issue #75930
// Tests that we cfg-strip all targets before invoking
// a derive macro

#[macro_use]
extern crate test_macros;

#[derive(Print)]
struct Foo<#[cfg(FALSE)] A, B> {
#[cfg(FALSE)] first: String,
second: bool,
third: [u8; {
#[cfg(FALSE)] struct Bar;
#[cfg(not(FALSE))] struct Inner;
#[cfg(FALSE)] let a = 25;
match true {
#[cfg(FALSE)] true => {},
false => {},
_ => {}
};
0
}],
fourth: B
}

fn main() {}
221 changes: 221 additions & 0 deletions src/test/ui/proc-macro/issue-75930-derive-cfg.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
PRINT-DERIVE INPUT (DISPLAY): struct Foo < B >
{
second : bool, third :
[u8 ;
{
#[cfg(not(FALSE))] struct Inner ; match true
{ false => { } _ => { } } ; 0
}], fourth : B,
}
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "Foo",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "B",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "second",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "bool",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "third",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "u8",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "cfg",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "not",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "FALSE",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "struct",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "Inner",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "match",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "true",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "false",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: '=',
spacing: Joint,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "_",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: '=',
spacing: Joint,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "fourth",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Ident {
ident: "B",
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:1:1: 1:1 (#0),
},
]