forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#76695 - iximeow:trait-generic-bound-suggest…
…ion, 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 rust-lang#70009 i'll.. cc @estebank ?
- Loading branch information
Showing
5 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters