Skip to content

E0311 for impl Trait parameter type suggests invalid syntax #105544

Closed
@AnthonyMikh

Description

@AnthonyMikh

Given the following code:

fn foo(d: impl Sized, p: &mut ()) -> impl Sized + '_ {
    (d, p)
}

The current output is:

error[E0311]: the parameter type `impl Sized` may not live long enough
 --> src/lib.rs:2:5
  |
2 |     (d, p)
  |     ^^^^^^
  |
note: the parameter type `impl Sized` must be valid for the anonymous lifetime defined here...
 --> src/lib.rs:1:26
  |
1 | fn foo(d: impl Sized, p: &mut ()) -> impl Sized + '_ {
  |                          ^^^^^^^
note: ...so that the type `impl Sized` will meet its required lifetime bounds
 --> src/lib.rs:2:5
  |
2 |     (d, p)
  |     ^^^^^^
help: consider adding an explicit lifetime bound...
  |
1 | fn foo(d: 'a, impl Sized + 'a, p: &mut ()) -> impl Sized + '_ {

Not only the suggested syntax is invalid, it also suggests introducing a fresh lifetime parameter instead of naming the elided one on p.

Ping @estebank.

@rustbot modify labels: +A-suggestion-diagnostics +D-invalid-suggestion

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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