Skip to content

Commit

Permalink
checker: fix generics method call with struct short syntax args(fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 committed Dec 6, 2023
1 parent d564db6 commit 55e86fe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions vlib/v/checker/fn.v
Expand Up @@ -2090,6 +2090,32 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
} else {
method.params[i + 1].typ
}
// If initialize a generic struct with short syntax,
// need to get the parameter information from the original generic method
if is_method_from_embed && arg.expr is ast.StructInit {
expr := arg.expr as ast.StructInit
is_short_syntax := expr.is_short_syntax && expr.typ == ast.void_type
if is_short_syntax {
embed_type := node.from_embed_types.last()
embed_sym := c.table.sym(embed_type)
if embed_sym.info is ast.Struct {
info := embed_sym.info as ast.Struct
if info.concrete_types.len > 0 {
parent_type := info.parent_type
parent_sym := c.table.sym(parent_type)
if parent_sym.info is ast.Struct && parent_sym.info.is_generic {
if f := parent_sym.find_method(method_name) {
exp_arg_typ = if f.is_variadic && i >= f.params.len - 1 {
f.params.last().typ
} else {
f.params[i + 1].typ
}
}
}
}
}
}
}
param_is_mut = false
no_type_promotion = false
}
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/tests/generics_method_call_with_short_syntax_args_test.v
@@ -0,0 +1,22 @@
struct Options[T] {
a T
}

struct Collector[T] {
mut:
a T
}

fn (mut c Collector[T]) use(options Options[T]) {
c.a = options.a
}

struct MainStruct {
Collector[int]
}

fn test_main() {
mut s := MainStruct{}
s.use(a: 1)
assert s.a == 1
}

0 comments on commit 55e86fe

Please sign in to comment.