-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Comments
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)
} |
Both |
This misleading diagnostic also caught me out too recently. In my case the usage was virtually identical to the OP's: I also found #90970 which seems related. |
Addressing in #91873 |
…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.
…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.
" This reverts commit 883b93c.
This reverts commit ac8cbbd.
…bank E0277: suggest dereferencing function arguments in more cases This unifies and generalizes some of the logic in `TypeErrCtxt::suggest_dereferences` so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases. Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom `Deref` impls when passing by-reference, and one specifically to catch rust-lang#87437). I've based the new checks loosely on what's done for `E0308` in `FnCtxt::suggest_deref_or_ref`: it will suggest dereferences to satisfy trait bounds whenever the referent is `Copy`, is boxed (& so can be moved out of the boxes), or is being passed by reference. This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of `&`-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step. Closes rust-lang#90997
…bank E0277: suggest dereferencing function arguments in more cases This unifies and generalizes some of the logic in `TypeErrCtxt::suggest_dereferences` so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases. Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom `Deref` impls when passing by-reference, and one specifically to catch rust-lang#87437). I've based the new checks loosely on what's done for `E0308` in `FnCtxt::suggest_deref_or_ref`: it will suggest dereferences to satisfy trait bounds whenever the referent is `Copy`, is boxed (& so can be moved out of the boxes), or is being passed by reference. This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of `&`-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step. Closes rust-lang#90997
Rollup merge of rust-lang#133292 - dianne:e0277-suggest-deref, r=estebank E0277: suggest dereferencing function arguments in more cases This unifies and generalizes some of the logic in `TypeErrCtxt::suggest_dereferences` so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases. Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom `Deref` impls when passing by-reference, and one specifically to catch rust-lang#87437). I've based the new checks loosely on what's done for `E0308` in `FnCtxt::suggest_deref_or_ref`: it will suggest dereferences to satisfy trait bounds whenever the referent is `Copy`, is boxed (& so can be moved out of the boxes), or is being passed by reference. This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of `&`-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step. Closes rust-lang#90997
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
The current output is:
Ideally the output should not lead with "expected an Fn" and also help me to add a missing ampersand.
The text was updated successfully, but these errors were encountered: