about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-09-22 00:31:03 +0000
committerbors <bors@rust-lang.org>2024-09-22 00:31:03 +0000
commit55043f067dcf7067e7c6ebccf3639af94ff57bda (patch)
treeb68c490fe50e1087af0df4daf9674e9bc1eec23c /tests
parent764e6aec81517cde60214ccd00a709a34eb0c07d (diff)
parent781ec111b728a4d1ae513ae57d5eecf27680a216 (diff)
downloadrust-55043f067dcf7067e7c6ebccf3639af94ff57bda.tar.gz
rust-55043f067dcf7067e7c6ebccf3639af94ff57bda.zip
Auto merge of #130337 - BoxyUwU:anon_const_macro_call, r=camelid
Fix anon const def-creation when macros are involved take 2

Fixes #130321

There were two cases that #129137 did not handle correctly:

- Given a const argument `Foo<{ bar!() }>` in which `bar!()` expands to `N`, we would visit the anon const and then visit the `{ bar() }` expression instead of visiting the macro call. This meant that we would build a def for the anon const as `{ bar!() }` is not a trivial const argument as `bar!()` is not a path.
- Given a const argument `Foo<{ bar!() }>` is which `bar!()` expands to `{ qux!() }` in which `qux!()` expands to `N`, it should not be considered a trivial const argument as `{{ N }}` has two pairs of braces.  If we only looked at `qux`'s expansion it would *look* like a trivial const argument even though it is not. We have to track whether we have "unwrapped" a brace already when recursing into the expansions of `bar`/`qux`/any macro

r? `@camelid`
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.rs14
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.stderr14
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.rs15
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.stderr15
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.rs15
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.stderr15
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-nested-braces.rs9
-rw-r--r--tests/ui/const-generics/early/trivial-const-arg-nested-braces.stderr11
8 files changed, 108 insertions, 0 deletions
diff --git a/tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.rs b/tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.rs
new file mode 100644
index 00000000000..33630205369
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.rs
@@ -0,0 +1,14 @@
+macro_rules! y {
+    () => {
+        N
+    };
+}
+
+struct A<const N: usize>;
+
+fn foo<const N: usize>() -> A<{ y!() }> {
+    A::<1>
+    //~^ ERROR: mismatched types
+}
+
+fn main() {}
diff --git a/tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.stderr b/tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.stderr
new file mode 100644
index 00000000000..4461477f3e9
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-macro-braced-expansion.stderr
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+  --> $DIR/trivial-const-arg-macro-braced-expansion.rs:10:5
+   |
+LL | fn foo<const N: usize>() -> A<{ y!() }> {
+   |                             ----------- expected `A<N>` because of return type
+LL |     A::<1>
+   |     ^^^^^^ expected `N`, found `1`
+   |
+   = note: expected struct `A<N>`
+              found struct `A<1>`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.rs b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.rs
new file mode 100644
index 00000000000..5a9e62561dc
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.rs
@@ -0,0 +1,15 @@
+macro_rules! y {
+    () => {
+        N
+        //~^ ERROR: generic parameters may not be used in const operations
+    };
+}
+
+struct A<const N: usize>;
+
+#[rustfmt::skip]
+fn foo<const N: usize>() -> A<{{ y!() }}> {
+    A::<1>
+}
+
+fn main() {}
diff --git a/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.stderr b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.stderr
new file mode 100644
index 00000000000..e40d05924b1
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces-2.stderr
@@ -0,0 +1,15 @@
+error: generic parameters may not be used in const operations
+  --> $DIR/trivial-const-arg-macro-nested-braces-2.rs:3:9
+   |
+LL |         N
+   |         ^ cannot perform const operation using `N`
+...
+LL | fn foo<const N: usize>() -> A<{{ y!() }}> {
+   |                                  ---- in this macro invocation
+   |
+   = help: const parameters may only be used as standalone arguments, i.e. `N`
+   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
+   = note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.rs b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.rs
new file mode 100644
index 00000000000..45c0768dde4
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.rs
@@ -0,0 +1,15 @@
+#[rustfmt::skip]
+macro_rules! y {
+    () => {
+        { N }
+        //~^ ERROR: generic parameters may not be used in const operations
+    };
+}
+
+struct A<const N: usize>;
+
+fn foo<const N: usize>() -> A<{ y!() }> {
+    A::<1>
+}
+
+fn main() {}
diff --git a/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.stderr b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.stderr
new file mode 100644
index 00000000000..b91d6c7a024
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-macro-nested-braces.stderr
@@ -0,0 +1,15 @@
+error: generic parameters may not be used in const operations
+  --> $DIR/trivial-const-arg-macro-nested-braces.rs:4:11
+   |
+LL |         { N }
+   |           ^ cannot perform const operation using `N`
+...
+LL | fn foo<const N: usize>() -> A<{ y!() }> {
+   |                                 ---- in this macro invocation
+   |
+   = help: const parameters may only be used as standalone arguments, i.e. `N`
+   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
+   = note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/const-generics/early/trivial-const-arg-nested-braces.rs b/tests/ui/const-generics/early/trivial-const-arg-nested-braces.rs
new file mode 100644
index 00000000000..941ba6bfea7
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-nested-braces.rs
@@ -0,0 +1,9 @@
+struct A<const N: usize>;
+
+#[rustfmt::skip]
+fn foo<const N: usize>() -> A<{ { N } }> {
+    //~^ ERROR: generic parameters may not be used in const operations
+    A::<1>
+}
+
+fn main() {}
diff --git a/tests/ui/const-generics/early/trivial-const-arg-nested-braces.stderr b/tests/ui/const-generics/early/trivial-const-arg-nested-braces.stderr
new file mode 100644
index 00000000000..d60516ba4bc
--- /dev/null
+++ b/tests/ui/const-generics/early/trivial-const-arg-nested-braces.stderr
@@ -0,0 +1,11 @@
+error: generic parameters may not be used in const operations
+  --> $DIR/trivial-const-arg-nested-braces.rs:4:35
+   |
+LL | fn foo<const N: usize>() -> A<{ { N } }> {
+   |                                   ^ cannot perform const operation using `N`
+   |
+   = help: const parameters may only be used as standalone arguments, i.e. `N`
+   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
+
+error: aborting due to 1 previous error
+