diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index dcc9431961..592d62a6ac 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -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) diff --git a/vlib/v/checker/tests/fn_mismatch_option_return_err.out b/vlib/v/checker/tests/fn_mismatch_option_return_err.out new file mode 100644 index 0000000000..2845ea5423 --- /dev/null +++ b/vlib/v/checker/tests/fn_mismatch_option_return_err.out @@ -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 | diff --git a/vlib/v/checker/tests/fn_mismatch_option_return_err.vv b/vlib/v/checker/tests/fn_mismatch_option_return_err.vv new file mode 100644 index 0000000000..1217d6d449 --- /dev/null +++ b/vlib/v/checker/tests/fn_mismatch_option_return_err.vv @@ -0,0 +1,7 @@ +import time + +fn foo() time.Duration { + return ?time.Duration(0) +} + +println(foo()) \ No newline at end of file