Skip to content

Commit

Permalink
Revert "cgen: fix codegen for struct array with generic field (fix #2…
Browse files Browse the repository at this point in the history
…2406) (#22413)"

This reverts commit 482aad5.
  • Loading branch information
medvednikov committed Oct 7, 2024
1 parent 482aad5 commit e1b842a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 99 deletions.
35 changes: 7 additions & 28 deletions vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -1841,10 +1841,6 @@ 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 @@ -1854,14 +1850,12 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
match ts.info {
Array {
dims, elem_type := t.get_array_dims(ts.info)
unwrap_typ := t.unwrap_generic_type_ex(elem_type, generic_names, concrete_types,
recheck_concrete_types)
unwrap_typ := t.unwrap_generic_type(elem_type, generic_names, 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_ex(ts.info.elem_type, generic_names, concrete_types,
recheck_concrete_types)
unwrap_typ := t.unwrap_generic_type(ts.info.elem_type, generic_names, 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 @@ -1871,16 +1865,15 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
return new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
Thread {
unwrap_typ := t.unwrap_generic_type_ex(ts.info.return_type, generic_names,
concrete_types, recheck_concrete_types)
unwrap_typ := t.unwrap_generic_type(ts.info.return_type, generic_names, 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_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)
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)
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 @@ -1932,20 +1925,6 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
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: 0 additions & 7 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ 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: 1 addition & 2 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,7 @@ 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_ex(node.typ, c.table.cur_fn.generic_names, c.table.cur_concrete_types,
true)
c.table.unwrap_generic_type(node.typ, c.table.cur_fn.generic_names, c.table.cur_concrete_types)
}
if !is_field_zero_struct_init {
c.ensure_type_exists(node.typ, node.pos)
Expand Down
62 changes: 0 additions & 62 deletions vlib/v/tests/structs/struct_array_generic_field_test.v

This file was deleted.

0 comments on commit e1b842a

Please sign in to comment.