cgen: fix generic method on alias struct receiver (#11080)
parent
3b116d2455
commit
ec39e38e14
|
@ -579,7 +579,12 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
typ_sym := g.table.get_type_symbol(unwrapped_rec_type)
|
mut typ_sym := g.table.get_type_symbol(unwrapped_rec_type)
|
||||||
|
// alias type that undefined this method (not include `str`) need to use parent type
|
||||||
|
if typ_sym.kind == .alias && node.name != 'str' && !typ_sym.has_method(node.name) {
|
||||||
|
unwrapped_rec_type = (typ_sym.info as ast.Alias).parent_type
|
||||||
|
typ_sym = g.table.get_type_symbol(unwrapped_rec_type)
|
||||||
|
}
|
||||||
rec_cc_type := g.cc_type(unwrapped_rec_type, false)
|
rec_cc_type := g.cc_type(unwrapped_rec_type, false)
|
||||||
mut receiver_type_name := util.no_dots(rec_cc_type)
|
mut receiver_type_name := util.no_dots(rec_cc_type)
|
||||||
if typ_sym.kind == .interface_ && (typ_sym.info as ast.Interface).defines_method(node.name) {
|
if typ_sym.kind == .interface_ && (typ_sym.info as ast.Interface).defines_method(node.name) {
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
struct Base {}
|
||||||
|
|
||||||
|
pub fn (base Base) hello() string {
|
||||||
|
return 'hello'
|
||||||
|
}
|
||||||
|
|
||||||
|
type Message = Base
|
||||||
|
|
||||||
|
fn hello<T>(hello_impl T) string {
|
||||||
|
return hello_impl.hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generic_method_on_alias_struct_receiver() {
|
||||||
|
mut message := Message(Base{})
|
||||||
|
ret1 := hello(message)
|
||||||
|
println(ret1)
|
||||||
|
assert ret1 == 'hello'
|
||||||
|
|
||||||
|
ret2 := message.hello()
|
||||||
|
println(ret2)
|
||||||
|
assert ret2 == 'hello'
|
||||||
|
}
|
Loading…
Reference in New Issue