Skip to content

Surpress, mark, or order superflous trait resolution type errors correctly #19406

Open
@Kimundi

Description

@Kimundi
Member

During a rusti session on IRC, a confusing class of type errors has been discovered. This piece of code:

fn main() {
    "1 2 3".split(" ").collect::<Vec<_>>()
}

Currently emits these two error messages:

figment_sketch.rs:2:13: 2:23 error: the trait `core::str::CharEq` is not implemented for the type `&str`
figment_sketch.rs:2     "1 2 3".split(" ").collect::<Vec<_>>()
                                ^~~~~~~~~~
figment_sketch.rs:2:24: 2:43 error: type `core::str::CharSplits<'_, &str>` does not implement any method in scope named `collect`
figment_sketch.rs:2     "1 2 3".split(" ").collect::<Vec<_>>()
                                           ^~~~~~~~~~~~~~~~~~~

Where &str does not implement CharEq and split() is defined as fn split<Sep: CharEq>(&self, s: Sep) -> CharSplit<, Sep>

Both are correct type errors, however the second error is confusing because its not he actual cause, AND it depends on the other type error existing, which IS the actual cause.
Furthermore, sometimes those two errors are emitted in a different order, making it even more confusing to understand.

The basic reasoning here is "&str does not implement CharEq" and "CharSplits<'_, &str> does not Implement something that provides collect() because the only candidate, Iterator, is only implemented if &str implements CharEq". However, the split invocation only typechecks if &str implements CharEq, and hence any type error on the return type becomes irrelevant, as the type would not be valid to begin with.

If possible, typecheck should construct a dependency graph between the type errors it encounters, so that for every pair of type errors a, b it is known if b depends on a.

Using this information, the error messages could be improved in a few ways:

  1. Only emit all type errors that are not depended on other type errors (suppressing the superfluous errors)
  2. Emit all errors, but in the order of their dependencies so that the actual relevant errors are always the first ones.
  3. Emit all errors, but mark all that have any dependencies with a note like "This might be a spurious error"

Activity

Kimundi

Kimundi commented on Nov 29, 2014

@Kimundi
MemberAuthor
nham

nham commented on Jul 1, 2015

@nham
Contributor

I'm not able to reproduce the error messages described above. In fact, the following code currently compiles on nightly:

fn main() {
    let _x = "1 2 3".split(" ").collect::<Vec<_>>();
}
bluss

bluss commented on Jul 1, 2015

@bluss
Member

Yes split(&str) is now implemented. If we try to split by a type that's not supported, the errors look a bit different:

 let v: Vec<_> = "1, 2, 3".split(b',').collect();
<anon>:2:43: 2:52 error: no method named `collect` found for type `core::str::Split<'_, u8>` in the current scope
<anon>:2     let v: Vec<_> = "1, 2, 3".split(b',').collect();
                                                   ^~~~~~~~~
<anon>:2:43: 2:52 note: the method `collect` exists but the following trait bounds were not satisfied: `u8 : core::str::pattern::Pattern<'static>`, `core::str::Split<'_, u8> : core::iter::Iterator`
<anon>:2:31: 2:42 error: the trait `core::ops::FnMut<(char,)>` is not implemented for the type `u8` [E0277]
<anon>:2     let v: Vec<_> = "1, 2, 3".split(b',').collect();
                                       ^~~~~~~~~~~
<anon>:2:31: 2:42 error: the trait `core::ops::FnOnce<(char,)>` is not implemented for the type `u8` [E0277]
<anon>:2     let v: Vec<_> = "1, 2, 3".split(b',').collect();

The new version seems a bit better, with the note!

bluss

bluss commented on Jul 1, 2015

@bluss
Member

I don't understand why the closure traits are “privileged” in that they are explicitly mentioned.

Kimundi

Kimundi commented on Jul 2, 2015

@Kimundi
MemberAuthor

I'm not sure if this means that the kinds of error messages that this issue are talking about are gone now, but that new NOTE definitely improves matters for this specific kind of type error.

steveklabnik

steveklabnik commented on Jan 3, 2017

@steveklabnik
Member

Triage: @bluss's example with the new format

error[E0277]: the trait bound `u8: std::ops::FnMut<(char,)>` is not satisfied
 --> <anon>:2:28
  |
2 |  let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                            ^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `u8`
  |
  = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `u8`

error[E0277]: the trait bound `u8: std::ops::FnOnce<(char,)>` is not satisfied
 --> <anon>:2:28
  |
2 |  let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                            ^^^^^ the trait `std::ops::FnOnce<(char,)>` is not implemented for `u8`
  |
  = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `u8`

error: no method named `collect` found for type `std::str::Split<'_, u8>` in the current scope
 --> <anon>:2:40
  |
2 |  let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                        ^^^^^^^
  |
  = note: the method `collect` exists but the following trait bounds were not satisfied: `u8 : std::str::pattern::Pattern`, `std::str::Split<'_, u8> : std::iter::Iterator`

can this be closed?

nikomatsakis

nikomatsakis commented on Jan 4, 2017

@nikomatsakis
Contributor

I think we can close, but I'm having a hard time understanding exactly what @Kimundi was trying to highlight in their example.

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 9, 2017
estebank

estebank commented on Jan 28, 2018

@estebank
Contributor

I believe the idea originally proposed is that only the first trait error should be emitted. In some way, the entire statement should be "poisoned" after encountering a trait bound error so that no other error is generated in that line, much like #46732 does for blocks that encounter a parse error.


For the record, the current output is shorter still:

error[E0277]: expected a `std::ops::FnMut<(char,)>` closure, found `u8`
 --> src/main.rs:2:32
  |
2 |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                ^^^^^ expected an `FnMut<(char,)>` closure, found `u8`
  |
  = help: the trait `std::ops::FnMut<(char,)>` is not implemented for `u8`
  = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `u8`

error[E0599]: no method named `collect` found for type `std::str::Split<'_, u8>` in the current scope
 --> src/main.rs:2:44
  |
2 |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                            ^^^^^^^
  |
  = note: the method `collect` exists but the following trait bounds were not satisfied:
          `std::str::Split<'_, u8> : std::iter::Iterator`
          `&mut std::str::Split<'_, u8> : std::iter::Iterator`

error: aborting due to 2 previous errors
estebank

estebank commented on Oct 15, 2018

@estebank
Contributor

I believe the ideal output would avoid emitting the E0599 which is a knock down effect of the E0277 error. That being said, I believe all we could do is always hide it (even if it would fail regardless). As things stand, I don't think we'll improve this diagnostic any time soon.

estebank

estebank commented on Oct 11, 2019

@estebank
Contributor

Current output:

error[E0277]: expected a `std::ops::FnMut<(char,)>` closure, found `u8`
 --> src/main.rs:2:38
  |
2 |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                      ^^^^ expected an `FnMut<(char,)>` closure, found `u8`
  |
  = help: the trait `std::ops::FnMut<(char,)>` is not implemented for `u8`
  = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `u8`

error[E0599]: no method named `collect` found for type `std::str::Split<'_, u8>` in the current scope
 --> src/main.rs:2:44
  |
2 |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                            ^^^^^^^ method not found in `std::str::Split<'_, u8>`
  |
  = note: the method `collect` exists but the following trait bounds were not satisfied:
          `&mut std::str::Split<'_, u8> : std::iter::Iterator`
          `std::str::Split<'_, u8> : std::iter::Iterator`
estebank

estebank commented on Jul 13, 2020

@estebank
Contributor

Current output:

error[E0277]: expected a `std::ops::FnMut<(char,)>` closure, found `u8`
 --> src/main.rs:2:38
  |
2 |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                      ^^^^ expected an `FnMut<(char,)>` closure, found `u8`
  |
  = help: the trait `std::ops::FnMut<(char,)>` is not implemented for `u8`
  = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `u8`

error[E0599]: no method named `collect` found for struct `std::str::Split<'_, u8>` in the current scope
   --> src/main.rs:2:44
    |
2   |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
    |                                            ^^^^^^^ method not found in `std::str::Split<'_, u8>`
    |
    = note: the method `collect` exists but the following trait bounds were not satisfied:
            `u8: std::str::pattern::Pattern<'_>`
            which is required by `std::str::Split<'_, u8>: std::iter::Iterator`
            `std::str::Split<'_, u8>: std::iter::Iterator`
            which is required by `&mut std::str::Split<'_, u8>: std::iter::Iterator`
estebank

estebank commented on Jun 8, 2022

@estebank
Contributor

Current output:

error[E0277]: expected a `FnMut<(char,)>` closure, found `u8`
 --> src/main.rs:2:38
  |
2 |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                ----- ^^^^ expected an `FnMut<(char,)>` closure, found `u8`
  |                                |
  |                                required by a bound introduced by this call
  |
  = help: the trait `FnMut<(char,)>` is not implemented for `u8`
  = help: the following other types implement trait `Pattern<'a>`:
            &'b String
            &'b [char; N]
            &'b [char]
            &'b str
            &'c &'b str
            [char; N]
            char
            pattern::MultiCharEqPattern<C>
  = note: required because of the requirements on the impl of `Pattern<'_>` for `u8`
note: required by a bound in `core::str::<impl str>::split`

error[E0599]: the method `collect` exists for struct `std::str::Split<'_, u8>`, but its trait bounds were not satisfied
 --> src/main.rs:2:44
  |
2 |        let v: Vec<_> = "1, 2, 3".split(b',').collect();
  |                                              ^^^^^^^ method cannot be called on `std::str::Split<'_, u8>` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `u8: Pattern<'_>`
          which is required by `std::str::Split<'_, u8>: Iterator`
          `std::str::Split<'_, u8>: Iterator`
          which is required by `&mut std::str::Split<'_, u8>: Iterator`
estebank

estebank commented on Aug 16, 2024

@estebank
Contributor

Current output:

error[E0277]: the trait bound `u8: Pattern` is not satisfied
    --> src/main.rs:2:38
     |
2    |      let v: Vec<_> = "1, 2, 3".split(b',').collect();
     |                                ----- ^^^^ the trait `FnMut(char)` is not implemented for `u8`, which is required by `u8: Pattern`
     |                                |
     |                                required by a bound introduced by this call
     |
     = help: the following other types implement trait `Pattern`:
               &'b String
               &'b [char; N]
               &'b [char]
               &'b str
               &'c &'b str
               [char; N]
               char
     = note: required for `u8` to implement `Pattern`
note: required by a bound in `core::str::<impl str>::split`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:1400:21
     |
1400 |     pub fn split<P: Pattern>(&self, pat: P) -> Split<'_, P> {
     |                     ^^^^^^^ required by this bound in `core::str::<impl str>::split`

error[E0599]: the method `collect` exists for struct `Split<'_, u8>`, but its trait bounds were not satisfied
   --> src/main.rs:2:44
    |
2   |        let v: Vec<_> = "1, 2, 3".split(b',').collect();
    |                                              ^^^^^^^ method cannot be called on `Split<'_, u8>` due to unsatisfied trait bounds
    |
   ::: /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/iter.rs:778:1
    |
778 | / generate_pattern_iterators! {
779 | |     forward:
780 | |         /// Created with the method [`split`].
781 | |         ///
...   |
793 | |     delegate double ended;
794 | | }
    | |_- doesn't satisfy `std::str::Split<'_, u8>: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `u8: Pattern`
            which is required by `std::str::Split<'_, u8>: Iterator`
            `std::str::Split<'_, u8>: Iterator`
            which is required by `&mut std::str::Split<'_, u8>: Iterator`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.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

        Participants

        @steveklabnik@nikomatsakis@nham@sfackler@estebank

        Issue actions

          Surpress, mark, or order superflous trait resolution type errors correctly · Issue #19406 · rust-lang/rust