From 86a5e72c742b3ad41dd70a3b6bde4cd99768f8fa Mon Sep 17 00:00:00 2001 From: 05st <60903484+05st@users.noreply.github.com> Date: Sat, 2 Oct 2021 15:26:46 -0500 Subject: [PATCH] v.checker: fix return type checking being skipped for mutable method receivers (#12043) --- vlib/v/checker/checker.v | 5 +---- .../tests/mut_receiver_wrong_return_type.out | 13 +++++++++++++ .../checker/tests/mut_receiver_wrong_return_type.vv | 9 +++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/mut_receiver_wrong_return_type.out create mode 100644 vlib/v/checker/tests/mut_receiver_wrong_return_type.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ce84dbf390..a61d662bad 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3758,10 +3758,6 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) { if !c.check_types(got_typ, exp_type) { got_typ_sym := c.table.get_type_symbol(got_typ) mut exp_typ_sym := c.table.get_type_symbol(exp_type) - pos := node.exprs[i].position() - if node.exprs[i].is_auto_deref_var() { - continue - } if exp_typ_sym.kind == .interface_ { if c.type_implements(got_typ, exp_type, node.pos) { if !got_typ.is_ptr() && !got_typ.is_pointer() && got_typ_sym.kind != .interface_ @@ -3771,6 +3767,7 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) { } continue } + pos := node.exprs[i].position() c.error('cannot use `$got_typ_sym.name` as type `${c.table.type_to_str(exp_type)}` in return argument', pos) } diff --git a/vlib/v/checker/tests/mut_receiver_wrong_return_type.out b/vlib/v/checker/tests/mut_receiver_wrong_return_type.out new file mode 100644 index 0000000000..3524822d3c --- /dev/null +++ b/vlib/v/checker/tests/mut_receiver_wrong_return_type.out @@ -0,0 +1,13 @@ +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:9: error: cannot use `Test` as type `int` in return argument + 6 | + 7 | fn (mut test Test) test2() int { + 8 | return test + | ~~~~ + 9 | } diff --git a/vlib/v/checker/tests/mut_receiver_wrong_return_type.vv b/vlib/v/checker/tests/mut_receiver_wrong_return_type.vv new file mode 100644 index 0000000000..e223907ecb --- /dev/null +++ b/vlib/v/checker/tests/mut_receiver_wrong_return_type.vv @@ -0,0 +1,9 @@ +struct Test {} + +fn (mut test Test) test() ?int { + return test +} + +fn (mut test Test) test2() int { + return test +}