rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.25k stars 12.71k forks source link

Improve diagnostic when writing signature `for<'_> Foo<'_>` #55329

Open estebank opened 6 years ago

estebank commented 6 years ago

When encountering the following case

error[E0637]: `'_` cannot be used here
  --> $DIR/underscore-lifetime-binders.rs:24:21
   |
LL | fn meh() -> Box<for<'_> Meh<'_>>
   |                     ^^ `'_` is a reserved lifetime name

error[E0106]: missing lifetime specifier
  --> $DIR/underscore-lifetime-binders.rs:24:29
   |
LL | fn meh() -> Box<for<'_> Meh<'_>>
   |                             ^^ help: consider giving it a 'static lifetime: `'static`
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from

emit a more targeted diagnostic, along the lines of

error[E0637]: `'_` cannot be used here
  --> $DIR/underscore-lifetime-binders.rs:24:21
   |
LL | fn meh() -> Box<for<'_> Meh<'_>>
   |                     ^^      -- `'_` can also not be used here
   |                     |
   |                     `'_` is a reserved lifetime name
help: give it a name instead
   |
LL | fn meh<'a>() -> Box<for<'a> Meh<'a>>
   |       ^^^^              ^^      ^^

Without emitting the E0106.

estebank commented 4 years ago

Triage: no change.

Dylan-DPC commented 2 months ago

Current output:

error[E0637]: `'_` cannot be used here
 --> src/lib.rs:2:25
  |
2 | fn meh() -> Box<dyn for<'_> Meh<'_>> {panic!()}
  |                         ^^ `'_` is a reserved lifetime name

error[E0106]: missing lifetime specifier
 --> src/lib.rs:2:33
  |
2 | fn meh() -> Box<dyn for<'_> Meh<'_>> {panic!()}
  |                                 ^^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
  |
2 | fn meh() -> Box<dyn for<'_> Meh<'static>> {panic!()}
  |                                 ~~~~~~~