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
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) p.check(.decl_assign)
comments << p.eat_comments() comments << p.eat_comments()
expr := p.expr(0) 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', p.error_with_pos('if guard condition expression is illegal, it should return optional',
expr.pos()) expr.pos())
} }

View File

@@ -12,11 +12,13 @@
[vlib/v/tests/inout/struct_field_optional.vv:33] sum: 4 [vlib/v/tests/inout/struct_field_optional.vv:33] sum: 4
4 4
[vlib/v/tests/inout/struct_field_optional.vv:36] sum: 4 [vlib/v/tests/inout/struct_field_optional.vv:36] sum: 4
3
none
Foo{ Foo{
bar: 3 bar: 3
baz: 0 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 bar: 3
baz: 0 baz: 0
} }

View File

@@ -34,6 +34,17 @@ fn main() {
sum = f.bar or { 123 } + 1 sum = f.bar or { 123 } + 1
println(sum) println(sum)
dump(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 // others test
println(f) println(f)
dump(f) dump(f)