cgen: fix duplicate optional generation for struct field

pull/8882/head
Joe Conigliaro 2021-02-22 00:00:39 +11:00
parent 514cabd7c8
commit 0470baafa6
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 14 additions and 6 deletions

View File

@ -5297,14 +5297,17 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) {
if field.typ.has_flag(.optional) { if field.typ.has_flag(.optional) {
// Dont use g.typ() here becuase it will register // Dont use g.typ() here becuase it will register
// optional and we dont want that // optional and we dont want that
styp, base := g.optional_type_name(field.typ)
if styp !in g.optionals {
last_text := g.type_definitions.after(start_pos).clone() last_text := g.type_definitions.after(start_pos).clone()
g.type_definitions.go_back_to(start_pos) g.type_definitions.go_back_to(start_pos)
styp, base := g.optional_type_name(field.typ)
g.optionals << styp g.optionals << styp
g.typedefs2.writeln('typedef struct $styp $styp;') g.typedefs2.writeln('typedef struct $styp $styp;')
g.type_definitions.writeln('${g.optional_type_text(styp, base)};') g.type_definitions.writeln('${g.optional_type_text(styp,
base)};')
g.type_definitions.write(last_text) g.type_definitions.write(last_text)
} }
}
type_name := g.typ(field.typ) type_name := g.typ(field.typ)
field_name := c_name(field.name) field_name := c_name(field.name)
g.type_definitions.writeln('\t$type_name $field_name;') g.type_definitions.writeln('\t$type_name $field_name;')

View File

@ -372,3 +372,8 @@ fn test_optional_sum_type() {
} }
assert false assert false
} }
struct MultiOptionalFieldTest {
a ?int
b ?int
}