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

checker: fix alias to struct generic type #22872

Merged
merged 4 commits into from
Nov 17, 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
66 changes: 40 additions & 26 deletions vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,6 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
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{}
mut nrt := ''
mut c_nrt := ''
ts := t.sym(typ)
Expand Down Expand Up @@ -2005,6 +2004,17 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
recheck_concrete_types)
}
}
// update concrete types
for i in 0 .. ts.info.generic_types.len {
if t_typ := t.convert_generic_type(ts.info.generic_types[i], t_generic_names,
t_concrete_types)
{
final_concrete_types << t_typ
}
}
if final_concrete_types.len > 0 {
t.unwrap_method_types(ts, generic_names, concrete_types, final_concrete_types)
}
}
return new_type(idx).derive(typ).clear_flag(.generic)
} else {
Expand Down Expand Up @@ -2053,27 +2063,6 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
final_concrete_types << t_typ
}
}
if final_concrete_types.len > 0 {
for method in ts.methods {
for i in 1 .. method.params.len {
if method.params[i].typ.has_flag(.generic)
&& method.params[i].typ != method.params[0].typ {
if method.params[i].typ !in needs_unwrap_types {
needs_unwrap_types << method.params[i].typ
}
}
if method.return_type.has_flag(.generic)
&& method.return_type != method.params[0].typ {
if method.return_type !in needs_unwrap_types {
needs_unwrap_types << method.return_type
}
}
}
if final_concrete_types.len == method.generic_names.len {
t.register_fn_concrete_types(method.fkey(), final_concrete_types)
}
}
}
}
}
else {}
Expand All @@ -2093,8 +2082,8 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
info: info
is_pub: ts.is_pub
)
for typ_ in needs_unwrap_types {
t.unwrap_generic_type(typ_, generic_names, concrete_types)
if final_concrete_types.len > 0 {
t.unwrap_method_types(ts, generic_names, concrete_types, final_concrete_types)
}
return new_type(new_idx).derive(typ).clear_flag(.generic)
}
Expand Down Expand Up @@ -2129,8 +2118,8 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
info: info
is_pub: ts.is_pub
)
for typ_ in needs_unwrap_types {
t.unwrap_generic_type(typ_, generic_names, concrete_types)
if final_concrete_types.len > 0 {
t.unwrap_method_types(ts, generic_names, concrete_types, final_concrete_types)
}
return new_type(new_idx).derive(typ).clear_flag(.generic)
}
Expand Down Expand Up @@ -2184,6 +2173,31 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
return typ
}

fn (mut t Table) unwrap_method_types(ts &TypeSymbol, generic_names []string, concrete_types []Type, final_concrete_types []Type) {
mut needs_unwrap_types := []Type{}
for method in ts.get_methods() {
for i in 1 .. method.params.len {
if method.params[i].typ.has_flag(.generic)
&& method.params[i].typ != method.params[0].typ {
if method.params[i].typ !in needs_unwrap_types {
needs_unwrap_types << method.params[i].typ
}
}
if method.return_type.has_flag(.generic) && method.return_type != method.params[0].typ {
if method.return_type !in needs_unwrap_types {
needs_unwrap_types << method.return_type
}
}
}
if final_concrete_types.len == method.generic_names.len {
t.register_fn_concrete_types(method.fkey(), final_concrete_types)
}
}
for typ_ in needs_unwrap_types {
t.unwrap_generic_type(typ_, generic_names, concrete_types)
}
}

// generic struct instantiations to concrete types
pub fn (mut t Table) generic_insts_to_concrete() {
for mut sym in t.type_symbols {
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/aliases/alias_generic_struct_test.v
felipensp marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module main

import math.vec

type Vec4 = vec.Vec4[f32]
type Vec4x = vec.Vec4[f32]

fn test_main() {
mut v := Vec4{0, 0, 0, 1}
v.one()
assert v.x == 1
assert v.y == 1
assert v.z == 1
assert v.w == 1
v.from(Vec4x{3, 3, 3, 3})
assert v.x == 3
assert v.y == 3
assert v.z == 3
assert v.w == 3
}
Loading