1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check mismatch of return result type (#15413)

This commit is contained in:
yuyi
2022-08-12 22:23:14 +08:00
committed by GitHub
parent e6606d8670
commit 584597aa3d
3 changed files with 27 additions and 0 deletions

View File

@ -104,6 +104,12 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument',
pos)
}
if got_typ.has_flag(.result) && (!exp_type.has_flag(.result)
|| c.table.type_to_str(got_typ) != c.table.type_to_str(exp_type)) {
pos := node.exprs[expr_idxs[i]].pos()
c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument',
pos)
}
if node.exprs[expr_idxs[i]] !is ast.ComptimeCall && !c.check_types(got_typ, exp_type) {
got_typ_sym := c.table.sym(got_typ)
mut exp_typ_sym := c.table.sym(exp_type)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/return_result_type_mismatch.vv:8:9: error: cannot use `!int` as type `?int` in return argument
6 | // Option
7 | fn bar(a int) ?int {
8 | return foo(a)
| ~~~~~~
9 | }
10 |

View File

@ -0,0 +1,14 @@
// Result
fn foo(a int) !int {
return a + 1
}
// Option
fn bar(a int) ?int {
return foo(a)
}
fn main() {
x := bar(1)?
println('$x')
}