1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix generic method on embed struct (fix #17929) (#17932)

This commit is contained in:
yuyi 2023-04-11 15:04:00 +08:00 committed by GitHub
parent 91874f3244
commit 8b37694760
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -1157,6 +1157,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
typ_sym = g.table.sym(unwrapped_rec_type) typ_sym = g.table.sym(unwrapped_rec_type)
} }
} }
if node.from_embed_types.len > 0 && !typ_sym.has_method(node.name) {
unwrapped_rec_type = node.from_embed_types.last()
typ_sym = g.table.sym(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) {

View File

@ -0,0 +1,32 @@
struct Context {}
pub fn (mut ctx Context) default_route() string {
println('from Context')
return 'from Context'
}
struct App {
Context
}
struct Other {
Context
}
pub fn (mut app Other) default_route() string {
println('from Other')
return 'from Other'
}
fn test_generic_method_on_nested_struct() {
mut app := &App{}
ret1 := call_generic(mut app)
assert ret1 == 'from Context'
mut other := &Other{}
ret2 := call_generic(mut other)
assert ret2 == 'from Other'
}
fn call_generic[T](mut app T) string {
return app.default_route()
}