1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ast, cgen: optimize generic struct with inconsistent generic types (#16708)

This commit is contained in:
yuyi 2022-12-19 21:02:27 +08:00 committed by GitHub
parent bcdf1b95c1
commit 2090e4a12f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -1805,7 +1805,11 @@ pub fn (mut t Table) generic_type_names(generic_type Type) []string {
if sym.info.is_generic {
if sym.generic_types.len > 0 {
// Foo[U] (declaration: Foo[T])
names << sym.generic_types.filter(it.has_flag(.generic)).map(t.sym(it).name)
for typ in sym.generic_types {
if typ.has_flag(.generic) {
names << t.sym(typ).name
}
}
} else {
names << sym.info.generic_types.map(t.sym(it).name)
}

View File

@ -5364,7 +5364,7 @@ fn (mut g Gen) write_sorted_types() {
}
fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
mut struct_names := []string{cap: 16}
mut struct_names := map[string]bool{}
for sym in symbols {
if sym.name.starts_with('C.') {
continue
@ -5379,9 +5379,9 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
mut name := sym.cname
match sym.info {
ast.Struct {
if name !in struct_names {
if !struct_names[name] {
g.struct_decl(sym.info, name, false)
struct_names << name
struct_names[name] = true
}
}
ast.Alias {
@ -5406,10 +5406,10 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
}
}
ast.SumType {
if sym.info.is_generic || name in struct_names {
if sym.info.is_generic || struct_names[name] {
continue
}
struct_names << name
struct_names[name] = true
g.typedefs.writeln('typedef struct ${name} ${name};')
g.type_definitions.writeln('')
g.type_definitions.writeln('// Union sum type ${name} = ')