Skip to content

Commit

Permalink
checker: fix return type checks, when returning struct values, implem…
Browse files Browse the repository at this point in the history
…enting IError in non-result fn (fix #22659) (fix #22658) (#22660)
  • Loading branch information
felipensp authored Oct 27, 2024
1 parent c8ddf21 commit 1ced1e9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
}
}
// `fn foo() !int { return Err{} }`
if got_type_sym.kind == .struct
if expected_fn_return_type_has_result && got_type_sym.kind == .struct
&& c.type_implements(got_type, ast.error_type, node.pos) {
node.exprs[expr_idxs[i]] = ast.CastExpr{
expr: node.exprs[expr_idxs[i]]
Expand Down
26 changes: 0 additions & 26 deletions vlib/v/checker/tests/mut_receiver_wrong_return_type.out
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
vlib/v/checker/tests/mut_receiver_wrong_return_type.vv:4:2: error: `&Test` doesn't implement method `msg` of interface `IError`
2 |
3 | fn (mut test Test) test() ?int {
4 | return test
| ~~~~~~~~~~~
5 | }
6 |
vlib/v/checker/tests/mut_receiver_wrong_return_type.vv:4:2: error: `&Test` doesn't implement method `code` of interface `IError`
2 |
3 | fn (mut test Test) test() ?int {
4 | return test
| ~~~~~~~~~~~
5 | }
6 |
vlib/v/checker/tests/mut_receiver_wrong_return_type.vv:4:9: error: cannot use `Test` as type `?int` in return argument
2 |
3 | fn (mut test Test) test() ?int {
4 | return test
| ~~~~
5 | }
6 |
vlib/v/checker/tests/mut_receiver_wrong_return_type.vv:8:2: error: `&Test` doesn't implement method `msg` of interface `IError`
6 |
7 | fn (mut test Test) test2() int {
8 | return test
| ~~~~~~~~~~~
9 | }
vlib/v/checker/tests/mut_receiver_wrong_return_type.vv:8:2: error: `&Test` doesn't implement method `code` of interface `IError`
6 |
7 | fn (mut test Test) test2() int {
8 | return test
| ~~~~~~~~~~~
9 | }
vlib/v/checker/tests/mut_receiver_wrong_return_type.vv:8:9: error: cannot use `Test` as type `int` in return argument
6 |
7 | fn (mut test Test) test2() int {
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/wrong_return_type_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/wrong_return_type_err.vv:9:10: error: cannot use `Error` as type `int` in return argument
7 | fn double_positive(num int) int {
8 | if num <= 0 {
9 | return Error{}
| ~~~~~~~
10 | }
11 | return num * 2
vlib/v/checker/tests/wrong_return_type_err.vv:16:10: error: cannot use `Error` as type `Val` in return argument
14 | fn new_val(num int) Val {
15 | if num <= 0 {
16 | return Error{}
| ~~~~~~~
17 | }
18 | return Val{
25 changes: 25 additions & 0 deletions vlib/v/checker/tests/wrong_return_type_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module main

struct Val {
val int
}

fn double_positive(num int) int {
if num <= 0 {
return Error{}
}
return num * 2
}

fn new_val(num int) Val {
if num <= 0 {
return Error{}
}
return Val{
val: num
}
}

fn main() {
println(new_val(2))
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn (my MyError) code() int {

type MapString = map[string]string | int

fn f() ?MapString {
fn f() !MapString {
return MyError{
msg: 'String Error'
}
Expand Down

0 comments on commit 1ced1e9

Please sign in to comment.