diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-09-16 12:34:20 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-16 12:34:20 +0200 |
| commit | 54d77285fcba0ab3d9d5ad8ff8a02bd6187bcf58 (patch) | |
| tree | 2396fc6921f9d83050e0f690ca20d9e990c824a7 /src | |
| parent | 2c2f1c239e58c47eb9b94bc6328af5f3378cfa4d (diff) | |
| parent | e1607c87f0bb96c1c59d84a2789b7f7d2b69e182 (diff) | |
| download | rust-54d77285fcba0ab3d9d5ad8ff8a02bd6187bcf58.tar.gz rust-54d77285fcba0ab3d9d5ad8ff8a02bd6187bcf58.zip | |
Rollup merge of #76695 - iximeow:trait-generic-bound-suggestion, r=estebank
fix syntax error in suggesting generic constraint in trait parameter
suggest `where T: Foo` for the first bound on a trait, then suggest
`, T: Foo` when the suggested bound would add to an existing set of
`where` clauses. `where T: Foo` may be the first bound if `T` has a
default, because we'd rather suggest
```
trait A<T=()> where T: Copy
```
than
```
trait A<T: Copy=()>
```
for legibility reasons.
the test case i added here is derived from [this reproduction](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0bf3ace9f2a183d0bdbd748c6b8e3971):
```
struct B<T: Copy> {
t: T
}
trait A<T = ()> {
fn returns_constrained_type(&self, t: T) -> B<T> {
B { t }
}
}
```
where the suggested fix,
```
trait A<T = ()>, T: Copy { ... }
```
is in fact invalid syntax!
i also found an error in the existing suggestion for `trait Base<T = String>: Super<T>` where rustc would suggest `trait Base<T = String>: Super<T>, T: Copy`, but `T: Copy` is the first of the trait's `where` clauses and should be `where T: Copy` as well. the test for that suggestion expects invalid syntax, and has been revised to a compiler-pleasing `trait Base<T = String>: Super<T> where T: Copy`.
judging by https://github.com/rust-lang/rust/pull/70009 i'll.. cc @estebank ?
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/ui/trait-impl-bound-suggestions.fixed | 20 | ||||
| -rw-r--r-- | src/test/ui/trait-impl-bound-suggestions.rs | 20 | ||||
| -rw-r--r-- | src/test/ui/trait-impl-bound-suggestions.stderr | 17 | ||||
| -rw-r--r-- | src/test/ui/type/type-check-defaults.stderr | 4 |
4 files changed, 59 insertions, 2 deletions
diff --git a/src/test/ui/trait-impl-bound-suggestions.fixed b/src/test/ui/trait-impl-bound-suggestions.fixed new file mode 100644 index 00000000000..db3a95f5c4f --- /dev/null +++ b/src/test/ui/trait-impl-bound-suggestions.fixed @@ -0,0 +1,20 @@ +// run-rustfix + +#[allow(unused)] +use std::fmt::Debug; +// Rustfix should add this, or use `std::fmt::Debug` instead. + +#[allow(dead_code)] +struct ConstrainedStruct<X: Copy> { + x: X +} + +#[allow(dead_code)] +trait InsufficientlyConstrainedGeneric<X=()> where X: Copy { + fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct<X> { + //~^ ERROR the trait bound `X: Copy` is not satisfied + ConstrainedStruct { x } + } +} + +pub fn main() { } diff --git a/src/test/ui/trait-impl-bound-suggestions.rs b/src/test/ui/trait-impl-bound-suggestions.rs new file mode 100644 index 00000000000..bf75175179e --- /dev/null +++ b/src/test/ui/trait-impl-bound-suggestions.rs @@ -0,0 +1,20 @@ +// run-rustfix + +#[allow(unused)] +use std::fmt::Debug; +// Rustfix should add this, or use `std::fmt::Debug` instead. + +#[allow(dead_code)] +struct ConstrainedStruct<X: Copy> { + x: X +} + +#[allow(dead_code)] +trait InsufficientlyConstrainedGeneric<X=()> { + fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct<X> { + //~^ ERROR the trait bound `X: Copy` is not satisfied + ConstrainedStruct { x } + } +} + +pub fn main() { } diff --git a/src/test/ui/trait-impl-bound-suggestions.stderr b/src/test/ui/trait-impl-bound-suggestions.stderr new file mode 100644 index 00000000000..3a21e9c6b2a --- /dev/null +++ b/src/test/ui/trait-impl-bound-suggestions.stderr @@ -0,0 +1,17 @@ +error[E0277]: the trait bound `X: Copy` is not satisfied + --> $DIR/trait-impl-bound-suggestions.rs:14:52 + | +LL | struct ConstrainedStruct<X: Copy> { + | ---- required by this bound in `ConstrainedStruct` +... +LL | fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct<X> { + | ^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `X` + | +help: consider further restricting type parameter `X` + | +LL | trait InsufficientlyConstrainedGeneric<X=()> where X: Copy { + | ^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/type/type-check-defaults.stderr b/src/test/ui/type/type-check-defaults.stderr index fa6f3422410..d8c7f595e62 100644 --- a/src/test/ui/type/type-check-defaults.stderr +++ b/src/test/ui/type/type-check-defaults.stderr @@ -56,8 +56,8 @@ LL | trait Base<T = String>: Super<T> { } | help: consider further restricting type parameter `T` | -LL | trait Base<T = String>: Super<T>, T: Copy { } - | ^^^^^^^^^ +LL | trait Base<T = String>: Super<T> where T: Copy { } + | ^^^^^^^^^^^^^ error[E0277]: cannot add `u8` to `i32` --> $DIR/type-check-defaults.rs:24:66 |
