cgen: fix generic result return (fix #24097) (#24100)

This commit is contained in:
kbkpbot 2025-04-01 20:54:19 +08:00 committed by GitHub
parent a8a4507237
commit a59b717daa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -980,6 +980,16 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
ret_typ = g.unwrap_generic(ret_sym.info.elem_type).derive(unwrapped_ret_typ) ret_typ = g.unwrap_generic(ret_sym.info.elem_type).derive(unwrapped_ret_typ)
} }
} }
} else {
r_typ := g.resolve_return_type(node)
if r_typ != ast.void_type && !r_typ.has_flag(.generic) {
// restore result/option flag, as `resolve_return_type` may clean them
if node.return_type.has_flag(.result) {
ret_typ = r_typ.set_flag(.result)
} else {
ret_typ = r_typ.set_flag(.option)
}
}
} }
} }
mut styp := g.styp(ret_typ) mut styp := g.styp(ret_typ)

View File

@ -0,0 +1,15 @@
fn decode_map[K, V](a map[K]V) !map[K]V {
return a
}
fn decode[T]() !T {
return decode_map(T{})!
}
fn test_main() {
x_str_str := decode[map[string]string]() or { panic(err) }
x_str_int := decode[map[string]int]() or { panic(err) }
assert typeof(x_str_str).name == 'map[string]string'
assert typeof(x_str_int).name == 'map[string]int'
}