From 5ebcd1913c0f37e657e78e483fbaf147db1d9d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 10 Dec 2024 22:01:43 +0000 Subject: [PATCH] Add test for rustc-cfg-placeholder interaction with proc-macro Ensure that the cfg-placeholder isn't visible by proc-macros. --- .../proc-macro/auxiliary/cfg-placeholder.rs | 28 +++++++++++++++++++ ...ttr-placeholder-invisible-to-proc-macro.rs | 13 +++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/ui/proc-macro/auxiliary/cfg-placeholder.rs create mode 100644 tests/ui/proc-macro/cfg-attr-placeholder-invisible-to-proc-macro.rs diff --git a/tests/ui/proc-macro/auxiliary/cfg-placeholder.rs b/tests/ui/proc-macro/auxiliary/cfg-placeholder.rs new file mode 100644 index 0000000000000..f28dff50e2f6b --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/cfg-placeholder.rs @@ -0,0 +1,28 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn my_proc_macro(_: TokenStream, input: TokenStream) -> TokenStream { + if format!("{input:#?}").contains("my_attr1") { + panic!("found gated attribute my_attr1"); + } + if !format!("{input:#?}").contains("my_attr2") { + panic!("didn't if gated my_attr2"); + } + input +} + +#[proc_macro_attribute] +pub fn my_attr1(_: TokenStream, input: TokenStream) -> TokenStream { + panic!("my_attr1 was called"); + input +} + +#[proc_macro_attribute] +pub fn my_attr2(_: TokenStream, input: TokenStream) -> TokenStream { + if format!("{input:#?}").contains("my_attr1") { + panic!("found gated attribute my_attr1"); + } + input +} diff --git a/tests/ui/proc-macro/cfg-attr-placeholder-invisible-to-proc-macro.rs b/tests/ui/proc-macro/cfg-attr-placeholder-invisible-to-proc-macro.rs new file mode 100644 index 0000000000000..62cac78788367 --- /dev/null +++ b/tests/ui/proc-macro/cfg-attr-placeholder-invisible-to-proc-macro.rs @@ -0,0 +1,13 @@ +// Ensure that `rustc-cfg-placeholder` isn't visible to proc-macros. +//@proc-macro: cfg-placeholder.rs +//@check-pass +#![feature(cfg_eval)] +#[macro_use] extern crate cfg_placeholder; + +#[cfg_eval] +#[my_proc_macro] +#[cfg_attr(FALSE, my_attr1)] +#[cfg_attr(all(), my_attr2)] +struct S {} + +fn main() {}