parser, checker, cgen: fix fn return alias of fixed array (#19116)

This commit is contained in:
yuyi 2023-08-12 01:57:05 +08:00 committed by GitHub
parent 5a0d9e7d67
commit 61ffbe1a8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 19 deletions

View File

@ -118,9 +118,17 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
} }
} }
} }
return_sym := c.table.final_sym(node.return_type) return_sym := c.table.sym(node.return_type)
if return_sym.info is ast.MultiReturn { if return_sym.info is ast.Alias {
for multi_type in return_sym.info.types { parent_sym := c.table.sym(return_sym.info.parent_type)
if parent_sym.info is ast.ArrayFixed {
c.table.find_or_register_array_fixed(parent_sym.info.elem_type, parent_sym.info.size,
parent_sym.info.size_expr, true)
}
}
final_return_sym := c.table.final_sym(node.return_type)
if final_return_sym.info is ast.MultiReturn {
for multi_type in final_return_sym.info.types {
if multi_type == ast.error_type { if multi_type == ast.error_type {
c.error('type `IError` cannot be used in multi-return, return an Option instead', c.error('type `IError` cannot be used in multi-return, return an Option instead',
node.return_type_pos) node.return_type_pos)

View File

@ -1491,7 +1491,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
} }
g.write(')') g.write(')')
if node.return_type != 0 && !node.return_type.has_flag(.option) if node.return_type != 0 && !node.return_type.has_flag(.option)
&& g.table.sym(node.return_type).kind == .array_fixed { && g.table.final_sym(node.return_type).kind == .array_fixed {
// it's non-option fixed array, requires accessing .ret_arr member to get the array // it's non-option fixed array, requires accessing .ret_arr member to get the array
g.write('.ret_arr') g.write('.ret_arr')
} }
@ -1798,7 +1798,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
g.write(')') g.write(')')
} }
if node.return_type != 0 && !node.return_type.has_flag(.option) if node.return_type != 0 && !node.return_type.has_flag(.option)
&& g.table.sym(node.return_type).kind == .array_fixed { && g.table.final_sym(node.return_type).kind == .array_fixed {
// it's non-option fixed array, requires accessing .ret_arr member to get the array // it's non-option fixed array, requires accessing .ret_arr member to get the array
g.write('.ret_arr') g.write('.ret_arr')
} }

View File

@ -718,16 +718,6 @@ fn (mut p Parser) find_type_or_add_placeholder(name string, language ast.Languag
typ = ast.new_type(idx) typ = ast.new_type(idx)
} }
} }
ast.Alias {
if p.inside_fn_return {
parent_sym := p.table.sym(sym.info.parent_type)
if parent_sym.kind == .array_fixed {
info := parent_sym.array_fixed_info()
typ = p.table.find_or_register_array_fixed(info.elem_type, info.size,
info.size_expr, p.inside_fn_return)
}
}
}
else {} else {}
} }
return typ return typ

View File

@ -7,7 +7,6 @@ fn test_main() {
assert z == [[1, 2]!, [3, 4]!]! assert z == [[1, 2]!, [3, 4]!]!
} }
// vfmt off
fn (v Mat) foo() Mat { fn (v Mat) foo() Mat {
return v return v
} }
@ -15,4 +14,3 @@ fn (v Mat) foo() Mat {
fn bar() Mat { fn bar() Mat {
return Mat([[1, 2]!, [3, 4]!]!) return Mat([[1, 2]!, [3, 4]!]!)
} }
// vfmt on

View File

@ -8,11 +8,9 @@ fn return_fixed_array_in_multi_return() ([3]int, [3]int) {
return [1, 2, 3]!, [4, 5, 6]! return [1, 2, 3]!, [4, 5, 6]!
} }
// vfmt off
fn return_fixed_array_with_alias() Abc { fn return_fixed_array_with_alias() Abc {
return [1, 2, 3]! return [1, 2, 3]!
} }
// vfmt on
fn test_with_alias() { fn test_with_alias() {
a := return_fixed_array_with_alias() a := return_fixed_array_with_alias()