vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.5k stars 2.15k forks source link

checker, cgen: fix auto dereference mut variable in if expr (fix #21309) #21720

Closed yuyi98 closed 4 days ago

yuyi98 commented 5 days ago

This PR fix auto dereference mut variable in if expr (fix #21309).

struct Foo {
    str string
}

fn (mut f Foo) foo(f2 Foo) string {
    return (if f.str != '' {
        f
    } else {
        f2
    }).str
}

fn main() {
    mut foo := Foo{}
    dump(foo.foo(Foo{ str: 'a' }))
    assert foo.foo(Foo{ str: 'a' }) == 'a'
}

PS D:\Test\v\tt1> v run .
[.\\tt1.v:15] foo.foo(main.Foo{....}): a