Skip to content

Commit a183866

Browse files
committed
Add tests for the edition of macro_rules from a proc-macro
1 parent 7899368 commit a183866

7 files changed

+138
-0
lines changed
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,11 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/macro_rules_edition_from_pm.rs:24:5
3+
|
4+
LL | assert!(edition_inner!(const {}) == 2024);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: edition_inner!(const {}) == 2024', $DIR/macro_rules_edition_from_pm.rs:24:5
6+
|
7+
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.

tests/ui/proc-macro/macro_rules_edition_from_pm.edition2024.stderr

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
//@[edition2024] 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 {}) == 2024);
25+
//[edition2021]~^ ERROR evaluation of constant value failed
26+
};
27+
28+
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+
error: unsafe attribute used without unsafe
2+
--> $DIR/unsafe-attributes-from-pm.rs:13:1
3+
|
4+
LL | unsafe_attributes_pm::macro_rules_missing_unsafe!();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ usage of unsafe attribute
6+
...
7+
LL | make_fn!();
8+
| ---------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `make_fn` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: wrap the attribute in `unsafe(...)`
12+
|
13+
LL | ununsafe(safe_attributes_pm::macro_rules_missing_unsafe!());
14+
| +++++++ +
15+
16+
error: aborting due to 1 previous error
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
//@[edition2021] 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+
//[edition2024]~^ ERROR unsafe attribute used without unsafe
15+
16+
make_fn!();
17+
18+
fn main() {}

0 commit comments

Comments
 (0)