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.
This commit is contained in:
parent
dd33766e4a
commit
0eac38b7a6
5 changed files with 105 additions and 22 deletions
20
src/test/ui/trait-impl-bound-suggestions.fixed
Normal file
20
src/test/ui/trait-impl-bound-suggestions.fixed
Normal file
|
|
@ -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() { }
|
||||
20
src/test/ui/trait-impl-bound-suggestions.rs
Normal file
20
src/test/ui/trait-impl-bound-suggestions.rs
Normal file
|
|
@ -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() { }
|
||||
17
src/test/ui/trait-impl-bound-suggestions.stderr
Normal file
17
src/test/ui/trait-impl-bound-suggestions.stderr
Normal file
|
|
@ -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`.
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue