Skip to content

Commit ae816b1

Browse files
authored
parser: fix if-guard for struct optional fields (fix #16460) (#16461)
1 parent 74efd26 commit ae816b1

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

vlib/v/parser/if_match.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
113113
p.check(.decl_assign)
114114
comments << p.eat_comments()
115115
expr := p.expr(0)
116-
if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr] {
116+
if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr, ast.SelectorExpr] {
117117
p.error_with_pos('if guard condition expression is illegal, it should return optional',
118118
expr.pos())
119119
}

vlib/v/tests/inout/struct_field_optional.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
[vlib/v/tests/inout/struct_field_optional.vv:33] sum: 4
1313
4
1414
[vlib/v/tests/inout/struct_field_optional.vv:36] sum: 4
15+
3
16+
none
1517
Foo{
1618
bar: 3
1719
baz: 0
1820
}
19-
[vlib/v/tests/inout/struct_field_optional.vv:39] f: Foo{
21+
[vlib/v/tests/inout/struct_field_optional.vv:50] f: Foo{
2022
bar: 3
2123
baz: 0
2224
}

vlib/v/tests/inout/struct_field_optional.vv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ fn main() {
3434
sum = f.bar or { 123 } + 1
3535
println(sum)
3636
dump(sum)
37+
// `if guard` test
38+
if c := f.bar {
39+
println(c)
40+
} else {
41+
println(err)
42+
}
43+
if c := f.baz {
44+
println(c)
45+
} else {
46+
println(err)
47+
}
3748
// others test
3849
println(f)
3950
dump(f)

0 commit comments

Comments
 (0)