mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix match expr returning optional (#10281)
This commit is contained in:
parent
5b9256ba0b
commit
5aa4f622b6
@ -5364,7 +5364,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||
}
|
||||
expr_type := c.expr(stmt.expr)
|
||||
if ret_type == ast.void_type {
|
||||
if node.is_expr
|
||||
if node.is_expr && !node.expected_type.has_flag(.optional)
|
||||
&& c.table.get_type_symbol(node.expected_type).kind == .sum_type {
|
||||
ret_type = node.expected_type
|
||||
} else {
|
||||
|
36
vlib/v/tests/match_expr_returning_optional_test.v
Normal file
36
vlib/v/tests/match_expr_returning_optional_test.v
Normal file
@ -0,0 +1,36 @@
|
||||
type Any = int | string
|
||||
|
||||
fn ok(s string) Any {
|
||||
return match s {
|
||||
'foo' {
|
||||
Any(1)
|
||||
}
|
||||
else {
|
||||
Any('asdf')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fails(s string) ?Any {
|
||||
return match s {
|
||||
'foo' {
|
||||
Any(1)
|
||||
}
|
||||
else {
|
||||
Any('asdf')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_match_expr_returning_optional() {
|
||||
ret1 := ok('foo')
|
||||
println(ret1)
|
||||
assert ret1 == Any(1)
|
||||
|
||||
ret2 := fails('foo') or {
|
||||
assert false
|
||||
return
|
||||
}
|
||||
println(ret2)
|
||||
assert ret2 == Any(1)
|
||||
}
|
Loading…
Reference in New Issue
Block a user