Skip to content

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

Closed
@jdm

Description

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

Activity

added
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
on Mar 2, 2020
added
A-diagnosticsArea: Messages for errors, warnings, and lints
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 2, 2020
added
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Mar 2, 2020
estebank

estebank commented on Mar 2, 2020

@estebank
Contributor
estebank

estebank commented on Mar 2, 2020

@estebank
Contributor

FWIW, the correct code would be

fn check<D: std::fmt::Debug>(constraints: impl Iterator<Item = D>) {
    for constraint in constraints {
        println!("{:?}", constraint);
    }
}

impl Trait is only correct in fn arguments and the return type, and cannot be nested (impl Iterator<impl Display> is invalid). When you see rustc complaining about <_ as Foo>::Bar = Qux it means you should have written Foo<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 have Foo<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:

fn check<I: Iterator>(constraints: I) where <I as Iterator>::Item: std::fmt::Debug {
    for constraint in constraints {
        println!("{:?}", constraint);
    }
}

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.

added
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
on Mar 2, 2020
added a commit that references this issue on Apr 10, 2020

Rollup merge of rust-lang#69707 - estebank:impl-trait-missing-bounds,…

6e49b91

4 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @jdm@estebank@jonas-schievink

      Issue actions

        Suggestions for refining `impl Trait` argument are invalid Rust code · Issue #69638 · rust-lang/rust