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#69707 - estebank:impl-trait-missing-bounds,…
… r=Centril Handle `impl Trait` where `Trait` has an assoc type with missing bounds When encountering a type parameter that needs more bounds the trivial case is `T` `where T: Bound`, but it can also be an `impl Trait` param that needs to be decomposed to a type param for cleaner code. For example, given ```rust fn foo(constraints: impl Iterator) { for constraint in constraints { println!("{:?}", constraint); } } ``` the previous output was ``` error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` --> src/main.rs:3:26 | 1 | fn foo(constraints: impl Iterator) { | - help: consider further restricting the associated type: `where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug` 2 | for constraint in constraints { 3 | println!("{:?}", constraint); | ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug` | = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` = note: required by `std::fmt::Debug::fmt` ``` which is incorrect as `where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug` is not valid syntax nor would it restrict the positional `impl Iterator` parameter if it were. The output being introduced is ``` error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` --> src/main.rs:3:26 | 3 | println!("{:?}", constraint); | ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug` | = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` = note: required by `std::fmt::Debug::fmt` help: introduce a type parameter with a trait bound instead of using `impl Trait` | LL | fn foo<T: Iterator>(constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug { | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` This suggestion is correct and lead the user in the right direction: because you have an associated type restriction you can no longer use `impl Trait`, the only reasonable alternative is to introduce a named type parameter, bound by `Trait` and with a `where` binding on the associated type for the new type parameter `as Trait` for the missing bound. *Ideally*, we would want to suggest something like the following, but that is not valid syntax today ``` error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` --> src/main.rs:3:26 | 3 | println!("{:?}", constraint); | ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug` | = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` = note: required by `std::fmt::Debug::fmt` help: introduce a type parameter with a trait bound instead of using `impl Trait` | LL | fn foo(constraints: impl Iterator<Item: std::fmt::Debug>) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` Fix rust-lang#69638.
- Loading branch information
Showing
7 changed files
with
304 additions
and
50 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
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
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,44 @@ | ||
// The double space in `impl Iterator` is load bearing! We want to make sure we don't regress by | ||
// accident if the internal string representation changes. | ||
#[rustfmt::skip] | ||
fn foo(constraints: impl Iterator) { | ||
for constraint in constraints { | ||
qux(constraint); | ||
//~^ ERROR `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` | ||
} | ||
} | ||
|
||
fn bar<T>(t: T, constraints: impl Iterator) where T: std::fmt::Debug { | ||
for constraint in constraints { | ||
qux(t); | ||
qux(constraint); | ||
//~^ ERROR `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` | ||
} | ||
} | ||
|
||
fn baz(t: impl std::fmt::Debug, constraints: impl Iterator) { | ||
for constraint in constraints { | ||
qux(t); | ||
qux(constraint); | ||
//~^ ERROR `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` | ||
} | ||
} | ||
|
||
fn bat<I, T: std::fmt::Debug>(t: T, constraints: impl Iterator, _: I) { | ||
for constraint in constraints { | ||
qux(t); | ||
qux(constraint); | ||
//~^ ERROR `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` | ||
} | ||
} | ||
|
||
fn bak(constraints: impl Iterator + std::fmt::Debug) { | ||
for constraint in constraints { | ||
qux(constraint); | ||
//~^ ERROR `<impl Iterator + std::fmt::Debug as std::iter::Iterator>::Item` doesn't implement | ||
} | ||
} | ||
|
||
fn qux(_: impl std::fmt::Debug) {} | ||
|
||
fn main() {} |
Oops, something went wrong.