diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a654567a64..73dec0ec1e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5297,13 +5297,16 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) { if field.typ.has_flag(.optional) { // Dont use g.typ() here becuase it will register // optional and we dont want that - last_text := g.type_definitions.after(start_pos).clone() - g.type_definitions.go_back_to(start_pos) styp, base := g.optional_type_name(field.typ) - g.optionals << styp - g.typedefs2.writeln('typedef struct $styp $styp;') - g.type_definitions.writeln('${g.optional_type_text(styp, base)};') - g.type_definitions.write(last_text) + if styp !in g.optionals { + last_text := g.type_definitions.after(start_pos).clone() + g.type_definitions.go_back_to(start_pos) + g.optionals << styp + g.typedefs2.writeln('typedef struct $styp $styp;') + g.type_definitions.writeln('${g.optional_type_text(styp, + base)};') + g.type_definitions.write(last_text) + } } type_name := g.typ(field.typ) field_name := c_name(field.name) diff --git a/vlib/v/tests/option_test.v b/vlib/v/tests/option_test.v index 3f7ab6a9e0..4c84e22716 100644 --- a/vlib/v/tests/option_test.v +++ b/vlib/v/tests/option_test.v @@ -372,3 +372,8 @@ fn test_optional_sum_type() { } assert false } + +struct MultiOptionalFieldTest { + a ?int + b ?int +}