1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix if-guard for struct optional fields (fix #16460) (#16461)

This commit is contained in:
shove 2022-11-17 20:30:16 +08:00 committed by GitHub
parent 74efd2621b
commit ae816b1719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

View File

@ -113,7 +113,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
p.check(.decl_assign)
comments << p.eat_comments()
expr := p.expr(0)
if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr] {
if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr, ast.SelectorExpr] {
p.error_with_pos('if guard condition expression is illegal, it should return optional',
expr.pos())
}

View File

@ -12,11 +12,13 @@
[vlib/v/tests/inout/struct_field_optional.vv:33] sum: 4
4
[vlib/v/tests/inout/struct_field_optional.vv:36] sum: 4
3
none
Foo{
bar: 3
baz: 0
}
[vlib/v/tests/inout/struct_field_optional.vv:39] f: Foo{
[vlib/v/tests/inout/struct_field_optional.vv:50] f: Foo{
bar: 3
baz: 0
}

View File

@ -34,6 +34,17 @@ fn main() {
sum = f.bar or { 123 } + 1
println(sum)
dump(sum)
// `if guard` test
if c := f.bar {
println(c)
} else {
println(err)
}
if c := f.baz {
println(c)
} else {
println(err)
}
// others test
println(f)
dump(f)