Description
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:
- Only emit all type errors that are not depended on other type errors (suppressing the superfluous errors)
- Emit all errors, but in the order of their dependencies so that the actual relevant errors are always the first ones.
- Emit all errors, but mark all that have any dependencies with a note like "This might be a spurious error"
Activity
Kimundi commentedon Nov 29, 2014
cc @nikomatsakis
nham commentedon Jul 1, 2015
I'm not able to reproduce the error messages described above. In fact, the following code currently compiles on nightly:
bluss commentedon Jul 1, 2015
Yes
split(&str)
is now implemented. If we try to split by a type that's not supported, the errors look a bit different:The new version seems a bit better, with the note!
bluss commentedon Jul 1, 2015
I don't understand why the closure traits are “privileged” in that they are explicitly mentioned.
Kimundi commentedon Jul 2, 2015
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 commentedon Jan 3, 2017
Triage: @bluss's example with the new format
can this be closed?
nikomatsakis commentedon Jan 4, 2017
I think we can close, but I'm having a hard time understanding exactly what @Kimundi was trying to highlight in their example.
estebank commentedon Jan 28, 2018
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:
estebank commentedon Oct 15, 2018
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 commentedon Oct 11, 2019
Current output:
estebank commentedon Jul 13, 2020
Current output:
estebank commentedon Jun 8, 2022
Current output:
estebank commentedon Aug 16, 2024
Current output: