Closed
Description
fn check(constraints: impl Iterator) {
for constraint in constraints {
println!("{:?}", constraint);
}
}
fn main() {}
This yields the error message:
error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
--> src/main.rs:3:26
|
1 | fn check(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`
error: aborting due to previous error
If I add where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug
to the function declaration, I receive:
error[E0667]: `impl Trait` is not allowed in path parameters
--> src/main.rs:2:8
|
2 | where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug
| ^^^^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> src/main.rs:2:8
|
2 | where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug
| ^^^^^^^^^^^^^
error: aborting due to 2 previous errors
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Category: This is a bug.Diagnostics: A structured suggestion resulting in incorrect code.Diagnostics: Confusing error or lint; hard to understand for new users.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
estebank commentedon Mar 2, 2020
cc #68982
estebank commentedon Mar 2, 2020
FWIW, the correct code would be
impl Trait
is only correct in fn arguments and the return type, and cannot be nested (impl Iterator<impl Display>
is invalid). When you seerustc
complaining about<_ as Foo>::Bar = Qux
it means you should have writtenFoo<Bar = Qux>
. Because here you have:
(any type that impls the trait that comes next) and not=
(only the type that comes next), and that can't be used to constrain the associated type (can't haveFoo<Bar: QuxTrait>
) you need to introduce a type parameter that will have that restriction and will be evaluated later:fn foo<Q: QuxTrait>(_: impl Foo<Bar = Q>) {}
.This is a bug that needs solving,
rustc
should have the same explanation I just gave when encountering these cases.Edit: alternatively, do not use
impl Trait
, which will give you a correct suggestion:A potential solution with the least amount of negative side-effects would be to discourage the use of
impl Trait
in arg position when having these kind of projection errors and suggest using a type param.impl Trait
whereTrait
has an assoc type with missing bounds #69707Rollup merge of rust-lang#69707 - estebank:impl-trait-missing-bounds,…
4 remaining items