Skip to content

"note: method exists but trait bounds not satisfied" could be better #36513

Closed
@durka

Description

@durka

I saw this broken code on IRC:

pub trait JoinWithString { 
  fn join(self, separator: &'static str) -> String; 
} 

impl<X> JoinWithString for X 
  where X: Iterator<Item = String> 
{ 
  fn join(self, separator: &'static str) -> String { 
    unimplemented!()
  } 
}


fn main() {
    let a: Vec<std::path::PathBuf> = Vec::new();
    a.iter().map(|x| x.to_str().unwrap()).join(":");

}

I know why it's broken, but I don't like the error message:

error: no method named `join` found for type `std::iter::Map<std::slice::Iter<'_, std::path::PathBuf>, [closure@<anon>:16:18: 16:41]>` in the current scope
  --> <anon>:16:43
   |>
16 |>     a.iter().map(|x| x.to_str().unwrap()).join(":");
   |>                                           ^^^^
note: the method `join` exists but the following trait bounds were not satisfied: `&std::iter::Map<std::slice::Iter<'_, std::path::PathBuf>, [closure@<anon>:16:18: 16:41]> : std::iter::Iterator`
help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `join`, perhaps you need to implement it:
help: candidate #1: `JoinWithString`
  • The note just told me that the problem is the trait bounds, so it is superfluous to suggest importing a trait (even without the note, it doesn't need to be imported so the help is useless).
  • The note says Iterator is not satisfied, which is not helpful. The problem is that Iterator<Item=String> is not satisfied.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.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