Skip to content

Suggestions for refining impl Trait argument are invalid Rust code #69638

Closed
@jdm

Description

@jdm
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

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions