about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAaron Hill <aa1ronham@gmail.com>2021-05-25 22:58:42 -0500
committerAaron Hill <aa1ronham@gmail.com>2021-05-29 13:09:14 -0500
commitd874ecc84f84cb4b3e1675c560759bb5deccf8d2 (patch)
tree3b32cf49c9b5500ff64bb12e3f21baee2e1fb1a4 /src
parentff2c947c00f867b9f012e28ba88cecfbe556f904 (diff)
downloadrust-d874ecc84f84cb4b3e1675c560759bb5deccf8d2.tar.gz
rust-d874ecc84f84cb4b3e1675c560759bb5deccf8d2.zip
Use correct edition when parsing `:pat` matchers
As described in issue #85708, we currently do not properly decode
`SyntaxContext::root()` and `ExpnId::root()` from foreign crates. As a
result, when we decode a span from a foreign crate with
`SyntaxContext::root()`, we end up up considering it to have the edition
of the *current* crate, instead of the foreign crate where it was
originally created.

A full fix for this issue will be a fairly significant undertaking.
Fortunately, it's possible to implement a partial fix, which gives us
the correct edition-dependent behavior for `:pat` matchers when the
macro is loaded from another crate. Since we have the edition of the
macro's defining crate available, we can 'recover' from seeing a
`SyntaxContext::root()` and use the edition of the macro's defining
crate.

Any solution to issue #85708 must reproduce the behavior of this
targeted fix - properly preserving a foreign `SyntaxContext::root()`
means (among other things) preserving its edition, which by definition
is the edition of the foreign crate itself. Therefore, this fix moves us
closer to the correct overall solution, and does not expose any new
incorrect behavior to macros.
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/macros/auxiliary/foreign-crate-macro-pat.rs11
-rw-r--r--src/test/ui/macros/cross-crate-pat-span.rs12
-rw-r--r--src/test/ui/macros/issue-84429-matches-edition.rs9
3 files changed, 32 insertions, 0 deletions
diff --git a/src/test/ui/macros/auxiliary/foreign-crate-macro-pat.rs b/src/test/ui/macros/auxiliary/foreign-crate-macro-pat.rs
new file mode 100644
index 00000000000..26d4c96d524
--- /dev/null
+++ b/src/test/ui/macros/auxiliary/foreign-crate-macro-pat.rs
@@ -0,0 +1,11 @@
+// edition:2018
+
+#[macro_export]
+macro_rules! custom_matches {
+    ($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => {
+        match $expression {
+            $( $pattern )|+ $( if $guard )? => true,
+            _ => false
+        }
+    }
+}
diff --git a/src/test/ui/macros/cross-crate-pat-span.rs b/src/test/ui/macros/cross-crate-pat-span.rs
new file mode 100644
index 00000000000..ed67142ce3d
--- /dev/null
+++ b/src/test/ui/macros/cross-crate-pat-span.rs
@@ -0,0 +1,12 @@
+// edition:2021
+// check-pass
+// aux-build: foreign-crate-macro-pat.rs
+//
+// Tests that the edition of the foreign crate is used
+// when determining the behavior of the `:pat` matcher.
+
+extern crate foreign_crate_macro_pat;
+
+fn main() {
+    let _b = foreign_crate_macro_pat::custom_matches!(b'3', b'0' ..= b'9');
+}
diff --git a/src/test/ui/macros/issue-84429-matches-edition.rs b/src/test/ui/macros/issue-84429-matches-edition.rs
new file mode 100644
index 00000000000..53f134c265f
--- /dev/null
+++ b/src/test/ui/macros/issue-84429-matches-edition.rs
@@ -0,0 +1,9 @@
+// edition:2021
+// check-pass
+//
+// Regression test for issue #84429
+// Tests that we can properly invoke `matches!` from a 2021-edition crate.
+
+fn main() {
+    let _b = matches!(b'3', b'0' ..= b'9');
+}