V: Give better error information when `If` assignment without `else`

Created on 13 Jan 2020  路  10Comments  路  Source: vlang/v

V version: V 0.1.24 3425934
OS: Linux

What did you do?

struct Person {
mut:
    name string = 'Person'
    age int = 8
}

fn new_person(name string, age int) Person {
    mut _person := Person{}
    _person.name = if name != '' { name }
    return _person
}

fn main() {
    don := new_person('', 20)
    println(don.name)
}

What did you expect to see?
Better error information. E.g The If assignment requires else or something better than that.
OR
If we allow that, then if the if fails, the _person.name will remain with the default value which is 'Person'.

What did you see instead?

main.tmp.c: In function 'main__new_person':
main.tmp.c:3876:62: error: expected ')' before ';' token
  _person .name  =  ((string_ne( name , tos3("") ) ) ? ...
(Use `v -g` to print the entire error message)

V error: C error. This should never happen. 
Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
Bug Compiler

Most helpful comment

I think we will need else case as it will make sense to have it.

All 10 comments

I think we will need else case as it will make sense to have it.

I think it would be better for it to be an optional if else is not provided.
Just to make the definition of the if statement self-contained instead of making its syntax rely on whether its value is used.

Also this is more of a bug report than a feature request (C error definitely shouldn't happen no matter what)

Is the use of if/else as a sort of lambda for variables necessary? The example could be written as

fn new_person(name string, age int) Person {
    mut _person := Person{}
    if(name != ''){
        _person.name = name
    }
    return _person
}

Wondering how to use the special syntax efficiently.

This code now gives a proper V error message:

json_primitives.v:9:19: error: if expression needs else clause
7 | fn new_person(name string, age int) Person {
8 | mut person := Person{}
9 | person.name = if name != '' { name }
| ~~
10 | return person
11 | }

If indeed this remains true, this issue can be closed I think.

I still think this is an issue and should be re-opened. 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

But I don't see an if expression here?

x := if ... else ...

I'ts right there, where to error points to: example_test.v:5:2:. This does not yield an error:

fn test_x() {
    assert array_eq(x(), [1, 2, 3])
    if !array_eq(x(), [1, 2, 3]) {
        println('x was not 1')
    } else {
        println('No error will be printed now')
    }
}

It's not an if expression, it's a simple if statement.

But I see that the compiler mistakenly thinks it's an expression. Can be solved by adding a random statement:

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

Good find. This should be a separate issue.

My bad, I mixed up expression and statement. I added #6233, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taojy123 picture taojy123  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

XVilka picture XVilka  路  3Comments

radare picture radare  路  3Comments

radare picture radare  路  3Comments