Skip to content

Commit

Permalink
checker: fix unwrap, when generic structs are used as arguments, in u…
Browse files Browse the repository at this point in the history
…ncalled methods (fix #20132) (#20135)
  • Loading branch information
shove70 committed Dec 10, 2023
1 parent 4938b35 commit fe4e1c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions vlib/v/checker/struct.v
Expand Up @@ -900,6 +900,23 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
generic_names := struct_sym.info.generic_types.map(c.table.sym(it).name)
node.typ = c.table.unwrap_generic_type(node.typ, generic_names, concrete_types)
}
} else if struct_sym.info.generic_types.len > 0
&& struct_sym.info.generic_types.len == struct_sym.info.concrete_types.len
&& c.table.cur_concrete_types.len == 0 {
parent_type := struct_sym.info.parent_type
parent_sym := c.table.sym(parent_type)
for method in parent_sym.methods {
generic_names := struct_sym.info.generic_types.map(c.table.sym(it).name)
for i, param in method.params {
if i == 0 || !param.typ.has_flag(.generic) {
continue
}
param_sym := c.table.sym(param.typ)
if param_sym.kind in [.struct_, .interface_, .sum_type] {
c.table.unwrap_generic_type(param.typ, generic_names, struct_sym.info.concrete_types)
}
}
}
}
}
return node.typ
Expand Down
@@ -0,0 +1,17 @@
struct Param[T] {
}

struct Struct[T] {
}

pub fn (s Struct[T]) method[T](p Param[T]) {
}

fn test_main() {
_ = Struct[int]{}
assert true
// NOTE:
// Do not test call Struct.method() here,
// to test unwrap of generic struct parameters when the method is not called.
// test results only need to can compile.
}

0 comments on commit fe4e1c0

Please sign in to comment.