V: Faulty `if` expression needs `else` clause

Created on 27 Aug 2020  路  2Comments  路  Source: vlang/v

V version: V 0.1.29 99dd72e
OS: macOS 10.15.6

What did you do?
Wrote a test

What did you expect to see?
No warnings or errors

What did you see instead?
An error

Haven't dug into the details but I noticed this test working in V 0.1.23 is no longer working in V 0.1.29 99dd72e.

$ tail -20 *.v
==> example.v <==
module example

pub fn x() []int {
    return [1, 2, 3]
}

==> example_test.v <==
module example

fn test_x() {
    assert array_eq(x(), [1, 2, 3])
    if !array_eq(x(), [1, 2, 3]) {
        println('x was not 1')
    }
}

fn array_eq(a, b []int) bool {
    if a.len != b.len {
        return false
    }
    for i, v in a {
        if v != b[i] {
            return false
        }
    }
    return true
}
$ v test example_test.v
----------------------------------- Testing... ---------------------------------
FAIL    55.918 ms example_test.v
example_test.v:5:2: error: `if` expression needs `else` clause
    3 | fn test_x() {
    4 |     assert array_eq(x(), [1, 2, 3])
    5 |     if !array_eq(x(), [1, 2, 3]) {
      |     ~~
    6 |         println('x was not 1')
    7 |     }


--------------------------------------------------------------------------------
       56.370 ms <=== total time spent running V _test.v files
                 ok, fail, skip, total =     0,     1,     0,     1

Is resolved by adding any other statement:

 assert array_eq(x(), [1, 2, 3])
 println(1)
Bug

All 2 comments

Reduced test case:

fn f(a []int) bool {
    return true
}
assert f([1,2,3])
if f([1,2,3]) {
    println('x')
}

Fixed with latest V.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  路  3Comments

jtkirkpatrick picture jtkirkpatrick  路  3Comments

medvednikov picture medvednikov  路  3Comments

arg2das picture arg2das  路  3Comments

aurora picture aurora  路  3Comments