diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-01-12 06:52:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-12 06:52:34 +0100 |
| commit | a8bd0c04b480255ed3c2eb329fdba403cbf66ecd (patch) | |
| tree | 0059f00bd23a580a8d784115069aeee7346773bc /tests/ui/error-codes | |
| parent | d4203eda5f0dd0b0192bfd22ea5989fbaf917818 (diff) | |
| parent | 621d4122413b371b0aaa53480d8efd80c4b5f0ba (diff) | |
| download | rust-a8bd0c04b480255ed3c2eb329fdba403cbf66ecd.tar.gz rust-a8bd0c04b480255ed3c2eb329fdba403cbf66ecd.zip | |
Rollup merge of #106167 - yanchen4791:issue-105544-fix, r=oli-obk
Fix invalid syntax and incomplete suggestion in impl Trait parameter type suggestions for E0311
Fixes #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.
Diffstat (limited to 'tests/ui/error-codes')
| -rw-r--r-- | tests/ui/error-codes/E0311.fixed | 13 | ||||
| -rw-r--r-- | tests/ui/error-codes/E0311.rs | 4 | ||||
| -rw-r--r-- | tests/ui/error-codes/E0311.stderr | 10 |
3 files changed, 22 insertions, 5 deletions
diff --git a/tests/ui/error-codes/E0311.fixed b/tests/ui/error-codes/E0311.fixed new file mode 100644 index 00000000000..4410a4d707a --- /dev/null +++ b/tests/ui/error-codes/E0311.fixed @@ -0,0 +1,13 @@ +// run-rustfix + +#![allow(warnings)] + +fn no_restriction<'a, T: 'a>(x: &'a ()) -> &() { + with_restriction::<T>(x) //~ ERROR E0311 +} + +fn with_restriction<'a, T: 'a>(x: &'a ()) -> &'a () { + x +} + +fn main() {} diff --git a/tests/ui/error-codes/E0311.rs b/tests/ui/error-codes/E0311.rs index 566b518b433..99e454f4d75 100644 --- a/tests/ui/error-codes/E0311.rs +++ b/tests/ui/error-codes/E0311.rs @@ -1,3 +1,7 @@ +// run-rustfix + +#![allow(warnings)] + fn no_restriction<T>(x: &()) -> &() { with_restriction::<T>(x) //~ ERROR E0311 } diff --git a/tests/ui/error-codes/E0311.stderr b/tests/ui/error-codes/E0311.stderr index 9873b5ae6ff..b0e6dd1e272 100644 --- a/tests/ui/error-codes/E0311.stderr +++ b/tests/ui/error-codes/E0311.stderr @@ -1,23 +1,23 @@ error[E0311]: the parameter type `T` may not live long enough - --> $DIR/E0311.rs:2:5 + --> $DIR/E0311.rs:6:5 | LL | with_restriction::<T>(x) | ^^^^^^^^^^^^^^^^^^^^^ | note: the parameter type `T` must be valid for the anonymous lifetime defined here... - --> $DIR/E0311.rs:1:25 + --> $DIR/E0311.rs:5:25 | LL | fn no_restriction<T>(x: &()) -> &() { | ^^^ note: ...so that the type `T` will meet its required lifetime bounds - --> $DIR/E0311.rs:2:5 + --> $DIR/E0311.rs:6:5 | LL | with_restriction::<T>(x) | ^^^^^^^^^^^^^^^^^^^^^ help: consider adding an explicit lifetime bound... | -LL | fn no_restriction<'a, T: 'a>(x: &()) -> &() { - | +++ ++++ +LL | fn no_restriction<'a, T: 'a>(x: &'a ()) -> &() { + | +++ ++++ ++ error: aborting due to previous error |
