gen: fix generic multi return (#8303)
parent
ef6839286e
commit
35432c5e92
|
@ -471,11 +471,11 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
|
|||
// resolve_generic_type resolves generics to real types T => int.
|
||||
// Even map[string]map[string]T can be resolved.
|
||||
// This is used for resolving the generic return type of CallExpr white `unwrap_generic` is used to resolve generic usage in FnDecl.
|
||||
fn (mut c Checker) resolve_generic_type(generic_type table.Type, generic_names []string, call_expr ast.CallExpr) ?table.Type {
|
||||
fn (mut c Checker) resolve_generic_type(generic_type table.Type, generic_names []string, generic_types []table.Type) ?table.Type {
|
||||
mut sym := c.table.get_type_symbol(generic_type)
|
||||
if sym.name in generic_names {
|
||||
index := generic_names.index(sym.name)
|
||||
mut typ := call_expr.generic_types[index]
|
||||
mut typ := generic_types[index]
|
||||
typ = typ.set_nr_muls(generic_type.nr_muls())
|
||||
if generic_type.has_flag(.optional) {
|
||||
typ = typ.set_flag(.optional)
|
||||
|
@ -491,14 +491,14 @@ fn (mut c Checker) resolve_generic_type(generic_type table.Type, generic_names [
|
|||
elem_sym = c.table.get_type_symbol(elem_type)
|
||||
dims++
|
||||
}
|
||||
if typ := c.resolve_generic_type(elem_type, generic_names, call_expr) {
|
||||
if typ := c.resolve_generic_type(elem_type, generic_names, generic_types) {
|
||||
idx := c.table.find_or_register_array_with_dims(typ, dims)
|
||||
array_typ := table.new_type(idx)
|
||||
return array_typ
|
||||
}
|
||||
} else if sym.kind == .chan {
|
||||
info := sym.info as table.Chan
|
||||
if typ := c.resolve_generic_type(info.elem_type, generic_names, call_expr) {
|
||||
if typ := c.resolve_generic_type(info.elem_type, generic_names, generic_types) {
|
||||
idx := c.table.find_or_register_chan(typ, typ.nr_muls() > 0)
|
||||
chan_typ := table.new_type(idx)
|
||||
return chan_typ
|
||||
|
@ -507,7 +507,7 @@ fn (mut c Checker) resolve_generic_type(generic_type table.Type, generic_names [
|
|||
mut types := []table.Type{}
|
||||
mut type_changed := false
|
||||
for ret_type in sym.info.types {
|
||||
if typ := c.resolve_generic_type(ret_type, generic_names, call_expr) {
|
||||
if typ := c.resolve_generic_type(ret_type, generic_names, generic_types) {
|
||||
types << typ
|
||||
type_changed = true
|
||||
} else {
|
||||
|
@ -523,11 +523,11 @@ fn (mut c Checker) resolve_generic_type(generic_type table.Type, generic_names [
|
|||
mut type_changed := false
|
||||
mut unwrapped_key_type := sym.info.key_type
|
||||
mut unwrapped_value_type := sym.info.value_type
|
||||
if typ := c.resolve_generic_type(sym.info.key_type, generic_names, call_expr) {
|
||||
if typ := c.resolve_generic_type(sym.info.key_type, generic_names, generic_types) {
|
||||
unwrapped_key_type = typ
|
||||
type_changed = true
|
||||
}
|
||||
if typ := c.resolve_generic_type(sym.info.value_type, generic_names, call_expr) {
|
||||
if typ := c.resolve_generic_type(sym.info.value_type, generic_names, generic_types) {
|
||||
unwrapped_value_type = typ
|
||||
type_changed = true
|
||||
}
|
||||
|
|
|
@ -1579,33 +1579,10 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
|
|||
c.infer_fn_types(method, mut call_expr)
|
||||
}
|
||||
if call_expr.generic_types.len > 0 && method.return_type != 0 {
|
||||
// Handle `foo<T>() T` => `foo<int>() int` => return int
|
||||
return_sym := c.table.get_type_symbol(method.return_type)
|
||||
if return_sym.name in method.generic_names {
|
||||
generic_index := method.generic_names.index(return_sym.name)
|
||||
mut typ := call_expr.generic_types[generic_index]
|
||||
typ = typ.set_nr_muls(method.return_type.nr_muls())
|
||||
if method.return_type.has_flag(.optional) {
|
||||
typ = typ.set_flag(.optional)
|
||||
}
|
||||
if typ := c.resolve_generic_type(method.return_type, method.generic_names,
|
||||
call_expr.generic_types) {
|
||||
call_expr.return_type = typ
|
||||
return typ
|
||||
} else if return_sym.kind == .array {
|
||||
elem_info := return_sym.info as table.Array
|
||||
elem_sym := c.table.get_type_symbol(elem_info.elem_type)
|
||||
if elem_sym.name in method.generic_names {
|
||||
generic_index := method.generic_names.index(elem_sym.name)
|
||||
typ := call_expr.generic_types[generic_index]
|
||||
idx := c.table.find_or_register_array(typ)
|
||||
return table.new_type(idx)
|
||||
}
|
||||
} else if return_sym.kind == .chan {
|
||||
elem_info := return_sym.info as table.Chan
|
||||
elem_sym := c.table.get_type_symbol(elem_info.elem_type)
|
||||
if elem_sym.name in method.generic_names {
|
||||
idx := c.table.find_or_register_chan(elem_info.elem_type, elem_info.elem_type.nr_muls() > 0)
|
||||
return table.new_type(idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
if call_expr.generic_types.len > 0 && method.generic_names.len == 0 {
|
||||
|
@ -1916,7 +1893,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
|
|||
c.infer_fn_types(f, mut call_expr)
|
||||
}
|
||||
if call_expr.generic_types.len > 0 && f.return_type != 0 {
|
||||
if typ := c.resolve_generic_type(f.return_type, f.generic_names, call_expr) {
|
||||
if typ := c.resolve_generic_type(f.return_type, f.generic_names, call_expr.generic_types) {
|
||||
call_expr.return_type = typ
|
||||
return typ
|
||||
}
|
||||
|
|
|
@ -614,6 +614,7 @@ static inline Option_void __Option_${styp}_pushval($styp ch, $el_type e) {
|
|||
fn (g &Gen) cc_type2(t table.Type) string {
|
||||
sym := g.table.get_type_symbol(g.unwrap_generic(t))
|
||||
mut styp := sym.cname
|
||||
// TODO: this needs to be removed; cgen shouldn't resolve generic types (job of checker)
|
||||
if mut sym.info is table.Struct {
|
||||
if sym.info.generic_types.len > 0 {
|
||||
mut sgtyps := '_T'
|
||||
|
@ -627,6 +628,14 @@ fn (g &Gen) cc_type2(t table.Type) string {
|
|||
}
|
||||
styp += sgtyps
|
||||
}
|
||||
} else if mut sym.info is table.MultiReturn {
|
||||
// TODO: this doesn't belong here, but makes it working for now
|
||||
mut cname := 'multi_return'
|
||||
for mr_typ in sym.info.types {
|
||||
mr_type_sym := g.table.get_type_symbol(g.unwrap_generic(mr_typ))
|
||||
cname += '_$mr_type_sym.cname'
|
||||
}
|
||||
return cname
|
||||
}
|
||||
return styp
|
||||
}
|
||||
|
@ -748,9 +757,12 @@ pub fn (mut g Gen) write_multi_return_types() {
|
|||
if sym.kind != .multi_return {
|
||||
continue
|
||||
}
|
||||
info := sym.mr_info()
|
||||
if info.types.filter(it.has_flag(.generic)).len > 0 {
|
||||
continue
|
||||
}
|
||||
g.typedefs.writeln('typedef struct $sym.cname $sym.cname;')
|
||||
g.type_definitions.writeln('struct $sym.cname {')
|
||||
info := sym.mr_info()
|
||||
for i, mr_typ in info.types {
|
||||
type_name := g.typ(mr_typ)
|
||||
g.type_definitions.writeln('\t$type_name arg$i;')
|
||||
|
|
|
@ -325,7 +325,7 @@ pub fn (g &Gen) unwrap_generic(typ table.Type) table.Type {
|
|||
break
|
||||
}
|
||||
}
|
||||
return g.cur_generic_types[0].derive(typ).clear_flag(.generic)
|
||||
return g.cur_generic_types[idx].derive(typ).clear_flag(.generic)
|
||||
}
|
||||
return typ
|
||||
}
|
||||
|
|
|
@ -123,6 +123,10 @@ pub fn (mut p Parser) parse_multi_return_type() table.Type {
|
|||
}
|
||||
}
|
||||
p.check(.rpar)
|
||||
if mr_types.len == 1 {
|
||||
// no multi return type needed
|
||||
return mr_types[0]
|
||||
}
|
||||
idx := p.table.find_or_register_multi_return(mr_types)
|
||||
return table.new_type(idx)
|
||||
}
|
||||
|
|
|
@ -363,7 +363,6 @@ fn test_generic_return_nested_map() {
|
|||
assert typeof(generic_return_nested_map<string>()).name == 'map[string]map[string]string'
|
||||
}
|
||||
|
||||
/*
|
||||
fn multi_return<A, B>() (A, B) {
|
||||
return A{}, B{}
|
||||
}
|
||||
|
@ -374,6 +373,7 @@ struct Foo3{}
|
|||
struct Foo4{}
|
||||
|
||||
fn test_multi_return() {
|
||||
// TODO: multi_return<Foo1, Foo2>()
|
||||
// TODO: temulti_returnst<Foo3, Foo4>()
|
||||
}*/
|
||||
// compiles
|
||||
multi_return<Foo1, Foo2>()
|
||||
multi_return<Foo3, Foo4>()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue