checker: fix return option mismatch checking (fix #20418) (#20423)

This commit is contained in:
Felipe Pena 2024-01-07 23:00:18 -03:00 committed by GitHub
parent ab9aea2282
commit d78adb09e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -171,8 +171,8 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
}
}
got_type := c.unwrap_generic(got_types[i])
if got_type.has_flag(.option)
&& got_type.clear_flag(.option) != exp_type.clear_flag(.option) {
if got_type.has_flag(.option) && (!exp_type.has_flag(.option)
|| got_type.clear_flag(.option) != exp_type.clear_flag(.option)) {
pos := node.exprs[expr_idxs[i]].pos()
c.error('cannot use `${c.table.type_to_str(got_type)}` as ${c.error_type_name(exp_type)} in return argument',
pos)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/fn_mismatch_option_return_err.vv:4:15: error: cannot use `?time.Duration` as type `time.Duration` in return argument
2 |
3 | fn foo() time.Duration {
4 | return ?time.Duration(0)
| ~~~~~~~~~~~
5 | }
6 |

View File

@ -0,0 +1,7 @@
import time
fn foo() time.Duration {
return ?time.Duration(0)
}
println(foo())