Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9a6ce9b

Browse filesBrowse files
committed
Auto merge of #133274 - ehuss:macro_rules-edition-from-pm, r=<try>
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 #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 717f5df + 993e084 commit 9a6ce9b
Copy full SHA for 9a6ce9b

File tree

Expand file treeCollapse file tree

5 files changed

+109
-1
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+109
-1
lines changed

‎compiler/rustc_resolve/src/def_collector.rs

Copy file name to clipboardExpand all lines: compiler/rustc_resolve/src/def_collector.rs
+1-1Lines changed: 1 addition & 1 deletion
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();
+42Lines changed: 42 additions & 0 deletions
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+
}
+27Lines changed: 27 additions & 0 deletions
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() {}
+22Lines changed: 22 additions & 0 deletions
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+
}
+17Lines changed: 17 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.