1
0
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:
yuyi 2023-02-26 13:54:53 +08:00 committed by GitHub
parent 00aecf92e7
commit 45d4849b0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -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 {

View 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
}
}