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

ast: fix generic structs with multiple levels of generic embedding #20042

Merged
merged 1 commit into from
Nov 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,7 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
fields = ts.info.fields.clone()
for i in 0 .. fields.len {
if fields[i].typ.has_flag(.generic) {
orig_type := fields[i].typ
sym := t.sym(fields[i].typ)
if sym.kind == .struct_ && fields[i].typ.idx() != typ.idx() {
fields[i].typ = t.unwrap_generic_type(fields[i].typ, t_generic_names,
Expand All @@ -1947,6 +1948,19 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
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() {
mut parent_sym := t.sym(typ)
mut parent_info := parent_sym.info
if mut parent_info is Struct {
for mut embed in parent_info.embeds {
if embed == orig_type {
embed = fields[i].typ.set_flag(.generic)
break
}
}
}
}
}
}
// update concrete types
Expand Down
21 changes: 19 additions & 2 deletions vlib/v/tests/generics_with_embed_generics_structs_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ struct Foo[T] {
field T
}

struct MainStruct[T] {
struct Bar[T] {
Foo[T]
}

struct Baz[T] {
Bar[T]
}

struct OneLevelEmbed[T] {
Foo[T]
}

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

struct MultiLevelEmbed[T] {
Baz[T]
}

fn test_multi_level() {
m := MultiLevelEmbed[int]{}
assert m.field == 0
}
Loading