Skip to content

Confusing E0284 "type annotations needed" when trait method bound trait bound conflict in associated type. #105513

@zachs18

Description

@zachs18

Given the following code (playground link):

pub trait Extension: IntoIterator<Item = u32> + Sized {
    fn test(self) -> Vec<u64>
    where
        Self: IntoIterator<Item = u64>,
    {
        self.into_iter().collect()
    }
}

(i.e. the bound on fn test conflicts with the bound of its trait by way of associated types)

The current output is:

error[E0284]: type annotations needed: cannot satisfy `<Self as IntoIterator>::Item == u32`
  |
note: required by a bound in `Extension`
 --> src/lib.rs:1:35
  |
1 | pub trait Extension: IntoIterator<Item = u32> + Sized {
  |                                   ^^^^^^^^^^ required by this bound in `Extension`

For more information about this error, try `rustc --explain E0284`.
error: could not compile `playground` due to previous error

Ideally the output should look like: something mentioning the conflicting bounds. (Or under #![feature(trivial_bounds)], maybe it could just be a trivially false bound?).

error[E0000]: conflicting bounds: cannot satisfy both `<Self as IntoIterator>::Item == u32` and `<Self as IntoIterator>::Item == u64`
  |
note: bounds in `Extension` and `fn test`
 --> src/lib.rs:1:35
  |
1 | pub trait Extension: IntoIterator<Item = u32> + Sized {
  |                                   ^^^^^^^^^^ required by this bound in `Extension`
4 |         Self: IntoIterator<Item = u64>,
  |                            ^^^^^^^^^^ required by this bound in `fn test`

error: could not compile `playground` due to previous error

Same result on stable, beta, and nightly.

Related: #103292, (can't find the issue at the moment, but impl<T: Trait<Assoc = u32>> SecondTrait for T conflicting with impl<T: Trait<Assoc = u64>> SecondTrait for T when it shouldn't is related).

@rustbot modify labels +A-traits +D-confusing +A-associated-items

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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