Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix return type checker when returning struct which implements IError on non-result fn #22660

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
spytheman marked this conversation as resolved.
Show resolved Hide resolved
return MyError{
msg: 'String Error'
}
Expand Down
Loading