Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix generics method call with struct short syntax args(fix #20030) #20100

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
}