Skip to content

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

Closed
@ben0x539

Description

@ben0x539

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! ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions