You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of rust-lang#106167 - yanchen4791:issue-105544-fix, r=oli-obk
Fix invalid syntax and incomplete suggestion in impl Trait parameter type suggestions for E0311
Fixesrust-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.
fn foo<'a>(d: impl Sized + 'a, p: &'a mut ()) -> impl Sized + '_ { //~ NOTE the parameter type `impl Sized` must be valid for the anonymous lifetime defined here...
6
+
//~^ HELP consider adding an explicit lifetime bound
7
+
(d, p)
8
+
//~^ ERROR the parameter type `impl Sized` may not live long enough
9
+
//~| NOTE ...so that the type `impl Sized` will meet its required lifetime bounds
//~^ HELP consider adding an explicit lifetime bound...
14
+
(d, p) //~ NOTE ...so that the type `impl Sized` will meet its required lifetime bounds
15
+
//~^ ERROR the parameter type `impl Sized` may not live long enough
16
+
}
17
+
18
+
fn foo2<'b, 'a>(d: impl Sized + 'a + 'b, p: &'b mut ()) -> impl Sized + '_ { //~ NOTE the parameter type `impl Sized + 'a` must be valid for the anonymous lifetime defined here...
19
+
//~^ HELP consider adding an explicit lifetime bound
20
+
(d, p)
21
+
//~^ ERROR the parameter type `impl Sized + 'a` may not live long enough
22
+
//~| NOTE ...so that the type `impl Sized + 'a` will meet its required lifetime bounds
23
+
}
24
+
25
+
fn bar<'a, T : Sized + 'a>(d: T, p: &'a mut ()) -> impl Sized + '_ { //~ NOTE the parameter type `T` must be valid for the anonymous lifetime defined here...
26
+
//~^ HELP consider adding an explicit lifetime bound
27
+
(d, p)
28
+
//~^ ERROR the parameter type `T` may not live long enough
29
+
//~| NOTE ...so that the type `T` will meet its required lifetime bounds
//~^ HELP consider adding an explicit lifetime bound...
34
+
(d, p) //~ NOTE ...so that the type `T` will meet its required lifetime bounds
35
+
//~^ ERROR the parameter type `T` may not live long enough
36
+
}
37
+
38
+
fn bar2<'b, 'a, T : Sized + 'a + 'b>(d: T, p: &'b mut ()) -> impl Sized + '_ { //~ NOTE the parameter type `T` must be valid for the anonymous lifetime defined here...
39
+
//~^ HELP consider adding an explicit lifetime bound
40
+
(d, p)
41
+
//~^ ERROR the parameter type `T` may not live long enough
42
+
//~| NOTE ...so that the type `T` will meet its required lifetime bounds
//~^ HELP consider adding an explicit lifetime bound...
14
+
(d, p)//~ NOTE ...so that the type `impl Sized` will meet its required lifetime bounds
15
+
//~^ ERROR the parameter type `impl Sized` may not live long enough
16
+
}
17
+
18
+
fnfoo2<'a>(d:implSized + 'a,p:&mut()) -> implSized + '_{//~ NOTE the parameter type `impl Sized + 'a` must be valid for the anonymous lifetime defined here...
19
+
//~^ HELP consider adding an explicit lifetime bound
20
+
(d, p)
21
+
//~^ ERROR the parameter type `impl Sized + 'a` may not live long enough
22
+
//~| NOTE ...so that the type `impl Sized + 'a` will meet its required lifetime bounds
23
+
}
24
+
25
+
fnbar<T:Sized>(d:T,p:&mut()) -> implSized + '_{//~ NOTE the parameter type `T` must be valid for the anonymous lifetime defined here...
26
+
//~^ HELP consider adding an explicit lifetime bound
27
+
(d, p)
28
+
//~^ ERROR the parameter type `T` may not live long enough
29
+
//~| NOTE ...so that the type `T` will meet its required lifetime bounds
0 commit comments