checker: check error for returning aliases of fixed array (#14349)

master
yuyi 2022-05-10 19:01:55 +08:00 committed by GitHub
parent 9e09b709e3
commit 606d8cfaca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -92,7 +92,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
}
return_sym := c.table.sym(node.return_type)
return_sym := c.table.final_sym(node.return_type)
if return_sym.info is ast.MultiReturn {
for multi_type in return_sym.info.types {
multi_sym := c.table.sym(multi_type)

View File

@ -0,0 +1,14 @@
vlib/v/checker/tests/return_aliases_of_fixed_array.vv:8:18: error: fixed array cannot be returned by function
6 | }
7 |
8 | fn (v Mat) foo() Mat {
| ~~~
9 | return v
10 | }
vlib/v/checker/tests/return_aliases_of_fixed_array.vv:12:10: error: fixed array cannot be returned by function
10 | }
11 |
12 | fn bar() Mat {
| ~~~
13 | return Mat([[1, 2]!, [3, 4]!]!)
14 | }

View File

@ -0,0 +1,14 @@
type Mat = [2][2]int
fn main() {
a := Mat([[1, 2]!, [3, 4]!]!)
println(a.foo())
}
fn (v Mat) foo() Mat {
return v
}
fn bar() Mat {
return Mat([[1, 2]!, [3, 4]!]!)
}