From a1838660c3820c74b987a1630c405ed5c575c70a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 20 Nov 2024 16:36:18 -0800 Subject: [PATCH 1/2] Add tests for the edition of macro_rules from a proc-macro --- .../auxiliary/macro_rules_edition_pm.rs | 42 +++++++++++++++++++ ...o_rules_edition_from_pm.edition2021.stderr | 11 +++++ ...o_rules_edition_from_pm.edition2024.stderr | 0 .../proc-macro/macro_rules_edition_from_pm.rs | 28 +++++++++++++ .../auxiliary/unsafe-attributes-pm.rs | 22 ++++++++++ ...safe-attributes-from-pm.edition2024.stderr | 17 ++++++++ .../unsafe-attributes-from-pm.rs | 18 ++++++++ 7 files changed, 138 insertions(+) create mode 100644 tests/ui/proc-macro/auxiliary/macro_rules_edition_pm.rs create mode 100644 tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr create mode 100644 tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr create mode 100644 tests/ui/proc-macro/macro_rules_edition_from_pm.rs create mode 100644 tests/ui/rust-2024/unsafe-attributes/auxiliary/unsafe-attributes-pm.rs create mode 100644 tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr create mode 100644 tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs diff --git a/tests/ui/proc-macro/auxiliary/macro_rules_edition_pm.rs b/tests/ui/proc-macro/auxiliary/macro_rules_edition_pm.rs new file mode 100644 index 0000000000000..a4fd76b9c9d33 --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/macro_rules_edition_pm.rs @@ -0,0 +1,42 @@ +//@ force-host +//@ no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro] +pub fn make_edition_macro(_input: TokenStream) -> TokenStream { + "macro_rules! edition { + ($_:expr) => { + 2024 + }; + (const {}) => { + 2021 + }; + } + " + .parse() + .unwrap() +} + +#[proc_macro] +pub fn make_nested_edition_macro(_input: TokenStream) -> TokenStream { + "macro_rules! make_inner { + () => { + macro_rules! edition_inner { + ($_:expr) => { + 2024 + }; + (const {}) => { + 2021 + }; + } + }; + } + " + .parse() + .unwrap() +} diff --git a/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr b/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr new file mode 100644 index 0000000000000..a783dac00cd49 --- /dev/null +++ b/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr @@ -0,0 +1,11 @@ +error[E0080]: evaluation of constant value failed + --> $DIR/macro_rules_edition_from_pm.rs:24:5 + | +LL | assert!(edition_inner!(const {}) == 2024); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: edition_inner!(const {}) == 2024', $DIR/macro_rules_edition_from_pm.rs:24:5 + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr b/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tests/ui/proc-macro/macro_rules_edition_from_pm.rs b/tests/ui/proc-macro/macro_rules_edition_from_pm.rs new file mode 100644 index 0000000000000..de614922d1fc7 --- /dev/null +++ b/tests/ui/proc-macro/macro_rules_edition_from_pm.rs @@ -0,0 +1,28 @@ +// Tests how edition hygiene works for macro_rules macros generated from a +// proc-macro. +// See https://github.com/rust-lang/rust/issues/132906 + +//@ aux-crate: macro_rules_edition_pm=macro_rules_edition_pm.rs +//@ revisions: edition2021 edition2024 +//@[edition2021] edition:2021 +//@[edition2024] edition:2024 +//@[edition2024] compile-flags: -Zunstable-options +//@[edition2024] check-pass + +// This checks how the expr fragment specifier works. +macro_rules_edition_pm::make_edition_macro!{} + +const _: () = { + assert!(edition!(const {}) == 2021); +}; + +// This checks how the expr fragment specifier from a nested macro. +macro_rules_edition_pm::make_nested_edition_macro!{} +make_inner!{} + +const _: () = { + assert!(edition_inner!(const {}) == 2024); + //[edition2021]~^ ERROR evaluation of constant value failed +}; + +fn main() {} diff --git a/tests/ui/rust-2024/unsafe-attributes/auxiliary/unsafe-attributes-pm.rs b/tests/ui/rust-2024/unsafe-attributes/auxiliary/unsafe-attributes-pm.rs new file mode 100644 index 0000000000000..557731d82d38e --- /dev/null +++ b/tests/ui/rust-2024/unsafe-attributes/auxiliary/unsafe-attributes-pm.rs @@ -0,0 +1,22 @@ +//@ force-host +//@ no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro] +pub fn missing_unsafe(_input: TokenStream) -> TokenStream { + "#[no_mangle] pub fn abc() {}".parse().unwrap() +} + +#[proc_macro] +pub fn macro_rules_missing_unsafe(_input: TokenStream) -> TokenStream { + "macro_rules! make_fn { + () => { #[no_mangle] pub fn foo() { } }; + }" + .parse() + .unwrap() +} diff --git a/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr new file mode 100644 index 0000000000000..4bdfe6153e7c7 --- /dev/null +++ b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr @@ -0,0 +1,17 @@ +error: unsafe attribute used without unsafe + --> $DIR/unsafe-attributes-from-pm.rs:13:1 + | +LL | unsafe_attributes_pm::macro_rules_missing_unsafe!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ usage of unsafe attribute +... +LL | make_fn!(); + | ---------- in this macro invocation + | + = note: this error originates in the macro `make_fn` (in Nightly builds, run with -Z macro-backtrace for more info) +help: wrap the attribute in `unsafe(...)` + | +LL | ununsafe(safe_attributes_pm::macro_rules_missing_unsafe!()); + | +++++++ + + +error: aborting due to 1 previous error + diff --git a/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs new file mode 100644 index 0000000000000..fbe3ffaf0b8c6 --- /dev/null +++ b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs @@ -0,0 +1,18 @@ +// Test for unsafe attributes generated by a proc-macro. +// See https://github.com/rust-lang/rust/issues/132906 + +//@ revisions: edition2021 edition2024 +//@[edition2021] check-pass +//@[edition2021] edition:2021 +//@[edition2024] edition:2024 +//@[edition2024] compile-flags: -Zunstable-options +//@ aux-crate: unsafe_attributes_pm=unsafe-attributes-pm.rs + +unsafe_attributes_pm::missing_unsafe!(); + +unsafe_attributes_pm::macro_rules_missing_unsafe!(); +//[edition2024]~^ ERROR unsafe attribute used without unsafe + +make_fn!(); + +fn main() {} From 993e084eb1eb8bc29eae0620bbbb2c3009cec6dd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 20 Nov 2024 17:12:56 -0800 Subject: [PATCH 2/2] Use edition of `macro_rules` when compiling the macro --- compiler/rustc_resolve/src/def_collector.rs | 2 +- ...cro_rules_edition_from_pm.edition2021.stderr | 11 ----------- ...cro_rules_edition_from_pm.edition2024.stderr | 0 .../proc-macro/macro_rules_edition_from_pm.rs | 5 ++--- ...unsafe-attributes-from-pm.edition2024.stderr | 17 ----------------- .../unsafe-attributes-from-pm.rs | 3 +-- 6 files changed, 4 insertions(+), 34 deletions(-) delete mode 100644 tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr delete mode 100644 tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr delete mode 100644 tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index bf27b767a4972..7536869e2fe25 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -191,7 +191,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { ItemKind::Const(..) => DefKind::Const, ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn, ItemKind::MacroDef(def) => { - let edition = self.resolver.tcx.sess.edition(); + let edition = i.span.edition(); let macro_data = self.resolver.compile_macro(def, i.ident, &i.attrs, i.span, i.id, edition); let macro_kind = macro_data.ext.macro_kind(); diff --git a/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr b/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr deleted file mode 100644 index a783dac00cd49..0000000000000 --- a/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2021.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error[E0080]: evaluation of constant value failed - --> $DIR/macro_rules_edition_from_pm.rs:24:5 - | -LL | assert!(edition_inner!(const {}) == 2024); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: edition_inner!(const {}) == 2024', $DIR/macro_rules_edition_from_pm.rs:24:5 - | - = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr b/tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr deleted file mode 100644 index e69de29bb2d1d..0000000000000 diff --git a/tests/ui/proc-macro/macro_rules_edition_from_pm.rs b/tests/ui/proc-macro/macro_rules_edition_from_pm.rs index de614922d1fc7..3ba80f5177a91 100644 --- a/tests/ui/proc-macro/macro_rules_edition_from_pm.rs +++ b/tests/ui/proc-macro/macro_rules_edition_from_pm.rs @@ -7,7 +7,7 @@ //@[edition2021] edition:2021 //@[edition2024] edition:2024 //@[edition2024] compile-flags: -Zunstable-options -//@[edition2024] check-pass +//@ check-pass // This checks how the expr fragment specifier works. macro_rules_edition_pm::make_edition_macro!{} @@ -21,8 +21,7 @@ macro_rules_edition_pm::make_nested_edition_macro!{} make_inner!{} const _: () = { - assert!(edition_inner!(const {}) == 2024); - //[edition2021]~^ ERROR evaluation of constant value failed + assert!(edition_inner!(const {}) == 2021); }; fn main() {} diff --git a/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr deleted file mode 100644 index 4bdfe6153e7c7..0000000000000 --- a/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.edition2024.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error: unsafe attribute used without unsafe - --> $DIR/unsafe-attributes-from-pm.rs:13:1 - | -LL | unsafe_attributes_pm::macro_rules_missing_unsafe!(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ usage of unsafe attribute -... -LL | make_fn!(); - | ---------- in this macro invocation - | - = note: this error originates in the macro `make_fn` (in Nightly builds, run with -Z macro-backtrace for more info) -help: wrap the attribute in `unsafe(...)` - | -LL | ununsafe(safe_attributes_pm::macro_rules_missing_unsafe!()); - | +++++++ + - -error: aborting due to 1 previous error - diff --git a/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs index fbe3ffaf0b8c6..782a39422362b 100644 --- a/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs +++ b/tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs @@ -2,7 +2,7 @@ // See https://github.com/rust-lang/rust/issues/132906 //@ revisions: edition2021 edition2024 -//@[edition2021] check-pass +//@ check-pass //@[edition2021] edition:2021 //@[edition2024] edition:2024 //@[edition2024] compile-flags: -Zunstable-options @@ -11,7 +11,6 @@ unsafe_attributes_pm::missing_unsafe!(); unsafe_attributes_pm::macro_rules_missing_unsafe!(); -//[edition2024]~^ ERROR unsafe attribute used without unsafe make_fn!();