Auto merge of #133274 - ehuss:macro_rules-edition-from-pm, r= · rust-lang/rust@9a6ce9b (original) (raw)

File tree

5 files changed

lines changed

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -191,7 +191,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
191 191 ItemKind::Const(..) => DefKind::Const,
192 192 ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
193 193 ItemKind::MacroDef(def) => {
194 -let edition = self.resolver.tcx.sess.edition();
194 +let edition = i.span.edition();
195 195 let macro_data =
196 196 self.resolver.compile_macro(def, i.ident, &i.attrs, i.span, i.id, edition);
197 197 let macro_kind = macro_data.ext.macro_kind();
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
1 +//@ force-host
2 +//@ no-prefer-dynamic
3 +
4 +#![crate_type = "proc-macro"]
5 +
6 +extern crate proc_macro;
7 +
8 +use proc_macro::TokenStream;
9 +
10 +#[proc_macro]
11 +pub fn make_edition_macro(_input: TokenStream) -> TokenStream {
12 +"macro_rules! edition {
13 + ($_:expr) => {
14 + 2024
15 + };
16 + (const {}) => {
17 + 2021
18 + };
19 + }
20 + "
21 +.parse()
22 +.unwrap()
23 +}
24 +
25 +#[proc_macro]
26 +pub fn make_nested_edition_macro(_input: TokenStream) -> TokenStream {
27 +"macro_rules! make_inner {
28 + () => {
29 + macro_rules! edition_inner {
30 + ($_:expr) => {
31 + 2024
32 + };
33 + (const {}) => {
34 + 2021
35 + };
36 + }
37 + };
38 + }
39 + "
40 +.parse()
41 +.unwrap()
42 +}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
1 +// Tests how edition hygiene works for macro_rules macros generated from a
2 +// proc-macro.
3 +// See https://github.com/rust-lang/rust/issues/132906
4 +
5 +//@ aux-crate: macro_rules_edition_pm=macro_rules_edition_pm.rs
6 +//@ revisions: edition2021 edition2024
7 +//@[edition2021] edition:2021
8 +//@[edition2024] edition:2024
9 +//@[edition2024] compile-flags: -Zunstable-options
10 +//@ check-pass
11 +
12 +// This checks how the expr fragment specifier works.
13 +macro_rules_edition_pm::make_edition_macro!{}
14 +
15 +const _: () = {
16 +assert!(edition!(const {}) == 2021);
17 +};
18 +
19 +// This checks how the expr fragment specifier from a nested macro.
20 +macro_rules_edition_pm::make_nested_edition_macro!{}
21 +make_inner!{}
22 +
23 +const _: () = {
24 +assert!(edition_inner!(const {}) == 2021);
25 +};
26 +
27 +fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1 +//@ force-host
2 +//@ no-prefer-dynamic
3 +
4 +#![crate_type = "proc-macro"]
5 +
6 +extern crate proc_macro;
7 +
8 +use proc_macro::TokenStream;
9 +
10 +#[proc_macro]
11 +pub fn missing_unsafe(_input: TokenStream) -> TokenStream {
12 +"#[no_mangle] pub fn abc() {}".parse().unwrap()
13 +}
14 +
15 +#[proc_macro]
16 +pub fn macro_rules_missing_unsafe(_input: TokenStream) -> TokenStream {
17 +"macro_rules! make_fn {
18 + () => { #[no_mangle] pub fn foo() { } };
19 + }"
20 +.parse()
21 +.unwrap()
22 +}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
1 +// Test for unsafe attributes generated by a proc-macro.
2 +// See https://github.com/rust-lang/rust/issues/132906
3 +
4 +//@ revisions: edition2021 edition2024
5 +//@ check-pass
6 +//@[edition2021] edition:2021
7 +//@[edition2024] edition:2024
8 +//@[edition2024] compile-flags: -Zunstable-options
9 +//@ aux-crate: unsafe_attributes_pm=unsafe-attributes-pm.rs
10 +
11 +unsafe_attributes_pm::missing_unsafe!();
12 +
13 +unsafe_attributes_pm::macro_rules_missing_unsafe!();
14 +
15 +make_fn!();
16 +
17 +fn main() {}