mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix returning embedded error result (#16208)
This commit is contained in:
parent
76606598c3
commit
03bef24456
@ -858,9 +858,9 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
|
||||
// TODO: remove once deprecation period for `IError` methods has ended
|
||||
if inter_sym.idx == ast.error_type_idx
|
||||
&& (imethod.name == 'msg' || imethod.name == 'code') {
|
||||
c.note("`$styp` doesn't implement method `$imethod.name` of interface `$inter_sym.name`. The usage of fields is being deprecated in favor of methods.",
|
||||
pos)
|
||||
continue
|
||||
// c.note("`$styp` doesn't implement method `$imethod.name` of interface `$inter_sym.name`. The usage of fields is being deprecated in favor of methods.",
|
||||
// pos)
|
||||
return false
|
||||
}
|
||||
// <<
|
||||
|
||||
|
@ -138,6 +138,17 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if got_typ_sym.kind == .struct_
|
||||
&& c.type_implements(got_typ, ast.error_type, node.pos) {
|
||||
node.exprs[expr_idxs[i]] = ast.CastExpr{
|
||||
expr: node.exprs[expr_idxs[i]]
|
||||
typname: 'IError'
|
||||
typ: ast.error_type
|
||||
expr_type: got_typ
|
||||
}
|
||||
node.types[expr_idxs[i]] = ast.error_type
|
||||
continue
|
||||
}
|
||||
got_typ_name := if got_typ_sym.kind == .function {
|
||||
'${c.table.type_to_str(got_typ)}'
|
||||
} else {
|
||||
|
21
vlib/v/tests/embed_error_in_returning_result_test.v
Normal file
21
vlib/v/tests/embed_error_in_returning_result_test.v
Normal file
@ -0,0 +1,21 @@
|
||||
struct NotFoundErr {
|
||||
Error
|
||||
id string
|
||||
}
|
||||
|
||||
struct User {}
|
||||
|
||||
fn find_by_id(id string) !User {
|
||||
return NotFoundErr{
|
||||
id: id
|
||||
}
|
||||
}
|
||||
|
||||
fn test_embed_error_in_returning_result() {
|
||||
find_by_id('id123') or {
|
||||
println(err)
|
||||
assert true
|
||||
return
|
||||
}
|
||||
assert false
|
||||
}
|
Loading…
Reference in New Issue
Block a user