From 5dad6c25751f577c53bd36d09f033e1c245d0806 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 6 Mar 2021 21:33:02 +0300 Subject: Implement built-in attribute macro `#[cfg_eval]` --- src/test/ui/proc-macro/cfg-eval-fail.rs | 9 ++ src/test/ui/proc-macro/cfg-eval-fail.stderr | 20 +++++ src/test/ui/proc-macro/cfg-eval.rs | 32 +++++++ src/test/ui/proc-macro/cfg-eval.stdout | 135 ++++++++++++++++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 src/test/ui/proc-macro/cfg-eval-fail.rs create mode 100644 src/test/ui/proc-macro/cfg-eval-fail.stderr create mode 100644 src/test/ui/proc-macro/cfg-eval.rs create mode 100644 src/test/ui/proc-macro/cfg-eval.stdout (limited to 'src') diff --git a/src/test/ui/proc-macro/cfg-eval-fail.rs b/src/test/ui/proc-macro/cfg-eval-fail.rs new file mode 100644 index 00000000000..379491f3126 --- /dev/null +++ b/src/test/ui/proc-macro/cfg-eval-fail.rs @@ -0,0 +1,9 @@ +#![feature(cfg_eval)] +#![feature(stmt_expr_attributes)] + +fn main() { + let _ = #[cfg_eval] #[cfg(FALSE)] 0; + //~^ ERROR removing an expression is not supported in this position + //~| ERROR removing an expression is not supported in this position + //~| ERROR removing an expression is not supported in this position +} diff --git a/src/test/ui/proc-macro/cfg-eval-fail.stderr b/src/test/ui/proc-macro/cfg-eval-fail.stderr new file mode 100644 index 00000000000..010ac006b0b --- /dev/null +++ b/src/test/ui/proc-macro/cfg-eval-fail.stderr @@ -0,0 +1,20 @@ +error: removing an expression is not supported in this position + --> $DIR/cfg-eval-fail.rs:5:25 + | +LL | let _ = #[cfg_eval] #[cfg(FALSE)] 0; + | ^^^^^^^^^^^^^ + +error: removing an expression is not supported in this position + --> $DIR/cfg-eval-fail.rs:5:25 + | +LL | let _ = #[cfg_eval] #[cfg(FALSE)] 0; + | ^^^^^^^^^^^^^ + +error: removing an expression is not supported in this position + --> $DIR/cfg-eval-fail.rs:5:25 + | +LL | let _ = #[cfg_eval] #[cfg(FALSE)] 0; + | ^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + diff --git a/src/test/ui/proc-macro/cfg-eval.rs b/src/test/ui/proc-macro/cfg-eval.rs new file mode 100644 index 00000000000..ea397df5452 --- /dev/null +++ b/src/test/ui/proc-macro/cfg-eval.rs @@ -0,0 +1,32 @@ +// check-pass +// compile-flags: -Z span-debug +// aux-build:test-macros.rs + +#![feature(cfg_eval)] +#![feature(proc_macro_hygiene)] +#![feature(stmt_expr_attributes)] + +#![no_std] // Don't load unnecessary hygiene information from std +extern crate std; + +#[macro_use] +extern crate test_macros; + +#[cfg_eval] +#[print_attr] +struct S1 { + #[cfg(FALSE)] + field_false: u8, + #[cfg(all(/*true*/))] + #[cfg_attr(FALSE, unknown_attr)] + #[cfg_attr(all(/*true*/), allow())] + field_true: u8, +} + +#[cfg_eval] +#[cfg(FALSE)] +struct S2 {} + +fn main() { + let _ = #[cfg_eval] #[print_attr](#[cfg(FALSE)] 0, #[cfg(all(/*true*/))] 1); +} diff --git a/src/test/ui/proc-macro/cfg-eval.stdout b/src/test/ui/proc-macro/cfg-eval.stdout new file mode 100644 index 00000000000..b98e8961bfe --- /dev/null +++ b/src/test/ui/proc-macro/cfg-eval.stdout @@ -0,0 +1,135 @@ +PRINT-ATTR INPUT (DISPLAY): struct S1 { #[cfg(all())] #[allow()] field_true : u8, } +PRINT-ATTR INPUT (DEBUG): TokenStream [ + Ident { + ident: "struct", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Ident { + ident: "S1", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Group { + delimiter: Brace, + stream: TokenStream [ + Punct { + ch: '#', + spacing: Alone, + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Group { + delimiter: Bracket, + stream: TokenStream [ + Ident { + ident: "cfg", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Group { + delimiter: Parenthesis, + stream: TokenStream [ + Ident { + ident: "all", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Group { + delimiter: Parenthesis, + stream: TokenStream [], + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + ], + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + ], + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Punct { + ch: '#', + spacing: Alone, + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Group { + delimiter: Bracket, + stream: TokenStream [ + Ident { + ident: "allow", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Group { + delimiter: Parenthesis, + stream: TokenStream [], + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + ], + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Ident { + ident: "field_true", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Punct { + ch: ':', + spacing: Alone, + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Ident { + ident: "u8", + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + Punct { + ch: ',', + spacing: Alone, + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, + ], + span: $DIR/cfg-eval.rs:17:1: 24:2 (#0), + }, +] +PRINT-ATTR INPUT (DISPLAY): (#[cfg(all())] 1,) +PRINT-ATTR INPUT (DEBUG): TokenStream [ + Group { + delimiter: Parenthesis, + stream: TokenStream [ + Punct { + ch: '#', + spacing: Alone, + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + Group { + delimiter: Bracket, + stream: TokenStream [ + Ident { + ident: "cfg", + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + Group { + delimiter: Parenthesis, + stream: TokenStream [ + Ident { + ident: "all", + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + Group { + delimiter: Parenthesis, + stream: TokenStream [], + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + ], + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + ], + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + Literal { + kind: Integer, + symbol: "1", + suffix: None, + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + Punct { + ch: ',', + spacing: Alone, + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, + ], + span: $DIR/cfg-eval.rs:31:38: 31:80 (#0), + }, +] -- cgit 1.4.1-3-g733a5