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-17 00:16:22 +0200
committerGitHub <noreply@github.com>2025-04-17 00:16:22 +0200
commit7ab385e2e1ab9f6cb45cfc8ee4fbe754e2f52486 (patch)
treec274070331ca87384103c3fe3be7ee655e218882 /tests/ui/parser
parentc594a88f4353766e2474163f7f8cf68840682a67 (diff)
parent6242335fdb7444876abf1c3669b6aab1649a0a64 (diff)
downloadrust-7ab385e2e1ab9f6cb45cfc8ee4fbe754e2f52486.tar.gz
rust-7ab385e2e1ab9f6cb45cfc8ee4fbe754e2f52486.zip
Rollup merge of #139854 - fmease:modern-diag-for-lt-in-ty, r=davidtwco
Improve parse errors for stray lifetimes in type position

While technically & syntactically speaking lifetimes do begin[^1] types in type contexts (this essentially excludes generic argument lists) and require a following `+` to form a complete type (`'a +` denotes a bare trait object type), the likelihood that a user meant to write a lifetime-prefixed bare trait object type in *modern* editions (Rust ≥2021) when placing a lifetime into a type context is incredibly low (they would need to add at least three tokens to turn it into a *semantically* well-formed TOT: `'a` → `dyn 'a + Trait`).

Therefore let's *lie* in modern editions (just like in PR https://github.com/rust-lang/rust/pull/131239, a precedent if you will) by stating "*expected type, found lifetime*" in such cases which is a lot more a approachable, digestible and friendly compared to "*lifetime in trait object type must be followed by `+`*" (as added in PR https://github.com/rust-lang/rust/pull/69760).

I've also added recovery for "ampersand-less" reference types (e.g., `'a ()`, `'a mut Ty`) in modern editions because it was trivial to do and I think it's not unlikely to occur in practice.

Fixes #133413.

[^1]: For example, in the context of decl macros, this implies that a lone `'a` always matches syntax fragment `ty` ("even if" there's a later macro matcher expecting syntax fragment `lifetime`). Rephrased, lifetimes (in type contexts) *commit* to the type parser.
Diffstat (limited to 'tests/ui/parser')
-rw-r--r--tests/ui/parser/issues/issue-73568-lifetime-after-mut.rs2
-rw-r--r--tests/ui/parser/issues/issue-73568-lifetime-after-mut.stderr4
-rw-r--r--tests/ui/parser/macro/trait-object-macro-matcher.e2015.stderr32
-rw-r--r--tests/ui/parser/macro/trait-object-macro-matcher.e2021.stderr16
-rw-r--r--tests/ui/parser/macro/trait-object-macro-matcher.rs12
-rw-r--r--tests/ui/parser/macro/trait-object-macro-matcher.stderr23
-rw-r--r--tests/ui/parser/recover/recover-ampersand-less-ref-ty.rs13
-rw-r--r--tests/ui/parser/recover/recover-ampersand-less-ref-ty.stderr24
-rw-r--r--tests/ui/parser/trait-object-bad-parens.rs12
-rw-r--r--tests/ui/parser/trait-object-bad-parens.stderr22
-rw-r--r--tests/ui/parser/trait-object-lifetime-parens.e2015.stderr (renamed from tests/ui/parser/trait-object-lifetime-parens.stderr)13
-rw-r--r--tests/ui/parser/trait-object-lifetime-parens.e2021.stderr51
-rw-r--r--tests/ui/parser/trait-object-lifetime-parens.rs14
-rw-r--r--tests/ui/parser/trait-object-polytrait-priority.rs2
-rw-r--r--tests/ui/parser/trait-object-polytrait-priority.stderr4
15 files changed, 186 insertions, 58 deletions
diff --git a/tests/ui/parser/issues/issue-73568-lifetime-after-mut.rs b/tests/ui/parser/issues/issue-73568-lifetime-after-mut.rs
index cf754a6854e..782a46ab060 100644
--- a/tests/ui/parser/issues/issue-73568-lifetime-after-mut.rs
+++ b/tests/ui/parser/issues/issue-73568-lifetime-after-mut.rs
@@ -12,7 +12,7 @@ mac!('a);
 
 // avoid false positives
 fn y<'a>(y: &mut 'a + Send) {
-    //~^ ERROR expected a path on the left-hand side of `+`, not `&mut 'a`
+    //~^ ERROR expected a path on the left-hand side of `+`
     //~| ERROR at least one trait is required for an object type
     let z = y as &mut 'a + Send;
     //~^ ERROR expected value, found trait `Send`
diff --git a/tests/ui/parser/issues/issue-73568-lifetime-after-mut.stderr b/tests/ui/parser/issues/issue-73568-lifetime-after-mut.stderr
index 6b8f8e4fe4e..ae1ed72853d 100644
--- a/tests/ui/parser/issues/issue-73568-lifetime-after-mut.stderr
+++ b/tests/ui/parser/issues/issue-73568-lifetime-after-mut.stderr
@@ -10,11 +10,11 @@ LL - fn x<'a>(x: &mut 'a i32){}
 LL + fn x<'a>(x: &'a mut i32){}
    |
 
-error[E0178]: expected a path on the left-hand side of `+`, not `&mut 'a`
+error[E0178]: expected a path on the left-hand side of `+`
   --> $DIR/issue-73568-lifetime-after-mut.rs:14:13
    |
 LL | fn y<'a>(y: &mut 'a + Send) {
-   |             ^^^^^^^^^^^^^^
+   |             ^^^^^^^
    |
 help: try adding parentheses
    |
diff --git a/tests/ui/parser/macro/trait-object-macro-matcher.e2015.stderr b/tests/ui/parser/macro/trait-object-macro-matcher.e2015.stderr
new file mode 100644
index 00000000000..f2db351de4a
--- /dev/null
+++ b/tests/ui/parser/macro/trait-object-macro-matcher.e2015.stderr
@@ -0,0 +1,32 @@
+error: lifetimes must be followed by `+` to form a trait object type
+  --> $DIR/trait-object-macro-matcher.rs:17:8
+   |
+LL |     m!('static);
+   |        ^^^^^^^
+   |
+help: consider adding a trait bound after the potential lifetime bound
+   |
+LL |     m!('static + /* Trait */);
+   |                +++++++++++++
+
+error: lifetimes must be followed by `+` to form a trait object type
+  --> $DIR/trait-object-macro-matcher.rs:17:8
+   |
+LL |     m!('static);
+   |        ^^^^^^^
+   |
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+help: consider adding a trait bound after the potential lifetime bound
+   |
+LL |     m!('static + /* Trait */);
+   |                +++++++++++++
+
+error[E0224]: at least one trait is required for an object type
+  --> $DIR/trait-object-macro-matcher.rs:17:8
+   |
+LL |     m!('static);
+   |        ^^^^^^^
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0224`.
diff --git a/tests/ui/parser/macro/trait-object-macro-matcher.e2021.stderr b/tests/ui/parser/macro/trait-object-macro-matcher.e2021.stderr
new file mode 100644
index 00000000000..7d9e8d795d1
--- /dev/null
+++ b/tests/ui/parser/macro/trait-object-macro-matcher.e2021.stderr
@@ -0,0 +1,16 @@
+error: expected type, found lifetime
+  --> $DIR/trait-object-macro-matcher.rs:17:8
+   |
+LL |     m!('static);
+   |        ^^^^^^^ expected type
+
+error: expected type, found lifetime
+  --> $DIR/trait-object-macro-matcher.rs:17:8
+   |
+LL |     m!('static);
+   |        ^^^^^^^ expected type
+   |
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/parser/macro/trait-object-macro-matcher.rs b/tests/ui/parser/macro/trait-object-macro-matcher.rs
index d4ec199070e..ba61752fe40 100644
--- a/tests/ui/parser/macro/trait-object-macro-matcher.rs
+++ b/tests/ui/parser/macro/trait-object-macro-matcher.rs
@@ -1,6 +1,10 @@
 // A single lifetime is not parsed as a type.
 // `ty` matcher in particular doesn't accept a single lifetime
 
+//@ revisions: e2015 e2021
+//@[e2015] edition: 2015
+//@[e2021] edition: 2021
+
 macro_rules! m {
     ($t: ty) => {
         let _: $t;
@@ -8,8 +12,10 @@ macro_rules! m {
 }
 
 fn main() {
+    //[e2021]~vv ERROR expected type, found lifetime
+    //[e2021]~v ERROR expected type, found lifetime
     m!('static);
-    //~^ ERROR lifetime in trait object type must be followed by `+`
-    //~| ERROR lifetime in trait object type must be followed by `+`
-    //~| ERROR at least one trait is required for an object type
+    //[e2015]~^ ERROR lifetimes must be followed by `+` to form a trait object type
+    //[e2015]~| ERROR lifetimes must be followed by `+` to form a trait object type
+    //[e2015]~| ERROR at least one trait is required for an object type
 }
diff --git a/tests/ui/parser/macro/trait-object-macro-matcher.stderr b/tests/ui/parser/macro/trait-object-macro-matcher.stderr
deleted file mode 100644
index 81dca6f71c4..00000000000
--- a/tests/ui/parser/macro/trait-object-macro-matcher.stderr
+++ /dev/null
@@ -1,23 +0,0 @@
-error: lifetime in trait object type must be followed by `+`
-  --> $DIR/trait-object-macro-matcher.rs:11:8
-   |
-LL |     m!('static);
-   |        ^^^^^^^
-
-error: lifetime in trait object type must be followed by `+`
-  --> $DIR/trait-object-macro-matcher.rs:11:8
-   |
-LL |     m!('static);
-   |        ^^^^^^^
-   |
-   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
-
-error[E0224]: at least one trait is required for an object type
-  --> $DIR/trait-object-macro-matcher.rs:11:8
-   |
-LL |     m!('static);
-   |        ^^^^^^^
-
-error: aborting due to 3 previous errors
-
-For more information about this error, try `rustc --explain E0224`.
diff --git a/tests/ui/parser/recover/recover-ampersand-less-ref-ty.rs b/tests/ui/parser/recover/recover-ampersand-less-ref-ty.rs
new file mode 100644
index 00000000000..8f1a42473b5
--- /dev/null
+++ b/tests/ui/parser/recover/recover-ampersand-less-ref-ty.rs
@@ -0,0 +1,13 @@
+//@ edition: 2021
+
+struct Entity<'a> {
+    name: 'a str, //~ ERROR expected type, found lifetime
+    //~^ HELP you might have meant to write a reference type here
+}
+
+struct Buffer<'buf> {
+    bytes: 'buf mut [u8], //~ ERROR expected type, found lifetime
+    //~^ HELP you might have meant to write a reference type here
+}
+
+fn main() {}
diff --git a/tests/ui/parser/recover/recover-ampersand-less-ref-ty.stderr b/tests/ui/parser/recover/recover-ampersand-less-ref-ty.stderr
new file mode 100644
index 00000000000..033348b2c40
--- /dev/null
+++ b/tests/ui/parser/recover/recover-ampersand-less-ref-ty.stderr
@@ -0,0 +1,24 @@
+error: expected type, found lifetime
+  --> $DIR/recover-ampersand-less-ref-ty.rs:4:11
+   |
+LL |     name: 'a str,
+   |           ^^ expected type
+   |
+help: you might have meant to write a reference type here
+   |
+LL |     name: &'a str,
+   |           +
+
+error: expected type, found lifetime
+  --> $DIR/recover-ampersand-less-ref-ty.rs:9:12
+   |
+LL |     bytes: 'buf mut [u8],
+   |            ^^^^ expected type
+   |
+help: you might have meant to write a reference type here
+   |
+LL |     bytes: &'buf mut [u8],
+   |            +
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/parser/trait-object-bad-parens.rs b/tests/ui/parser/trait-object-bad-parens.rs
index 8e267c7448f..bb047a4b431 100644
--- a/tests/ui/parser/trait-object-bad-parens.rs
+++ b/tests/ui/parser/trait-object-bad-parens.rs
@@ -5,12 +5,8 @@
 auto trait Auto {}
 
 fn main() {
-    let _: Box<((Auto)) + Auto>;
-    //~^ ERROR expected a path on the left-hand side of `+`, not `((Auto))`
-    let _: Box<(Auto + Auto) + Auto>;
-    //~^ ERROR expected a path on the left-hand side of `+`, not `(Auto + Auto)`
-    let _: Box<(Auto +) + Auto>;
-    //~^ ERROR expected a path on the left-hand side of `+`, not `(Auto)`
-    let _: Box<(dyn Auto) + Auto>;
-    //~^ ERROR expected a path on the left-hand side of `+`, not `(dyn Auto)`
+    let _: Box<((Auto)) + Auto>; //~ ERROR expected a path on the left-hand side of `+`
+    let _: Box<(Auto + Auto) + Auto>; //~ ERROR expected a path on the left-hand side of `+`
+    let _: Box<(Auto +) + Auto>; //~ ERROR expected a path on the left-hand side of `+`
+    let _: Box<(dyn Auto) + Auto>; //~ ERROR expected a path on the left-hand side of `+`
 }
diff --git a/tests/ui/parser/trait-object-bad-parens.stderr b/tests/ui/parser/trait-object-bad-parens.stderr
index 74e484eebee..7c2559ce89f 100644
--- a/tests/ui/parser/trait-object-bad-parens.stderr
+++ b/tests/ui/parser/trait-object-bad-parens.stderr
@@ -1,26 +1,26 @@
-error[E0178]: expected a path on the left-hand side of `+`, not `((Auto))`
+error[E0178]: expected a path on the left-hand side of `+`
   --> $DIR/trait-object-bad-parens.rs:8:16
    |
 LL |     let _: Box<((Auto)) + Auto>;
-   |                ^^^^^^^^^^^^^^^ expected a path
+   |                ^^^^^^^^ expected a path
 
-error[E0178]: expected a path on the left-hand side of `+`, not `(Auto + Auto)`
-  --> $DIR/trait-object-bad-parens.rs:10:16
+error[E0178]: expected a path on the left-hand side of `+`
+  --> $DIR/trait-object-bad-parens.rs:9:16
    |
 LL |     let _: Box<(Auto + Auto) + Auto>;
-   |                ^^^^^^^^^^^^^^^^^^^^ expected a path
+   |                ^^^^^^^^^^^^^ expected a path
 
-error[E0178]: expected a path on the left-hand side of `+`, not `(Auto)`
-  --> $DIR/trait-object-bad-parens.rs:12:16
+error[E0178]: expected a path on the left-hand side of `+`
+  --> $DIR/trait-object-bad-parens.rs:10:16
    |
 LL |     let _: Box<(Auto +) + Auto>;
-   |                ^^^^^^^^^^^^^^^ expected a path
+   |                ^^^^^^^^ expected a path
 
-error[E0178]: expected a path on the left-hand side of `+`, not `(dyn Auto)`
-  --> $DIR/trait-object-bad-parens.rs:14:16
+error[E0178]: expected a path on the left-hand side of `+`
+  --> $DIR/trait-object-bad-parens.rs:11:16
    |
 LL |     let _: Box<(dyn Auto) + Auto>;
-   |                ^^^^^^^^^^^^^^^^^ expected a path
+   |                ^^^^^^^^^^ expected a path
 
 error: aborting due to 4 previous errors
 
diff --git a/tests/ui/parser/trait-object-lifetime-parens.stderr b/tests/ui/parser/trait-object-lifetime-parens.e2015.stderr
index 280c0e40c64..cf0b3d77f5b 100644
--- a/tests/ui/parser/trait-object-lifetime-parens.stderr
+++ b/tests/ui/parser/trait-object-lifetime-parens.e2015.stderr
@@ -1,5 +1,5 @@
 error: parenthesized lifetime bounds are not supported
-  --> $DIR/trait-object-lifetime-parens.rs:5:21
+  --> $DIR/trait-object-lifetime-parens.rs:9:21
    |
 LL | fn f<'a, T: Trait + ('a)>() {}
    |                     ^^^^
@@ -11,7 +11,7 @@ LL + fn f<'a, T: Trait + 'a>() {}
    |
 
 error: parenthesized lifetime bounds are not supported
-  --> $DIR/trait-object-lifetime-parens.rs:8:24
+  --> $DIR/trait-object-lifetime-parens.rs:12:24
    |
 LL |     let _: Box<Trait + ('a)>;
    |                        ^^^^
@@ -22,11 +22,16 @@ LL -     let _: Box<Trait + ('a)>;
 LL +     let _: Box<Trait + 'a>;
    |
 
-error: lifetime in trait object type must be followed by `+`
-  --> $DIR/trait-object-lifetime-parens.rs:10:17
+error: lifetimes must be followed by `+` to form a trait object type
+  --> $DIR/trait-object-lifetime-parens.rs:16:17
    |
 LL |     let _: Box<('a) + Trait>;
    |                 ^^
+   |
+help: consider adding a trait bound after the potential lifetime bound
+   |
+LL |     let _: Box<('a + /* Trait */) + Trait>;
+   |                    +++++++++++++
 
 error: aborting due to 3 previous errors
 
diff --git a/tests/ui/parser/trait-object-lifetime-parens.e2021.stderr b/tests/ui/parser/trait-object-lifetime-parens.e2021.stderr
new file mode 100644
index 00000000000..b65c079788a
--- /dev/null
+++ b/tests/ui/parser/trait-object-lifetime-parens.e2021.stderr
@@ -0,0 +1,51 @@
+error: parenthesized lifetime bounds are not supported
+  --> $DIR/trait-object-lifetime-parens.rs:9:21
+   |
+LL | fn f<'a, T: Trait + ('a)>() {}
+   |                     ^^^^
+   |
+help: remove the parentheses
+   |
+LL - fn f<'a, T: Trait + ('a)>() {}
+LL + fn f<'a, T: Trait + 'a>() {}
+   |
+
+error: parenthesized lifetime bounds are not supported
+  --> $DIR/trait-object-lifetime-parens.rs:12:24
+   |
+LL |     let _: Box<Trait + ('a)>;
+   |                        ^^^^
+   |
+help: remove the parentheses
+   |
+LL -     let _: Box<Trait + ('a)>;
+LL +     let _: Box<Trait + 'a>;
+   |
+
+error: expected type, found lifetime
+  --> $DIR/trait-object-lifetime-parens.rs:16:17
+   |
+LL |     let _: Box<('a) + Trait>;
+   |                 ^^ expected type
+
+error[E0178]: expected a path on the left-hand side of `+`
+  --> $DIR/trait-object-lifetime-parens.rs:16:16
+   |
+LL |     let _: Box<('a) + Trait>;
+   |                ^^^^ expected a path
+
+error[E0782]: expected a type, found a trait
+  --> $DIR/trait-object-lifetime-parens.rs:12:16
+   |
+LL |     let _: Box<Trait + ('a)>;
+   |                ^^^^^^^^^^^^
+   |
+help: you can add the `dyn` keyword if you want a trait object
+   |
+LL |     let _: Box<dyn Trait + ('a)>;
+   |                +++
+
+error: aborting due to 5 previous errors
+
+Some errors have detailed explanations: E0178, E0782.
+For more information about an error, try `rustc --explain E0178`.
diff --git a/tests/ui/parser/trait-object-lifetime-parens.rs b/tests/ui/parser/trait-object-lifetime-parens.rs
index f44ebe5ba5b..0ff4660bb0d 100644
--- a/tests/ui/parser/trait-object-lifetime-parens.rs
+++ b/tests/ui/parser/trait-object-lifetime-parens.rs
@@ -1,4 +1,8 @@
-#![allow(bare_trait_objects)]
+//@ revisions: e2015 e2021
+//@[e2015] edition: 2015
+//@[e2021] edition: 2021
+
+#![cfg_attr(e2015, allow(bare_trait_objects))]
 
 trait Trait {}
 
@@ -6,8 +10,12 @@ fn f<'a, T: Trait + ('a)>() {} //~ ERROR parenthesized lifetime bounds are not s
 
 fn check<'a>() {
     let _: Box<Trait + ('a)>; //~ ERROR parenthesized lifetime bounds are not supported
-    // FIXME: It'd be great if we could add suggestion to the following case.
-    let _: Box<('a) + Trait>; //~ ERROR lifetime in trait object type must be followed by `+`
+    //[e2021]~^ ERROR expected a type, found a trait
+    // FIXME: It'd be great if we could suggest removing the parentheses here too.
+    //[e2015]~v ERROR lifetimes must be followed by `+` to form a trait object type
+    let _: Box<('a) + Trait>;
+    //[e2021]~^ ERROR expected type, found lifetime
+    //[e2021]~| ERROR expected a path on the left-hand side of `+`
 }
 
 fn main() {}
diff --git a/tests/ui/parser/trait-object-polytrait-priority.rs b/tests/ui/parser/trait-object-polytrait-priority.rs
index e7f085104ae..85568f0fe1b 100644
--- a/tests/ui/parser/trait-object-polytrait-priority.rs
+++ b/tests/ui/parser/trait-object-polytrait-priority.rs
@@ -4,6 +4,6 @@ trait Trait<'a> {}
 
 fn main() {
     let _: &for<'a> Trait<'a> + 'static;
-    //~^ ERROR expected a path on the left-hand side of `+`, not `&for<'a> Trait<'a>`
+    //~^ ERROR expected a path on the left-hand side of `+`
     //~| HELP try adding parentheses
 }
diff --git a/tests/ui/parser/trait-object-polytrait-priority.stderr b/tests/ui/parser/trait-object-polytrait-priority.stderr
index 8cb564e7930..a291a8e229c 100644
--- a/tests/ui/parser/trait-object-polytrait-priority.stderr
+++ b/tests/ui/parser/trait-object-polytrait-priority.stderr
@@ -1,8 +1,8 @@
-error[E0178]: expected a path on the left-hand side of `+`, not `&for<'a> Trait<'a>`
+error[E0178]: expected a path on the left-hand side of `+`
   --> $DIR/trait-object-polytrait-priority.rs:6:12
    |
 LL |     let _: &for<'a> Trait<'a> + 'static;
-   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |            ^^^^^^^^^^^^^^^^^^
    |
 help: try adding parentheses
    |