Skip to content

Type errors with for and do expressions could be cleverer #3651

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

Closed
ben0x539 opened this issue Oct 4, 2012 · 5 comments
Closed

Type errors with for and do expressions could be cleverer #3651

ben0x539 opened this issue Oct 4, 2012 · 5 comments
Labels
A-type-system Area: Type system
Milestone

Comments

@ben0x539
Copy link
Contributor

ben0x539 commented Oct 4, 2012

Calling functions meant to be used in for/do expressions in the respective other kind of expression gives type errors. While that's correct, it's not immediately clear where the error is (not even the squiggly lines under the code excerpts are very clear), and I think it should be yelling at me directly for using the wrong syntax sugar instead of outlining how it noticed that I screwed up. Here is some examples that I found a bit misleading:

foo.rs:2:14: 2:36 error: a `loop` function's last argument should return `bool`, not `()`
foo.rs:2     for task::spawn { return true; }
                       ^~~~~~~~~~~~~~~~~~~~~~

I'm returning a bool, what's the matter! I expect rustc is pointing at the type of task::spawn here even though my closure is underlined and the mismatch is really between for and the function type.

Similarly, in the inverse case,

foo.rs:2:15: 2:17 error: mismatched types: expected `bool` but found `()` (expected bool but found ())
foo.rs:2     do 5.times {}
                        ^~

Well, okay then, let me just return true...

foo.rs:2:24: 2:28 error: mismatched types: expected `()` but found `bool` (expected () but found bool)
foo.rs:2     do 5.times { return true; }
                                 ^~~~

Argh! Which is it! ;-)

@ben0x539
Copy link
Contributor Author

ben0x539 commented Dec 9, 2012

I assume this got handled in #4141.

@ben0x539 ben0x539 closed this as completed Dec 9, 2012
@ben0x539
Copy link
Contributor Author

ben0x539 commented Dec 9, 2012

Looking closer, I think the first example could still do with mentioning that I should use do. Or maybe I'm too thick-headed for the loop protocol.

I still feel a bit weird that do 5.times { false } compiles, too.

@ben0x539 ben0x539 reopened this Dec 9, 2012
@catamorphism
Copy link
Contributor

I'll take a look at this.

@ghost ghost assigned catamorphism Dec 10, 2012
@catamorphism
Copy link
Contributor

There are definitely a couple things to fix here. First, the error message itself verges on misleading. In my branch, I've improved it quite a bit. Second, there are multiple errors. This is a case where my work on suppressing derived errors wasn't doing the trick; I've extended it a bit, but the output still doesn't look as good as I'd hope. Third, the spans (which are what tell the pretty-printer where to put the squiggly lines) are wrong. (I have no idea why).

I'm working on it!

catamorphism added a commit to catamorphism/rust that referenced this issue Jan 24, 2013
As per rust-lang#3651, print out a clearer error message when a `for` gets
used with the wrong type of iterator. Also fix spans on `for` loop
bodies, and suppress some more derived errors.
catamorphism added a commit that referenced this issue Jan 24, 2013
Print out a clearer error message when a `for` gets
used with the wrong type of iterator. Also fix spans on `for` loop
bodies, and suppress some more derived errors.

r=brson

Closes #3651
@catamorphism
Copy link
Contributor

Fixed in 5b64c79 -- please start a new issue if you find other confusing error messages like this! I love improving error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants