v.checker: fix return type checking being skipped for mutable method receivers (#12043)

pull/11977/head
05st 2021-10-02 15:26:46 -05:00 committed by GitHub
parent 02e4aa0f0e
commit 86a5e72c74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -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)
}

View File

@ -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 | }

View File

@ -0,0 +1,9 @@
struct Test {}
fn (mut test Test) test() ?int {
return test
}
fn (mut test Test) test2() int {
return test
}