mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix returning match expr with custom error (#17413)
This commit is contained in:
parent
00aecf92e7
commit
45d4849b0f
@ -69,7 +69,21 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||
ret_type = expr_type
|
||||
}
|
||||
} else if node.is_expr && ret_type.idx() != expr_type.idx() {
|
||||
c.check_match_branch_last_stmt(stmt, ret_type, expr_type)
|
||||
if (node.expected_type.has_flag(.option)
|
||||
|| node.expected_type.has_flag(.result))
|
||||
&& c.table.sym(stmt.typ).kind == .struct_
|
||||
&& c.type_implements(stmt.typ, ast.error_type, node.pos) {
|
||||
stmt.expr = ast.CastExpr{
|
||||
expr: stmt.expr
|
||||
typname: 'IError'
|
||||
typ: ast.error_type
|
||||
expr_type: stmt.typ
|
||||
pos: node.pos
|
||||
}
|
||||
stmt.typ = ast.error_type
|
||||
} else {
|
||||
c.check_match_branch_last_stmt(stmt, ret_type, expr_type)
|
||||
}
|
||||
}
|
||||
} else if stmt !is ast.Return {
|
||||
if node.is_expr && ret_type != ast.void_type {
|
||||
|
18
vlib/v/tests/return_match_expr_with_custom_error_test.v
Normal file
18
vlib/v/tests/return_match_expr_with_custom_error_test.v
Normal file
@ -0,0 +1,18 @@
|
||||
struct CustomError {
|
||||
Error
|
||||
}
|
||||
|
||||
fn ret() !string {
|
||||
return match true {
|
||||
true { 'ok' }
|
||||
else { CustomError{} }
|
||||
}
|
||||
}
|
||||
|
||||
fn test_return_match_expr_with_custom_error() {
|
||||
if r := ret() {
|
||||
assert r == 'ok'
|
||||
} else {
|
||||
assert false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user