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

ast, cgen: fix match struct.field? {...} (#16478)

This commit is contained in:
shove 2022-11-19 16:42:36 +08:00 committed by GitHub
parent 2d9808b2dc
commit 092f984708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 6 deletions

View File

@ -440,7 +440,14 @@ pub fn (x Expr) str() string {
return 'ast.SelectExpr' return 'ast.SelectExpr'
} }
SelectorExpr { SelectorExpr {
return '${x.expr.str()}.${x.field_name}' propagate_suffix := if x.or_block.kind == .propagate_option {
'?'
} else if x.or_block.kind == .propagate_result {
'!'
} else {
''
}
return '${x.expr.str()}.${x.field_name}${propagate_suffix}'
} }
SizeOf { SizeOf {
if x.is_type { if x.is_type {

View File

@ -63,7 +63,9 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
g.inside_match_result = true g.inside_match_result = true
} }
} }
if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral] { if node.cond in [ast.Ident, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral]
|| (node.cond is ast.SelectorExpr
&& (node.cond as ast.SelectorExpr).or_block.kind == .absent) {
cond_var = g.expr_string(node.cond) cond_var = g.expr_string(node.cond)
} else { } else {
line := if is_expr { line := if is_expr {

View File

@ -1,9 +1,9 @@
1 1
[vlib/v/tests/inout/struct_field_optional.vv:11] f.bar: 1 [vlib/v/tests/inout/struct_field_optional.vv:11] f.bar?: 1
2 2
[vlib/v/tests/inout/struct_field_optional.vv:18] f.bar: 2 [vlib/v/tests/inout/struct_field_optional.vv:18] f.bar?: 2
3 3
[vlib/v/tests/inout/struct_field_optional.vv:22] f.bar: 3 [vlib/v/tests/inout/struct_field_optional.vv:22] f.bar?: 3
3 3
[vlib/v/tests/inout/struct_field_optional.vv:26] a: 3 [vlib/v/tests/inout/struct_field_optional.vv:26] a: 3
9999 9999
@ -14,11 +14,12 @@
[vlib/v/tests/inout/struct_field_optional.vv:36] sum: 4 [vlib/v/tests/inout/struct_field_optional.vv:36] sum: 4
3 3
none none
3
Foo{ Foo{
bar: 3 bar: 3
baz: 0 baz: 0
} }
[vlib/v/tests/inout/struct_field_optional.vv:50] f: Foo{ [vlib/v/tests/inout/struct_field_optional.vv:57] f: Foo{
bar: 3 bar: 3
baz: 0 baz: 0
} }

View File

@ -45,6 +45,13 @@ fn main() {
} else { } else {
println(err) println(err)
} }
// `match` test
match f.bar? {
f.bar? {
println(f.bar?)
}
else {}
}
// others test // others test
println(f) println(f)
dump(f) dump(f)