Improve parse errors for lifetimes in type position

This commit is contained in:
León Orell Valerian Liehr 2025-04-14 20:48:51 +02:00
parent 97c966bb40
commit 8887af72a0
No known key found for this signature in database
GPG key ID: D17A07215F68E713
14 changed files with 234 additions and 42 deletions

View file

@ -3,7 +3,7 @@ trait X {
}
fn foo<'a>(arg: Box<dyn X<Y('a) = &'a ()>>) {}
//~^ ERROR: lifetime in trait object type must be followed by `+`
//~^ ERROR: lifetimes must be followed by `+` to form a trait object type
//~| ERROR: parenthesized generic arguments cannot be used
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments

View file

@ -1,8 +1,13 @@
error: lifetime in trait object type must be followed by `+`
error: lifetimes must be followed by `+` to form a trait object type
--> $DIR/gat-trait-path-parenthesised-args.rs:5:29
|
LL | fn foo<'a>(arg: Box<dyn X<Y('a) = &'a ()>>) {}
| ^^
|
help: consider adding a trait bound after the potential lifetime bound
|
LL | fn foo<'a>(arg: Box<dyn X<Y('a + /* Trait */) = &'a ()>>) {}
| +++++++++++++
error: parenthesized generic arguments cannot be used in associated type constraints
--> $DIR/gat-trait-path-parenthesised-args.rs:5:27

View file

@ -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`.

View file

@ -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

View file

@ -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
}

View file

@ -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`.

View file

@ -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() {}

View file

@ -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

View file

@ -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

View file

@ -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 `+`, not `((/*ERROR*/))`
--> $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`.

View file

@ -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() {}