-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #99741 - compiler-errors:copy-impl-impl-generics, r=f…
…ee1-dead Use `impl`'s generics when suggesting fix on bad `impl Copy` See the UI test for a more complicated example, but we weren't correctly suggesting to add bounds given a manual `impl` whose generics didn't match the struct generics. ```rust #[derive(Clone)] struct Wrapper<T>(T); impl<S> Copy for Wrapper<S> {} ``` Coincidentally this fix didn't cause any regressions for `derive(Copy)` impls, I think because those use the same spans in the impl generics as the struct generics, so the machinery still applies the same change.
- Loading branch information
Showing
7 changed files
with
103 additions
and
18 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
19 changes: 19 additions & 0 deletions
19
src/test/ui/suggestions/missing-bound-in-manual-copy-impl-2.fixed
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,19 @@ | ||
// run-rustfix | ||
|
||
#[derive(Clone)] | ||
struct Wrapper<T>(T); | ||
|
||
struct OnlyCopyIfDisplay<T>(std::marker::PhantomData<T>); | ||
|
||
impl<T: std::fmt::Display> Clone for OnlyCopyIfDisplay<T> { | ||
fn clone(&self) -> Self { | ||
OnlyCopyIfDisplay(std::marker::PhantomData) | ||
} | ||
} | ||
|
||
impl<T: std::fmt::Display> Copy for OnlyCopyIfDisplay<T> {} | ||
|
||
impl<S: std::fmt::Display> Copy for Wrapper<OnlyCopyIfDisplay<S>> {} | ||
//~^ ERROR the trait `Copy` may not be implemented for this type | ||
|
||
fn main() {} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/suggestions/missing-bound-in-manual-copy-impl-2.rs
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,19 @@ | ||
// run-rustfix | ||
|
||
#[derive(Clone)] | ||
struct Wrapper<T>(T); | ||
|
||
struct OnlyCopyIfDisplay<T>(std::marker::PhantomData<T>); | ||
|
||
impl<T: std::fmt::Display> Clone for OnlyCopyIfDisplay<T> { | ||
fn clone(&self) -> Self { | ||
OnlyCopyIfDisplay(std::marker::PhantomData) | ||
} | ||
} | ||
|
||
impl<T: std::fmt::Display> Copy for OnlyCopyIfDisplay<T> {} | ||
|
||
impl<S> Copy for Wrapper<OnlyCopyIfDisplay<S>> {} | ||
//~^ ERROR the trait `Copy` may not be implemented for this type | ||
|
||
fn main() {} |
22 changes: 22 additions & 0 deletions
22
src/test/ui/suggestions/missing-bound-in-manual-copy-impl-2.stderr
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,22 @@ | ||
error[E0204]: the trait `Copy` may not be implemented for this type | ||
--> $DIR/missing-bound-in-manual-copy-impl-2.rs:16:9 | ||
| | ||
LL | struct Wrapper<T>(T); | ||
| - this field does not implement `Copy` | ||
... | ||
LL | impl<S> Copy for Wrapper<OnlyCopyIfDisplay<S>> {} | ||
| ^^^^ | ||
| | ||
note: the `Copy` impl for `OnlyCopyIfDisplay<S>` requires that `S: std::fmt::Display` | ||
--> $DIR/missing-bound-in-manual-copy-impl-2.rs:4:19 | ||
| | ||
LL | struct Wrapper<T>(T); | ||
| ^ | ||
help: consider restricting type parameter `S` | ||
| | ||
LL | impl<S: std::fmt::Display> Copy for Wrapper<OnlyCopyIfDisplay<S>> {} | ||
| +++++++++++++++++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0204`. |
9 changes: 9 additions & 0 deletions
9
src/test/ui/suggestions/missing-bound-in-manual-copy-impl.fixed
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,9 @@ | ||
// run-rustfix | ||
|
||
#[derive(Clone)] | ||
struct Wrapper<T>(T); | ||
|
||
impl<S: Copy> Copy for Wrapper<S> {} | ||
//~^ ERROR the trait `Copy` may not be implemented for this type | ||
|
||
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,9 @@ | ||
// run-rustfix | ||
|
||
#[derive(Clone)] | ||
struct Wrapper<T>(T); | ||
|
||
impl<S> Copy for Wrapper<S> {} | ||
//~^ ERROR the trait `Copy` may not be implemented for this type | ||
|
||
fn main() {} |
17 changes: 17 additions & 0 deletions
17
src/test/ui/suggestions/missing-bound-in-manual-copy-impl.stderr
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[E0204]: the trait `Copy` may not be implemented for this type | ||
--> $DIR/missing-bound-in-manual-copy-impl.rs:6:9 | ||
| | ||
LL | struct Wrapper<T>(T); | ||
| - this field does not implement `Copy` | ||
LL | | ||
LL | impl<S> Copy for Wrapper<S> {} | ||
| ^^^^ | ||
| | ||
help: consider restricting type parameter `S` | ||
| | ||
LL | impl<S: Copy> Copy for Wrapper<S> {} | ||
| ++++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0204`. |