Skip to content

Commit 784361f

Browse files
authored
checker: fix generic method on aliases receiver type (#14729)
1 parent e1360cc commit 784361f

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

vlib/v/checker/check_types.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
619619
mut to_set := ast.void_type
620620
// resolve generic struct receiver
621621
if node.is_method && param.typ.has_flag(.generic) {
622-
sym := c.table.sym(node.receiver_type)
622+
sym := c.table.final_sym(node.receiver_type)
623623
match sym.info {
624624
ast.Struct, ast.Interface, ast.SumType {
625625
if !isnil(c.table.cur_fn) && c.table.cur_fn.generic_names.len > 0 { // in generic fn

vlib/v/checker/fn.v

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,14 +1193,14 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
11931193
method = m
11941194
has_method = true
11951195
} else {
1196-
if left_sym.kind in [.struct_, .sum_type, .interface_] {
1196+
if final_left_sym.kind in [.struct_, .sum_type, .interface_] {
11971197
mut parent_type := ast.void_type
1198-
if left_sym.info is ast.Struct {
1199-
parent_type = left_sym.info.parent_type
1200-
} else if left_sym.info is ast.SumType {
1201-
parent_type = left_sym.info.parent_type
1202-
} else if left_sym.info is ast.Interface {
1203-
parent_type = left_sym.info.parent_type
1198+
if final_left_sym.info is ast.Struct {
1199+
parent_type = final_left_sym.info.parent_type
1200+
} else if final_left_sym.info is ast.SumType {
1201+
parent_type = final_left_sym.info.parent_type
1202+
} else if final_left_sym.info is ast.Interface {
1203+
parent_type = final_left_sym.info.parent_type
12041204
}
12051205
if parent_type != 0 {
12061206
type_sym := c.table.sym(parent_type)
@@ -1214,7 +1214,7 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
12141214
if !has_method {
12151215
has_method = true
12161216
mut embed_types := []ast.Type{}
1217-
method, embed_types = c.table.find_method_from_embeds(left_sym, method_name) or {
1217+
method, embed_types = c.table.find_method_from_embeds(final_left_sym, method_name) or {
12181218
if err.msg() != '' {
12191219
c.error(err.msg(), node.pos)
12201220
}
@@ -1226,14 +1226,14 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
12261226
node.from_embed_types = embed_types
12271227
}
12281228
}
1229-
if left_sym.kind == .aggregate {
1229+
if final_left_sym.kind == .aggregate {
12301230
// the error message contains the problematic type
12311231
unknown_method_msg = err.msg()
12321232
}
12331233
}
12341234
if has_method {
12351235
// x is Bar<T>, x.foo() -> x.foo<T>()
1236-
rec_sym := c.table.sym(node.left_type)
1236+
rec_sym := c.table.final_sym(node.left_type)
12371237
rec_is_generic := left_type.has_flag(.generic)
12381238
mut rec_concrete_types := []ast.Type{}
12391239
if rec_sym.info is ast.Struct {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module main
2+
3+
struct Container<T> {
4+
value T
5+
}
6+
7+
fn (c Container<T>) id() int {
8+
return 1
9+
}
10+
11+
type Text = Container<string>
12+
13+
fn test_generic_method_on_receiver_aliases_type() {
14+
t := Text{'test'}
15+
println(t.id())
16+
assert t.id() == 1
17+
}

0 commit comments

Comments
 (0)