about summary refs log tree commit diff
path: root/tests/ui/parser
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-14 18:15:31 +0200
committerGitHub <noreply@github.com>2025-04-14 18:15:31 +0200
commitbf49dfc943406c086d31ecf381eea628d13fcc2e (patch)
tree8ac64bc587efbe72e194cb9d0db0d1093b617f10 /tests/ui/parser
parenta4adc005a19a745b1e9159841a1cbd45fca5c9af (diff)
parent6dfbe7c986d55a5a48f625d37d4576092e5638eb (diff)
downloadrust-bf49dfc943406c086d31ecf381eea628d13fcc2e.tar.gz
rust-bf49dfc943406c086d31ecf381eea628d13fcc2e.zip
Rollup merge of #139392 - compiler-errors:raw-expr, r=oli-obk
Detect and provide suggestion for `&raw EXPR`

When emitting an error in the parser, and we detect that the previous token was `raw` and we *could* have consumed `const`/`mut`, suggest that this may have been a mistyped raw ref expr. To do this, we add `const`/`mut` to the expected token set when parsing `&raw` as an expression (which does not affect the "good path" of parsing, for the record).

This is kind of a rudimentary error improvement, since it doesn't actually attempt to recover anything, leading to some other knock-on errors b/c we still treat `&raw` as the expression that was parsed... but at least we add the suggestion! I don't think the parser grammar means we can faithfully recover `&raw EXPR` early, i.e. during `parse_expr_borrow`.

Fixes #133231
Diffstat (limited to 'tests/ui/parser')
-rw-r--r--tests/ui/parser/recover/raw-no-const-mut.rs31
-rw-r--r--tests/ui/parser/recover/raw-no-const-mut.stderr109
2 files changed, 140 insertions, 0 deletions
diff --git a/tests/ui/parser/recover/raw-no-const-mut.rs b/tests/ui/parser/recover/raw-no-const-mut.rs
new file mode 100644
index 00000000000..d0ae69cc308
--- /dev/null
+++ b/tests/ui/parser/recover/raw-no-const-mut.rs
@@ -0,0 +1,31 @@
+fn a() {
+    let x = &raw 1;
+    //~^ ERROR expected one of
+}
+
+fn b() {
+    [&raw const 1, &raw 2]
+    //~^ ERROR expected one of
+    //~| ERROR cannot find value `raw` in this scope
+    //~| ERROR cannot take address of a temporary
+}
+
+fn c() {
+    if x == &raw z {}
+    //~^ ERROR expected `{`
+}
+
+fn d() {
+    f(&raw 2);
+    //~^ ERROR expected one of
+    //~| ERROR cannot find value `raw` in this scope
+    //~| ERROR cannot find function `f` in this scope
+}
+
+fn e() {
+    let x;
+    x = &raw 1;
+    //~^ ERROR expected one of
+}
+
+fn main() {}
diff --git a/tests/ui/parser/recover/raw-no-const-mut.stderr b/tests/ui/parser/recover/raw-no-const-mut.stderr
new file mode 100644
index 00000000000..65032c80795
--- /dev/null
+++ b/tests/ui/parser/recover/raw-no-const-mut.stderr
@@ -0,0 +1,109 @@
+error: expected one of `!`, `.`, `::`, `;`, `?`, `const`, `else`, `mut`, `{`, or an operator, found `1`
+  --> $DIR/raw-no-const-mut.rs:2:18
+   |
+LL |     let x = &raw 1;
+   |                  ^ expected one of 10 possible tokens
+   |
+help: `&raw` must be followed by `const` or `mut` to be a raw reference expression
+   |
+LL |     let x = &raw const 1;
+   |                  +++++
+LL |     let x = &raw mut 1;
+   |                  +++
+
+error: expected one of `!`, `,`, `.`, `::`, `?`, `]`, `const`, `mut`, `{`, or an operator, found `2`
+  --> $DIR/raw-no-const-mut.rs:7:25
+   |
+LL |     [&raw const 1, &raw 2]
+   |                         ^ expected one of 10 possible tokens
+   |
+help: `&raw` must be followed by `const` or `mut` to be a raw reference expression
+   |
+LL |     [&raw const 1, &raw const 2]
+   |                         +++++
+LL |     [&raw const 1, &raw mut 2]
+   |                         +++
+help: missing `,`
+   |
+LL |     [&raw const 1, &raw, 2]
+   |                        +
+
+error: expected `{`, found `z`
+  --> $DIR/raw-no-const-mut.rs:14:18
+   |
+LL |     if x == &raw z {}
+   |                  ^ expected `{`
+   |
+note: the `if` expression is missing a block after this condition
+  --> $DIR/raw-no-const-mut.rs:14:8
+   |
+LL |     if x == &raw z {}
+   |        ^^^^^^^^^
+help: `&raw` must be followed by `const` or `mut` to be a raw reference expression
+   |
+LL |     if x == &raw const z {}
+   |                  +++++
+LL |     if x == &raw mut z {}
+   |                  +++
+
+error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `const`, `mut`, `{`, or an operator, found `2`
+  --> $DIR/raw-no-const-mut.rs:19:12
+   |
+LL |     f(&raw 2);
+   |            ^ expected one of 10 possible tokens
+   |
+help: `&raw` must be followed by `const` or `mut` to be a raw reference expression
+   |
+LL |     f(&raw const 2);
+   |            +++++
+LL |     f(&raw mut 2);
+   |            +++
+help: missing `,`
+   |
+LL |     f(&raw, 2);
+   |           +
+
+error: expected one of `!`, `.`, `::`, `;`, `?`, `const`, `mut`, `{`, `}`, or an operator, found `1`
+  --> $DIR/raw-no-const-mut.rs:27:14
+   |
+LL |     x = &raw 1;
+   |              ^ expected one of 10 possible tokens
+   |
+help: `&raw` must be followed by `const` or `mut` to be a raw reference expression
+   |
+LL |     x = &raw const 1;
+   |              +++++
+LL |     x = &raw mut 1;
+   |              +++
+
+error[E0425]: cannot find value `raw` in this scope
+  --> $DIR/raw-no-const-mut.rs:7:21
+   |
+LL |     [&raw const 1, &raw 2]
+   |                     ^^^ not found in this scope
+
+error[E0425]: cannot find value `raw` in this scope
+  --> $DIR/raw-no-const-mut.rs:19:8
+   |
+LL |     f(&raw 2);
+   |        ^^^ not found in this scope
+
+error[E0745]: cannot take address of a temporary
+  --> $DIR/raw-no-const-mut.rs:7:17
+   |
+LL |     [&raw const 1, &raw 2]
+   |                 ^ temporary value
+
+error[E0425]: cannot find function `f` in this scope
+  --> $DIR/raw-no-const-mut.rs:19:5
+   |
+LL | fn a() {
+   | ------ similarly named function `a` defined here
+...
+LL |     f(&raw 2);
+   |     ^ help: a function with a similar name exists: `a`
+
+error: aborting due to 9 previous errors
+
+Some errors have detailed explanations: E0425, E0745.
+For more information about an error, try `rustc --explain E0425`.