better mismatched types error message
These are great error messages but most of the time they're quite hard to read because they are so long. They would be much more readable if they were six lines instead of one line.
So instead of
src/configuration.rs:179:54: 179:70 error: mismatched types: expected `@std::map::chained::hashmap_<@~str,mustache::mustache::Data>` but found `@std::map::chained::hashmap_<~str,mustache::mustache::Data>` (expected @-ptr but found ~str)
something like
src/configuration.rs:179:54: 179:70 error: mismatched types
expected:
`@std::map::chained::hashmap_<@~str,mustache::mustache::Data>`
but found:
`@std::map::chained::hashmap_<~str,mustache::mustache::Data>`
expected @-ptr but found ~str
+1, except: is the multi-line version preferable even when the type names are short?
Another possible option?
src/configuration.rs:179:54: 179:70 error: mismatched types
expected: `@std::map::chained::hashmap_<@~str,mustache::mustache::Data>`
but found: `@std::map::chained::hashmap_<~str,mustache::mustache::Data>`
expected @-ptr but found ~str
Maybe not lkuper: I did see an error afterwards that looked fine. Perhaps a heuristic can be used to split the error into multiple lines.
Perhaps even a general heuristic that splits long error messages into multiple lines at places marked as line breakable.
+1 for brson's version.
I assume that @jesse99 meant @bproctor. I agree that error message is nicer.
accepted for P-low
visiting for triage. This is still a problem today. I'm interested in fixing this, but I have no idea what I'm doing. Does this fix only require changing every instance of
format!("mismatched types: expected `{}` but found {}", e, actual)
in librustc::middle::typeck::check::_match to something like:
format!("mismatched types:\n expected: `{}`\n but found: {}", e, actual)
? Or is it more complicated than that?
presumably we'd want to update the API to take in multiple parts. I'd like for it to be able to diff the strings and highlight the differences, as well.
I strongly believe this should be done by 1.0. Some of the type mismatch errors can get pretty ridiculous, especially because error messages don't use type aliases when they were used. Using type aliases in error messages probably entails a large undertaking in the compiler to make possible, but putting the expected and actual type on separate lines would be a very simple way to improve readability.
I can imagine that if a user gets a very long type mismatch error that is difficult to parse, it can elicit or increase the feeling that the compiler is being wrestled with, and that "the types are getting in the way," when instead the compiler is helping.
This is even more pronounced when using iterator types. Given a function which returns the keys of a hashmap field on a struct:
pub fn nodes(&self) -> Keys<'a, &T, HashSet<&T>> {
self.edges.keys()
}
If I change the return type from Keys<'a, &T, HashSet<&T>> to Keys<'a, &T, HashSet<T>> I get this ridiculous error message:
error: mismatched types: expected core::iter::Map<'static,(&'a &T,&'a std::collections::hashmap::set::HashSet
),&'a &T,std::collections::hashmap::map::Entries<'a,&T,std::collections::hashmap::set::HashSet >>, found core::iter::Map<'static,(&&'a T,&std::collections::hashmap::set::HashSet<&'a T>),&&'a T,std::collections::hashmap::map::Entries<'_,&'a T,std::collections::hashmap::set::HashSet<&'a T>>> (expected type parameter, found &-ptr)
The last parenthesized message, (expected type parameter, found &-ptr), is completely useless because it doesn't say what part of the type contains the inconsistency. It's also difficult to track down where the "expected" type ends and the "found" type begins in that mess. When I get messages like these I have to put them in vim and line them up to figure out what's inconsistent:
error: mismatched types: (expected type parameter, found &-ptr)
expected `core::iter::Map<'static,(&'a &T,&'a std::collections::hashmap::set::HashSet<T>),&'a &T,std::collections::hashmap::map::Entries<'a,&T,std::collections::hashmap::set::HashSet<T>>>`
found `core::iter::Map<'static,(&&'a T,&std::collections::hashmap::set::HashSet<&'a T>),&&'a T,std::collections::hashmap::map::Entries<'_,&'a T,std::collections::hashmap::set::HashSet<&'a T>>>`
Granted, the type is still long, but at least it's easier to spot the inconsistency.
For this particular error currently have
t2.rs:5:10: 5:23 error: mismatched types:
expected `collections::string::String`,
found `&'static str`
(expected struct `collections::string::String`,
found &-ptr) [E0308]
which I believe is something this issue wanted to get (though it is implemented in a very hacky way). I think this can be closed. cc @steveklabnik
Closing per @nagisa's comment. Thanks everyone!