Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E0311 for impl Trait parameter type suggests invalid syntax #105544

Closed
AnthonyMikh opened this issue Dec 11, 2022 · 1 comment · Fixed by #106167
Closed

E0311 for impl Trait parameter type suggests invalid syntax #105544

AnthonyMikh opened this issue Dec 11, 2022 · 1 comment · Fixed by #106167
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@AnthonyMikh
Copy link
Contributor

AnthonyMikh commented Dec 11, 2022

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

@AnthonyMikh AnthonyMikh added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 11, 2022
@rustbot rustbot added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 11, 2022
@estebank estebank added A-lifetimes Area: lifetime related A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. labels Dec 13, 2022
@yanchen4791
Copy link
Contributor

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jan 12, 2023
…i-obk

Fix invalid syntax and incomplete suggestion in impl Trait parameter type suggestions for E0311

Fixes rust-lang#105544

The problems: The suggestion given for E0311 has invalid syntax when the synthetic type parameter is used for Trait type in function declaration:
```rust
fn foo(d: impl Sized) -> impl Sized
```
instead of explicitly specified like the following:
```rust
fn foo<T: Sized>(d: T) -> impl Sized
```
In addition to the syntax error, the suggestions given for E0311 are not complete when multiple elided lifetimes are involved in lifetime bounds, not all involved parameters are given the named lifetime in the suggestions. For the following test case:
```
fn foo(d: impl Sized, p: &mut ()) -> impl Sized + '_ {
    (d, p)
}
```
a good suggestion should add the lifetime 'a to both d and p, instead of d only:
```
fn foo<'a>(d: impl Sized + 'a, p: &'a mut ()) -> impl Sized + '_ {
    (d, p)
}
```

The Solution: Fix the syntax problem in the suggestions when synthetic type parameter is used, and also add lifetimes for all involved parameters.
@bors bors closed this as completed in a8bd0c0 Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants