From d78adb09e4f3733d3a939e20e29d16807f9ef8f1 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 7 Jan 2024 23:00:18 -0300 Subject: [PATCH] checker: fix return option mismatch checking (fix #20418) (#20423) --- vlib/v/checker/return.v | 4 ++-- vlib/v/checker/tests/fn_mismatch_option_return_err.out | 7 +++++++ vlib/v/checker/tests/fn_mismatch_option_return_err.vv | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/fn_mismatch_option_return_err.out create mode 100644 vlib/v/checker/tests/fn_mismatch_option_return_err.vv 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