Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use edition of macro_rules when compiling the macro #133274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/proc-macro/auxiliary/macro_rules_edition_pm.rs
Original file line number Diff line number Diff line change
@@ -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()
}
27 changes: 27 additions & 0 deletions tests/ui/proc-macro/macro_rules_edition_from_pm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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
//@ 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 {}) == 2021);
};

fn main() {}
Original file line number Diff line number Diff line change
@@ -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()
}
17 changes: 17 additions & 0 deletions tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-from-pm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Test for unsafe attributes generated by a proc-macro.
// See https://github.com/rust-lang/rust/issues/132906

//@ revisions: edition2021 edition2024
//@ 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!();

make_fn!();

fn main() {}
Loading