Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Confusing error message when missing an else in an if expression #4201

Closed
ghost opened this Issue · 9 comments

8 participants

@ghost
fn main() {
    let a = if true {
        0 
    } else if false {
        1
    };
}
./example.rs:4:20: 6:5 error: mismatched types: expected `()` but found `<VI1>` (expected () but found integral variable)
./example.rs:4     } else if false {
./example.rs:5         1
./example.rs:6     };
./example.rs:4:11: 6:5 error: mismatched types: expected `<VI0>` but found `()` (expected integral  variable but found ())
./example.rs:4     } else if false {
./example.rs:5         1
./example.rs:6     };
error: aborting due to 2 previous errors
@toffaletti

I apologize in advance if this is a separate issue.

With the map() generic function from the tutorial:

fn main() {                                                                                                                                                                                                                              
    let a = do map([0, 1, 2, 3, 4]) |i| {
        *i + 1;
    }
}

That code gives a nice error message:

$ rustc example.rs 
example.rs:5:0: 5:1 error: expected `;` but found `}`
example.rs:5 }
             ^

However, remove the assignment and what should be the same error message becomes:

$ rustc example.rs 
example.rs:9:11: 9:33 error: mismatched types: expected `()` but found `~[()]` (expected () but found vector)
example.rs:9         do map([0, 1, 2, 3, 4]) |i| {
                        ^~~~~~~~~~~~~~~~~~~~~~
@jdm
Collaborator

@toffaletti Yes, that is unrelated to this issue. You're seeing an expected type mismatch error since the return type of main() is () and your samples result in two different non-() results.

@toffaletti

Thanks @jdm I was one hour into learning Rust when I came across this. I understand what is going on now. I still think the error message could be improved to mention that "expected ()" is referring to the return type of main(). That simple clue could help people not as familiar with Rust to remember the semicolon rule to turn a statement into an expression.

@catamorphism

Not critical for 0.7, but improving error messages is always good. Nominating for milestone 5.

@graydon

declined. this is bad but it's not a blocker.

@msullivan

Confirmed that the situation is unchanged.

@thestinger

This is still an issue.

@metajack

Bug triage. Still an issue.

@steveklabnik

Very, very small change:

test.rs:4:21: 6:6 error: mismatched types: expected `()` but found `<generic integer #1>` (expected () but found integral variable)
test.rs:4     } else if false {
test.rs:5         1
test.rs:6     };
test.rs:2:13: 6:6 error: if and else have incompatible types: expected `<generic integer #0>` but found `()` (expected integral variable but found ())
test.rs:2     let a = if true {
test.rs:3         0 
test.rs:4     } else if false {
test.rs:5         1
test.rs:6     };

@bors bors closed this in #18015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.