Skip to content

Commit 83965ef

Browse files
committed
Auto merge of rust-lang#133274 - ehuss:macro_rules-edition-from-pm, r=compiler-errors
Use edition of `macro_rules` when compiling the macro This changes the edition assigned to a macro_rules macro when it is compiled to use the edition of where the macro came from instead of the local crate's edition. This fixes a problem when a macro_rules macro is created by a proc-macro. Previously that macro would be tagged with the local edition, which would cause problems with using the correct edition behavior inside the macro. For example, the check for unsafe attributes would cause errors in 2024 when using proc-macros from older editions. This is partially related to rust-lang#132906. Unfortunately this is only a half fix for that issue. It fixes the error that happens in 2024, but does not fix the lint firing in 2021. I'm still trying to think of some way to fix that, but I'm running low on ideas.
2 parents 48696f5 + 993e084 commit 83965ef

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

compiler/rustc_resolve/src/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
191191
ItemKind::Const(..) => DefKind::Const,
192192
ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
193193
ItemKind::MacroDef(def) => {
194-
let edition = self.resolver.tcx.sess.edition();
194+
let edition = i.span.edition();
195195
let macro_data =
196196
self.resolver.compile_macro(def, i.ident, &i.attrs, i.span, i.id, edition);
197197
let macro_kind = macro_data.ext.macro_kind();
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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() {}

0 commit comments

Comments
 (0)