Skip to content

Commit

Permalink
checker: fix generic interface interring error (#20098)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Dec 6, 2023
1 parent 21d4407 commit 09d05bb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
49 changes: 46 additions & 3 deletions vlib/v/checker/interface.v
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,52 @@ fn (mut c Checker) resolve_generic_interface(typ ast.Type, interface_type ast.Ty
}
for i, iparam in imethod.params {
param := method.params[i] or { ast.Param{} }
if iparam.typ.has_flag(.generic)
&& c.table.get_type_name(iparam.typ) == gt_name {
inferred_type = param.typ
if iparam.typ.has_flag(.generic) {
param_sym := c.table.sym(iparam.typ)
arg_sym := c.table.sym(param.typ)
if c.table.get_type_name(iparam.typ) == gt_name {
inferred_type = param.typ
} else if arg_sym.info is ast.Array && param_sym.info is ast.Array {
mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type
mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
for {
if mut arg_elem_sym.info is ast.Array
&& mut param_elem_sym.info is ast.Array {
arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type
arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
} else {
if param_elem_sym.name == gt_name {
inferred_type = arg_elem_typ
}
break
}
}
} else if arg_sym.info is ast.ArrayFixed
&& param_sym.info is ast.ArrayFixed {
mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type
mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
for {
if mut arg_elem_sym.info is ast.ArrayFixed
&& mut param_elem_sym.info is ast.ArrayFixed {
arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type
arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
} else {
if param_elem_sym.name == gt_name {
inferred_type = arg_elem_typ
}
break
}
}
} else if arg_sym.info is ast.Map && param_sym.info is ast.Map {
if param_sym.info.key_type.has_flag(.generic)
&& c.table.sym(param_sym.info.key_type).name == gt_name {
inferred_type = arg_sym.info.key_type
}
if param_sym.info.value_type.has_flag(.generic)
&& c.table.sym(param_sym.info.value_type).name == gt_name {
inferred_type = arg_sym.info.value_type
}
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions vlib/v/tests/generic_interface_array_type_infer_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
struct Metadata {}

struct TagOrText {}

pub interface Layout[R] {
render(content []R) string
}

struct DefaultLayout {
}

pub fn (self DefaultLayout) to_layout() Layout[TagOrText] {
return self
}

pub fn (self DefaultLayout) render(content []TagOrText) string {
return 'Hello world'
}

fn test_generic_interface_array_type_infer() {
ret := DefaultLayout{}.to_layout().render([])
println(ret)
assert ret == 'Hello world'
}

0 comments on commit 09d05bb

Please sign in to comment.