about summary refs log tree commit diff
path: root/tests/ui
diff options
context:
space:
mode:
authorTrevor Gross <t.gross35@gmail.com>2025-07-10 03:23:53 -0400
committerGitHub <noreply@github.com>2025-07-10 03:23:53 -0400
commit643efdaa823b21e0423c6614f74a8b014c67520b (patch)
tree4f6158fd60dcfc3957916b2b425aedf35a7b729e /tests/ui
parent73d3adc67b700ab3a41c72791f0ca5cbaef51425 (diff)
parent87e981996fe47826a8f3a69e3d3cc73ec68dbf8d (diff)
downloadrust-643efdaa823b21e0423c6614f74a8b014c67520b.tar.gz
rust-643efdaa823b21e0423c6614f74a8b014c67520b.zip
Rollup merge of #142950 - tgross35:metavariable-expr-rework, r=petrochenkov
mbe: Rework diagnostics for metavariable expressions

Make the diagnostics for metavariable expressions more user-friendly. This mostly addresses syntactic errors; I will be following up with improvements to `concat(..)`.
Diffstat (limited to 'tests/ui')
-rw-r--r--tests/ui/macros/metavar-expressions/syntax-errors.rs33
-rw-r--r--tests/ui/macros/metavar-expressions/syntax-errors.stderr192
2 files changed, 115 insertions, 110 deletions
diff --git a/tests/ui/macros/metavar-expressions/syntax-errors.rs b/tests/ui/macros/metavar-expressions/syntax-errors.rs
index 8fc76a74baa..585ea4d5979 100644
--- a/tests/ui/macros/metavar-expressions/syntax-errors.rs
+++ b/tests/ui/macros/metavar-expressions/syntax-errors.rs
@@ -30,7 +30,7 @@ macro_rules! metavar_with_literal_suffix {
 
 macro_rules! mve_without_parens {
     ( $( $i:ident ),* ) => { ${ count } };
-    //~^ ERROR meta-variable expression parameter must be wrapped in parentheses
+    //~^ ERROR expected `(`
 }
 
 #[rustfmt::skip]
@@ -45,9 +45,14 @@ macro_rules! open_brackets_with_lit {
      //~^ ERROR expected identifier
  }
 
+macro_rules! mvs_missing_paren {
+    ( $( $i:ident ),* ) => { ${ count $i ($i) } };
+    //~^ ERROR expected `(`
+}
+
 macro_rules! mve_wrong_delim {
     ( $( $i:ident ),* ) => { ${ count{i} } };
-    //~^ ERROR meta-variable expression parameter must be wrapped in parentheses
+    //~^ ERROR expected `(`
 }
 
 macro_rules! invalid_metavar {
@@ -64,28 +69,30 @@ macro_rules! open_brackets_with_group {
 macro_rules! extra_garbage_after_metavar {
     ( $( $i:ident ),* ) => {
         ${count() a b c}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
         ${count($i a b c)}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
         ${count($i, 1 a b c)}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
         ${count($i) a b c}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
 
         ${ignore($i) a b c}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
         ${ignore($i a b c)}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
 
         ${index() a b c}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
         ${index(1 a b c)}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
 
         ${index() a b c}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
         ${index(1 a b c)}
-        //~^ ERROR unexpected token: a
+        //~^ ERROR unexpected trailing tokens
+        ${index(1, a b c)}
+        //~^ ERROR unexpected trailing tokens
     };
 }
 
@@ -111,7 +118,7 @@ macro_rules! unknown_ignore_ident {
 
 macro_rules! unknown_metavar {
     ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
-    //~^ ERROR unrecognized meta-variable expression
+    //~^ ERROR unrecognized metavariable expression
 }
 
 fn main() {}
diff --git a/tests/ui/macros/metavar-expressions/syntax-errors.stderr b/tests/ui/macros/metavar-expressions/syntax-errors.stderr
index 20d2358facc..bf1c7673a6c 100644
--- a/tests/ui/macros/metavar-expressions/syntax-errors.stderr
+++ b/tests/ui/macros/metavar-expressions/syntax-errors.stderr
@@ -40,167 +40,165 @@ error: only unsuffixes integer literals are supported in meta-variable expressio
 LL |     ( $( $i:ident ),* ) => { ${ index(1u32) } };
    |                                 ^^^^^
 
-error: meta-variable expression parameter must be wrapped in parentheses
+error: expected `(`
   --> $DIR/syntax-errors.rs:32:33
    |
 LL |     ( $( $i:ident ),* ) => { ${ count } };
-   |                                 ^^^^^
+   |                                 ^^^^^- help: try adding parentheses: `( /* ... */ )`
+   |                                 |
+   |                                 for this this metavariable expression
+   |
+   = note: metavariable expressions use function-like parentheses syntax
 
-error: meta-variable expression parameter must be wrapped in parentheses
+error: expected `(`
   --> $DIR/syntax-errors.rs:49:33
    |
+LL |     ( $( $i:ident ),* ) => { ${ count $i ($i) } };
+   |                                 ^^^^^ - unexpected token
+   |                                 |
+   |                                 for this this metavariable expression
+   |
+   = note: metavariable expressions use function-like parentheses syntax
+
+error: expected `(`
+  --> $DIR/syntax-errors.rs:54:33
+   |
 LL |     ( $( $i:ident ),* ) => { ${ count{i} } };
-   |                                 ^^^^^
+   |                                 ^^^^^ for this this metavariable expression
+   |
+   = note: metavariable expressions use function-like parentheses syntax
 
 error: expected identifier, found `123`
-  --> $DIR/syntax-errors.rs:54:23
+  --> $DIR/syntax-errors.rs:59:23
    |
 LL |     () => { ${ignore($123)} }
    |                       ^^^ help: try removing `123`
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:66:19
-   |
-LL |         ${count() a b c}
-   |                   ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:66:19
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:71:19
    |
 LL |         ${count() a b c}
-   |                   ^
+   |           -----   ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:68:20
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:73:20
    |
 LL |         ${count($i a b c)}
-   |                    ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:68:20
+   |           -----    ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
    |
-LL |         ${count($i a b c)}
-   |                    ^
+   = note: the `count` metavariable expression takes between 1 and 2 arguments
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:70:23
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:75:23
    |
 LL |         ${count($i, 1 a b c)}
-   |                       ^
+   |           -----       ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
    |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:70:23
-   |
-LL |         ${count($i, 1 a b c)}
-   |                       ^
+   = note: the `count` metavariable expression takes between 1 and 2 arguments
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:72:21
-   |
-LL |         ${count($i) a b c}
-   |                     ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:72:21
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:77:21
    |
 LL |         ${count($i) a b c}
-   |                     ^
+   |           -----     ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:75:22
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:80:22
    |
 LL |         ${ignore($i) a b c}
-   |                      ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:75:22
-   |
-LL |         ${ignore($i) a b c}
-   |                      ^
+   |           ------     ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:77:21
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:82:21
    |
 LL |         ${ignore($i a b c)}
-   |                     ^
+   |           ------    ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
    |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:77:21
-   |
-LL |         ${ignore($i a b c)}
-   |                     ^
+   = note: the `ignore` metavariable expression takes a single argument
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:80:19
-   |
-LL |         ${index() a b c}
-   |                   ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:80:19
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:85:19
    |
 LL |         ${index() a b c}
-   |                   ^
+   |           -----   ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:82:19
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:87:19
    |
 LL |         ${index(1 a b c)}
-   |                   ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:82:19
+   |           -----   ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
    |
-LL |         ${index(1 a b c)}
-   |                   ^
+   = note: the `index` metavariable expression takes between 0 and 1 arguments
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:85:19
-   |
-LL |         ${index() a b c}
-   |                   ^
-   |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:85:19
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:90:19
    |
 LL |         ${index() a b c}
-   |                   ^
+   |           -----   ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
 
-error: unexpected token: a
-  --> $DIR/syntax-errors.rs:87:19
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:92:19
    |
 LL |         ${index(1 a b c)}
-   |                   ^
+   |           -----   ^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
    |
-note: meta-variable expression must not have trailing tokens
-  --> $DIR/syntax-errors.rs:87:19
+   = note: the `index` metavariable expression takes between 0 and 1 arguments
+
+error: unexpected trailing tokens
+  --> $DIR/syntax-errors.rs:94:18
    |
-LL |         ${index(1 a b c)}
-   |                   ^
+LL |         ${index(1, a b c)}
+   |           -----  ^^^^^^^ help: try removing these tokens
+   |           |
+   |           for this metavariable expression
+   |
+   = note: the `index` metavariable expression takes between 0 and 1 arguments
 
 error: meta-variable expression depth must be a literal
-  --> $DIR/syntax-errors.rs:94:33
+  --> $DIR/syntax-errors.rs:101:33
    |
 LL |     ( $( $i:ident ),* ) => { ${ index(IDX) } };
    |                                 ^^^^^
 
 error: meta-variables within meta-variable expressions must be referenced using a dollar sign
-  --> $DIR/syntax-errors.rs:100:11
+  --> $DIR/syntax-errors.rs:107:11
    |
 LL |         ${count(foo)}
    |           ^^^^^
 
 error: meta-variables within meta-variable expressions must be referenced using a dollar sign
-  --> $DIR/syntax-errors.rs:107:11
+  --> $DIR/syntax-errors.rs:114:11
    |
 LL |         ${ignore(bar)}
    |           ^^^^^^
 
-error: unrecognized meta-variable expression
-  --> $DIR/syntax-errors.rs:113:33
+error: unrecognized metavariable expression
+  --> $DIR/syntax-errors.rs:120:33
    |
 LL |     ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
-   |                                 ^^^^^^^^^^^^^^ help: supported expressions are count, ignore, index and len
+   |                                 ^^^^^^^^^^^^^^ not a valid metavariable expression
+   |
+   = note: valid metavariable expressions are `count`, `ignore`, `index`, `len`, and `concat`
 
 error: expected identifier or string literal
   --> $DIR/syntax-errors.rs:38:14
@@ -215,10 +213,10 @@ LL |      () => { ${ "hi" } };
    |                 ^^^^ help: try removing `"hi"`
 
 error: expected identifier or string literal
-  --> $DIR/syntax-errors.rs:60:33
+  --> $DIR/syntax-errors.rs:65:33
    |
 LL |     ( $( $i:ident ),* ) => { ${ {} } };
    |                                 ^^
 
-error: aborting due to 25 previous errors
+error: aborting due to 27 previous errors