Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using str::contains inside of a filter leads to confusing error message. #87437

Closed
ethercrow opened this issue Jul 24, 2021 · 4 comments · Fixed by #91873
Closed

Using str::contains inside of a filter leads to confusing error message. #87437

ethercrow opened this issue Jul 24, 2021 · 4 comments · Fixed by #91873
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ethercrow
Copy link

I've been trying to learn some Rust by doing CodeWars. I've spent some time being stuck on this diagnostic. One of the easiest exercises there is to count vowels in a string. First I did it with a loop, but then got stuck trying to rewrite is as a filter.

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=785ded94c58b6d9a3a8acd16145a4970

fn get_vowel_count(string: &str) -> usize {
        string
        .chars()
        .filter(|c| "aeiou".contains(c))
        .count()
}

The current output is:

error[E0277]: expected a `Fn<(char,)>` closure, found `char`
 --> src/lib.rs:4:38
  |
4 |         .filter(|c| "aeiou".contains(c))
  |                                      ^ expected an `Fn<(char,)>` closure, found `char`
  |
  = help: the trait `Fn<(char,)>` is not implemented for `char`
  = note: required because of the requirements on the impl of `FnOnce<(char,)>` for `&char`
  = note: required because of the requirements on the impl of `Pattern<'_>` for `&char`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `challenge`

Ideally the output should not lead with "expected an Fn" and also help me to add a missing ampersand.

@ethercrow ethercrow added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 24, 2021
@osa1
Copy link
Contributor

osa1 commented Aug 14, 2021

and also help me to add a missing ampersand.

Do you mean a missing star? Working version:

fn get_vowel_count(string: &str) -> usize {
        string
        .chars()
        .filter(|c| "aeiou".contains(*c))
        .count()
}

I agree that the error message is very misleading.

Smaller repro:

fn test(c: &char) -> bool {
    "aeiou".contains(c)
}

@ethercrow
Copy link
Author

ethercrow commented Aug 14, 2021

Do you mean a missing star?

Both |c| "aeiou".contains(*c) and |&c| "aeiou".contains(c) are accepted. At the time of writing the ticket I found only the latter way to fix the error, that's why I said ampersand.

@edmorley
Copy link
Contributor

edmorley commented Feb 16, 2022

This misleading diagnostic also caught me out too recently.

In my case the usage was virtually identical to the OP's:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=43fbdf70f0270c762d0558fdb34ae1f3

I also found #90970 which seems related.

@estebank
Copy link
Contributor

Addressing in #91873

Screen Shot 2022-03-26 at 3 38 34 PM

estebank added a commit to estebank/rust that referenced this issue Apr 4, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 4, 2022
…ied-trait, r=davidtwco

Mention implementers of unsatisfied trait

When encountering an unsatisfied trait bound, if there are no other
suggestions, mention all the types that *do* implement that trait:

```
error[E0277]: the trait bound `f32: Foo` is not satisfied
  --> $DIR/impl_wf.rs:22:6
   |
LL | impl Baz<f32> for f32 { }
   |      ^^^^^^^^ the trait `Foo` is not implemented for `f32`
   |
   = help: the trait `Foo` is implemented for `i32`
note: required by a bound in `Baz`
  --> $DIR/impl_wf.rs:18:31
   |
LL | trait Baz<U: ?Sized> where U: Foo { }
   |                               ^^^ required by this bound in `Baz`
```
```
error[E0277]: the trait bound `u32: Foo` is not satisfied
  --> $DIR/associated-types-path-2.rs:29:5
   |
LL |     f1(2u32, 4u32);
   |     ^^ the trait `Foo` is not implemented for `u32`
   |
   = help: the trait `Foo` is implemented for `i32`
note: required by a bound in `f1`
  --> $DIR/associated-types-path-2.rs:13:14
   |
LL | pub fn f1<T: Foo>(a: T, x: T::A) {}
   |              ^^^ required by this bound in `f1`
```

Suggest dereferencing in more cases.

Fix rust-lang#87437, fix rust-lang#90970.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 4, 2022
…ied-trait, r=davidtwco

Mention implementers of unsatisfied trait

When encountering an unsatisfied trait bound, if there are no other
suggestions, mention all the types that *do* implement that trait:

```
error[E0277]: the trait bound `f32: Foo` is not satisfied
  --> $DIR/impl_wf.rs:22:6
   |
LL | impl Baz<f32> for f32 { }
   |      ^^^^^^^^ the trait `Foo` is not implemented for `f32`
   |
   = help: the trait `Foo` is implemented for `i32`
note: required by a bound in `Baz`
  --> $DIR/impl_wf.rs:18:31
   |
LL | trait Baz<U: ?Sized> where U: Foo { }
   |                               ^^^ required by this bound in `Baz`
```
```
error[E0277]: the trait bound `u32: Foo` is not satisfied
  --> $DIR/associated-types-path-2.rs:29:5
   |
LL |     f1(2u32, 4u32);
   |     ^^ the trait `Foo` is not implemented for `u32`
   |
   = help: the trait `Foo` is implemented for `i32`
note: required by a bound in `f1`
  --> $DIR/associated-types-path-2.rs:13:14
   |
LL | pub fn f1<T: Foo>(a: T, x: T::A) {}
   |              ^^^ required by this bound in `f1`
```

Suggest dereferencing in more cases.

Fix rust-lang#87437, fix rust-lang#90970.
@bors bors closed this as completed in 883b93c Apr 5, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants