Skip to content

Commit

Permalink
ast: fix generics with embed generics structs(fix #20021) (#20022)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 committed Nov 28, 2023
1 parent 46086c0 commit 52f40aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vlib/v/ast/table.v
Expand Up @@ -2110,6 +2110,7 @@ pub fn (mut t Table) generic_insts_to_concrete() {
generic_names := t.get_generic_names(parent_info.generic_types)
for i in 0 .. fields.len {
if fields[i].typ.has_flag(.generic) {
orig_type := fields[i].typ
if fields[i].typ.idx() != info.parent_idx {
fields[i].typ = t.unwrap_generic_type(fields[i].typ,
generic_names, info.concrete_types)
Expand All @@ -2119,6 +2120,15 @@ pub fn (mut t Table) generic_insts_to_concrete() {
{
fields[i].typ = t_typ
}
// Update type in `info.embeds`, if it's embed
if fields[i].name.len > 1 && fields[i].name[0].is_capital() {
for mut embed in parent_info.embeds {
if embed == orig_type {
embed = fields[i].typ.set_flag(.generic)
break
}
}
}
}
}
parent_info.is_generic = false
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/generics_with_embed_generics_structs_test.v
@@ -0,0 +1,12 @@
struct Foo[T] {
field T
}

struct MainStruct[T] {
Foo[T]
}

fn test_main() {
m := MainStruct[int]{}
assert m.field == 0
}

0 comments on commit 52f40aa

Please sign in to comment.