Skip to content

Misleading/Incomplete errors involving Pattern #90970

New issue

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

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

Already on GitHub? # to your account

Closed
glandium opened this issue Nov 17, 2021 · 5 comments · Fixed by #91873
Closed

Misleading/Incomplete errors involving Pattern #90970

glandium opened this issue Nov 17, 2021 · 5 comments · Fixed by #91873
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

glandium commented Nov 17, 2021

Given the following code:

pub fn strip_lf(s: &str) -> &str {
    s.strip_suffix(b'\n').unwrap_or(s)
}

The current output is:

error[E0277]: expected a `FnMut<(char,)>` closure, found `u8`
 --> src/lib.rs:2:20
  |
2 |     s.strip_suffix(b'\n').unwrap_or(s)
  |                    ^^^^^ expected an `FnMut<(char,)>` closure, found `u8`
  |
  = help: the trait `FnMut<(char,)>` is not implemented for `u8`
  = note: required because of the requirements on the impl of `Pattern<'_>` for `u8`

Pattern<'a> has implementations for:

  • char
  • &str
  • &String
  • &[char]
  • &&str
  • F where F: FnMut(char) -> bool

While it is true that u8 doesn't match any of the non-generic impls, and that it doesn't have an impl for FnMut<(char,)> -> bool, it's sad that the error message doesn't mention that those other possibilities exist.
(Also, the error message doesn't mention the closure return type)

@glandium glandium 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 Nov 17, 2021
@JakobDegen
Copy link
Contributor

@rustbot claim

@estebank
Copy link
Contributor

Linking to the Zulip discussion for the record.

@estebank estebank added A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Nov 18, 2021
@JakobDegen
Copy link
Contributor

I thought about this a bunch, and don't really know the best solution here. Happy to let someone else try

@taladar
Copy link

taladar commented Dec 23, 2021

I encountered a related issue today with the pattern parameter on replace

fn main() {
    let s1: String = "Foo".to_string();
    let s2: String = "Bar".to_string();
    let mut s: String = "FooBarBaz".to_string();
    s.replace(s1, &s2);
    format!("Replacement result: {}", s);
}

(no & for s1 in the replace call) results in the error:

   Compiling compiler-bug-test v0.1.0 (/home/taladar/temp/20211223/compiler-bug-test)
error[E0277]: expected a `FnMut<(char,)>` closure, found `String`
 --> src/main.rs:5:7
  |
5 |     s.replace(s1, &s2);
  |       ^^^^^^^ expected an `FnMut<(char,)>` closure, found `String`
  |
  = help: the trait `FnMut<(char,)>` is not implemented for `String`
  = note: required because of the requirements on the impl of `Pattern<'_>` for `String`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `compiler-bug-test` due to previous error

which is misleading because it marks "replace" instead of the first parameter in addition to the issue mentioned above.

@edmorley
Copy link
Contributor

See also #87437 (which may be a dupe of this or vice versa, unless I'm misunderstanding).

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 ac8cbbd Apr 5, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

5 participants