Skip to content
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

better mismatched types error message #3417

Closed
jesse99 opened this issue Sep 7, 2012 · 11 comments
Closed

better mismatched types error message #3417

jesse99 opened this issue Sep 7, 2012 · 11 comments
Assignees
Labels
A-typesystem Area: The type system P-low Low priority

Comments

@jesse99
Copy link
Contributor

jesse99 commented Sep 7, 2012

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
@lkuper
Copy link
Contributor

lkuper commented Sep 7, 2012

+1, except: is the multi-line version preferable even when the type names are short?

@bproctor
Copy link

bproctor commented Sep 7, 2012

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

@jesse99
Copy link
Contributor Author

jesse99 commented Sep 7, 2012

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.

@jesse99
Copy link
Contributor Author

jesse99 commented Sep 7, 2012

+1 for brson's version.

@ghost ghost assigned nikomatsakis Sep 13, 2012
@metajack
Copy link
Contributor

I assume that @jesse99 meant @bproctor. I agree that error message is nicer.

@pnkfelix
Copy link
Member

accepted for P-low

@nham
Copy link
Contributor

nham commented Aug 6, 2014

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?

@nikomatsakis
Copy link
Contributor

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.

@blaenk
Copy link
Contributor

blaenk commented Nov 12, 2014

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.

@nagisa
Copy link
Member

nagisa commented Feb 28, 2015

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

@huonw
Copy link
Member

huonw commented Sep 26, 2015

Closing per @nagisa's comment. Thanks everyone!

@huonw huonw closed this as completed Sep 26, 2015
bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
RalfJung pushed a commit to RalfJung/rust that referenced this issue Mar 31, 2024
…lfJung

we have to ignore RUSTC_WORKSPACE_WRAPPER as well

This matches what we do with RUSTC_WRAPPER.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system P-low Low priority
Projects
None yet
Development

No branches or pull requests

10 participants