Skip to content

E0311 references nonexistant lifetime #101027

Closed
@lcnr

Description

@lcnr

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d55c130a01f69dede262830dd4771bdf

fn no_restriction<T>(x: &()) -> &() {
    with_restriction::<T>(x)
}

fn with_restriction<'b, T: 'b>(x: &'b ()) -> &'b () {
    x
}

The current output is:

error[E0311]: the parameter type `T` may not live long enough
 --> src/lib.rs:2:5
  |
2 |     with_restriction::<T>(x)
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
 --> src/lib.rs:1:25
  |
1 | fn no_restriction<T>(x: &()) -> &() {
  |                         ^^^
note: ...so that the type `T` will meet its required lifetime bounds
 --> src/lib.rs:2:5
  |
2 |     with_restriction::<T>(x)
  |     ^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
  |
1 | fn no_restriction<T: 'a>(x: &()) -> &() {
  |                    ++++

'a does not exist, maybe it should get introduced by the diagnostic

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions