Skip to content

Commit

Permalink
Run cfg-stripping on generic parameters before invoking derive macros
Browse files Browse the repository at this point in the history
Fixes #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.
  • Loading branch information
Aaron1011 committed Aug 30, 2020
1 parent 85fbf49 commit a97dcfa
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 4 deletions.
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),
},
]

0 comments on commit a97dcfa

Please sign in to comment.