Skip to content

cgen: fix codegen for struct array generic field #22413

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

Merged
merged 7 commits into from
Oct 7, 2024
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
35 changes: 28 additions & 7 deletions vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,10 @@ pub fn (mut t Table) generic_type_names(generic_type Type) []string {
}

pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concrete_types []Type) Type {
return t.unwrap_generic_type_ex(typ, generic_names, concrete_types, false)
}

pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, concrete_types []Type, recheck_concrete_types bool) Type {
mut final_concrete_types := []Type{}
mut fields := []StructField{}
mut needs_unwrap_types := []Type{}
Expand All @@ -1877,12 +1881,14 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
match ts.info {
Array {
dims, elem_type := t.get_array_dims(ts.info)
unwrap_typ := t.unwrap_generic_type(elem_type, generic_names, concrete_types)
unwrap_typ := t.unwrap_generic_type_ex(elem_type, generic_names, concrete_types,
recheck_concrete_types)
idx := t.find_or_register_array_with_dims(unwrap_typ, dims)
return new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
ArrayFixed {
unwrap_typ := t.unwrap_generic_type(ts.info.elem_type, generic_names, concrete_types)
unwrap_typ := t.unwrap_generic_type_ex(ts.info.elem_type, generic_names, concrete_types,
recheck_concrete_types)
idx := t.find_or_register_array_fixed(unwrap_typ, ts.info.size, None{}, false)
return new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
Expand All @@ -1892,15 +1898,16 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
return new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
Thread {
unwrap_typ := t.unwrap_generic_type(ts.info.return_type, generic_names, concrete_types)
unwrap_typ := t.unwrap_generic_type_ex(ts.info.return_type, generic_names,
concrete_types, recheck_concrete_types)
idx := t.find_or_register_thread(unwrap_typ)
return new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
Map {
unwrap_key_type := t.unwrap_generic_type(ts.info.key_type, generic_names,
concrete_types)
unwrap_value_type := t.unwrap_generic_type(ts.info.value_type, generic_names,
concrete_types)
unwrap_key_type := t.unwrap_generic_type_ex(ts.info.key_type, generic_names,
concrete_types, recheck_concrete_types)
unwrap_value_type := t.unwrap_generic_type_ex(ts.info.value_type, generic_names,
concrete_types, recheck_concrete_types)
idx := t.find_or_register_map(unwrap_key_type, unwrap_value_type)
return new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
Expand Down Expand Up @@ -1952,6 +1959,20 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
nrt += ']'
idx := t.type_idxs[nrt]
if idx != 0 && t.type_symbols[idx].kind != .placeholder {
if recheck_concrete_types {
fields = ts.info.fields.clone()
for i in 0 .. fields.len {
if !fields[i].typ.has_flag(.generic) {
continue
}
// Map[T], []Type[T]
if t.type_kind(fields[i].typ) in [.array, .array_fixed, .map]
&& t.check_if_elements_need_unwrap(typ, fields[i].typ) {
t.unwrap_generic_type_ex(fields[i].typ, t_generic_names, t_concrete_types,
recheck_concrete_types)
}
}
}
return new_type(idx).derive(typ).clear_flag(.generic)
} else {
// fields type translate to concrete type
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
node.return_type_pos)
}
}
if gs.kind == .struct_ && c.needs_unwrap_generic_type(node.return_type) {
// resolve generic Array[T], Map[T] generics, avoid recursive generic resolving type
if c.ensure_generic_type_specify_type_names(node.return_type, node.return_type_pos) {
c.table.unwrap_generic_type_ex(node.return_type, c.table.cur_fn.generic_names,
c.table.cur_concrete_types, true)
}
}
}
return_sym := c.table.sym(node.return_type)
if return_sym.info is ast.Alias {
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
}
// register generic struct type when current fn is generic fn
if c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0 {
c.table.unwrap_generic_type(node.typ, c.table.cur_fn.generic_names, c.table.cur_concrete_types)
c.table.unwrap_generic_type_ex(node.typ, c.table.cur_fn.generic_names, c.table.cur_concrete_types,
true)
}
if !is_field_zero_struct_init {
c.ensure_type_exists(node.typ, node.pos)
Expand Down
62 changes: 62 additions & 0 deletions vlib/v/tests/structs/struct_array_generic_field_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// callback types
type CBnoret[T] = fn (val T)

type CBnoret2[T] = fn (val T, prev T)

type CBvret[T] = fn (val T) T

type CBvret2[T] = fn (val T, prev T) T

type Callback[T] = CBnoret[T] | CBnoret2[T] | CBvret[T] | CBvret2[T]

interface IObv[T] {
v T
prev T
cb []Callback[T]
}

struct Obv[T] {
mut:
v T
prev T
cb []Callback[T]
}

fn (o Obv[T]) get() T {
return o.v
}

fn (o Obv[T]) do_callbacks() T {
return o.v
}

fn (mut o Obv[T]) set(new_value T) T {
prev := o.v
if prev != new_value {
o.v = new_value
o.prev = prev
}

return o.v
}

fn oo2[T](init T) ?Obv[T] {
return none
}

fn oo[T](init T) Obv[T] {
return Obv[T]{
v: init
}
}

fn test_main() {
one := oo(1)
txt := oo('lala')

println('txt: ${txt.get()}')
println('one: ${one.get()}')

assert txt.get() == 'lala'
assert one.get() == 1
}
Loading