mirror of
https://github.com/vlang/v.git
synced 2025-09-10 07:47:20 -04:00
checker: add option type inference on if expr (implement most of #23827, except the error for v := if c { none } else { none }
) (#23829)
This commit is contained in:
parent
80477e3acf
commit
8dbde185ef
@ -510,6 +510,15 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
|
|||||||
stmt.typ = ast.error_type
|
stmt.typ = ast.error_type
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if (node.typ == ast.none_type && stmt.typ != ast.none_type)
|
||||||
|
|| (stmt.typ == ast.none_type && node.typ != ast.none_type) {
|
||||||
|
node.typ = if stmt.typ != ast.none_type {
|
||||||
|
stmt.typ.set_flag(.option)
|
||||||
|
} else {
|
||||||
|
node.typ.set_flag(.option)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
|
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
|
||||||
node.pos)
|
node.pos)
|
||||||
} else {
|
} else {
|
||||||
|
8
vlib/v/tests/options/option_if_infer_test.v
Normal file
8
vlib/v/tests/options/option_if_infer_test.v
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fn test_main() {
|
||||||
|
v := if true { none } else { 1 }
|
||||||
|
assert '${v}' == 'Option(none)'
|
||||||
|
v2 := if false { 1 } else { none }
|
||||||
|
assert '${v2}' == 'Option(none)'
|
||||||
|
v3 := if true { 1 } else { none }
|
||||||
|
assert '${v3}' == 'Option(1)'
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user