about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorLeón Orell Valerian Liehr <me@fmease.dev>2025-02-05 05:03:07 +0100
committerGitHub <noreply@github.com>2025-02-05 05:03:07 +0100
commitc20a58df296efa94960953cb0b197b6babae8dd7 (patch)
tree5847afda8d1c41438a3b73135bfb5806859b2ed0 /tests
parentba420062f1277bcf85810b7a5785fe25d501c9c3 (diff)
parent4636dd9347573e81056f88189ac52ee1e8ccc2f7 (diff)
downloadrust-c20a58df296efa94960953cb0b197b6babae8dd7.tar.gz
rust-c20a58df296efa94960953cb0b197b6babae8dd7.zip
Rollup merge of #136509 - ehuss:nested-macro-rules-edition, r=jieyouxu
Add tests for nested macro_rules edition behavior

This adds tests to check the behavior of how nested macro_rules definitions work across edition boundaries. This covers a change in behavior due to https://github.com/rust-lang/rust/pull/133274.

See https://github.com/rust-lang/rust/issues/135669
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/editions/auxiliary/nested_macro_rules_dep_2021.rs23
-rw-r--r--tests/ui/editions/auxiliary/nested_macro_rules_dep_2024.rs23
-rw-r--r--tests/ui/editions/nested-macro-rules-edition.e2021.stderr27
-rw-r--r--tests/ui/editions/nested-macro-rules-edition.rs39
4 files changed, 112 insertions, 0 deletions
diff --git a/tests/ui/editions/auxiliary/nested_macro_rules_dep_2021.rs b/tests/ui/editions/auxiliary/nested_macro_rules_dep_2021.rs
new file mode 100644
index 00000000000..36d3e712dc0
--- /dev/null
+++ b/tests/ui/editions/auxiliary/nested_macro_rules_dep_2021.rs
@@ -0,0 +1,23 @@
+//@ edition: 2021
+
+#[macro_export]
+macro_rules! make_macro_with_input {
+    ($i:ident) => {
+        macro_rules! macro_inner_input {
+            () => {
+                pub fn $i() {}
+            };
+        }
+    };
+}
+
+#[macro_export]
+macro_rules! make_macro {
+    () => {
+        macro_rules! macro_inner {
+            () => {
+                pub fn gen() {}
+            };
+        }
+    };
+}
diff --git a/tests/ui/editions/auxiliary/nested_macro_rules_dep_2024.rs b/tests/ui/editions/auxiliary/nested_macro_rules_dep_2024.rs
new file mode 100644
index 00000000000..4012398fe66
--- /dev/null
+++ b/tests/ui/editions/auxiliary/nested_macro_rules_dep_2024.rs
@@ -0,0 +1,23 @@
+//@ edition: 2024
+
+#[macro_export]
+macro_rules! make_macro_with_input {
+    ($i:ident) => {
+        macro_rules! macro_inner_input {
+            () => {
+                pub fn $i() {}
+            };
+        }
+    };
+}
+
+#[macro_export]
+macro_rules! make_macro {
+    () => {
+        macro_rules! macro_inner {
+            () => {
+                pub fn gen() {}
+            };
+        }
+    };
+}
diff --git a/tests/ui/editions/nested-macro-rules-edition.e2021.stderr b/tests/ui/editions/nested-macro-rules-edition.e2021.stderr
new file mode 100644
index 00000000000..eac80262364
--- /dev/null
+++ b/tests/ui/editions/nested-macro-rules-edition.e2021.stderr
@@ -0,0 +1,27 @@
+error: expected identifier, found reserved keyword `gen`
+  --> $DIR/nested-macro-rules-edition.rs:30:5
+   |
+LL |     macro_inner_input!{}
+   |     ^^^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
+   |
+   = note: this error originates in the macro `macro_inner_input` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: escape `gen` to use it as an identifier
+   |
+LL |     nested_macro_rules_dep::make_macro_with_input!{r#gen}
+   |                                                    ++
+
+error: expected identifier, found reserved keyword `gen`
+  --> $DIR/nested-macro-rules-edition.rs:35:5
+   |
+LL |     macro_inner!{}
+   |     ^^^^^^^^^^^^^^ expected identifier, found reserved keyword
+   |
+   = note: this error originates in the macro `macro_inner` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: escape `gen` to use it as an identifier
+  --> $DIR/auxiliary/nested_macro_rules_dep_2024.rs:19:24
+   |
+LL |                 pub fn r#gen() {}
+   |                        ++
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/editions/nested-macro-rules-edition.rs b/tests/ui/editions/nested-macro-rules-edition.rs
new file mode 100644
index 00000000000..28d568f7750
--- /dev/null
+++ b/tests/ui/editions/nested-macro-rules-edition.rs
@@ -0,0 +1,39 @@
+// This checks the behavior of how nested macro_rules definitions are handled
+// with regards to edition spans. Prior to https://github.com/rust-lang/rust/pull/133274,
+// the compiler would compile the inner macro with the edition of the local crate.
+// Afterwards, it uses the edition where the macro was *defined*.
+//
+// Unfortunately macro_rules compiler discards the edition of any *input* that
+// was used to generate the macro. This is possibly not the behavior that we
+// want. If we want to keep with the philosophy that code should follow the
+// edition rules of the crate where it is written, then presumably we would
+// want the input tokens to retain the edition of where they were written.
+//
+// See https://github.com/rust-lang/rust/issues/135669 for more.
+//
+// This has two revisions, one where local=2021 and the dep=2024. The other
+// revision is vice-versa.
+
+//@ revisions: e2021 e2024
+//@[e2021] edition:2021
+//@[e2024] edition:2024
+//@[e2021] aux-crate: nested_macro_rules_dep=nested_macro_rules_dep_2024.rs
+//@[e2024] aux-crate: nested_macro_rules_dep=nested_macro_rules_dep_2021.rs
+//@[e2024] check-pass
+
+mod with_input {
+    // If we change the macro_rules input behavior, then this should pass when
+    // local edition is 2021 because `gen` is written in a context with 2021
+    // behavior. For local edition 2024, the reverse would be true and this
+    // should fail.
+    nested_macro_rules_dep::make_macro_with_input!{gen}
+    macro_inner_input!{}
+    //[e2021]~^ ERROR found reserved keyword
+}
+mod no_input {
+    nested_macro_rules_dep::make_macro!{}
+    macro_inner!{}
+    //[e2021]~^ ERROR found reserved keyword
+}
+
+fn main() {}