diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1439ffd0a71a22..f4d3083e55f247 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2420,9 +2420,9 @@ fn (mut g Gen) write_sumtype_casting_fn(fun SumtypeCastingFn) { field_styp := g.typ(field.typ) if got_sym.kind in [.sum_type, .interface_] { // the field is already a wrapped pointer; we shouldn't wrap it once again - sb.write_string(', .${field.name} = ptr->${field.name}') + sb.write_string(', .${c_name(field.name)} = ptr->${field.name}') } else { - sb.write_string(', .${field.name} = (${field_styp}*)((char*)${ptr} + __offsetof_ptr(${ptr}, ${type_cname}, ${field.name}))') + sb.write_string(', .${c_name(field.name)} = (${field_styp}*)((char*)${ptr} + __offsetof_ptr(${ptr}, ${type_cname}, ${c_name(field.name)}))') } } sb.writeln('};\n}') @@ -6579,7 +6579,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { if sym.info.fields.len > 0 { g.writeln('\t// pointers to common sumtype fields') for field in sym.info.fields { - g.type_definitions.writeln('\t${g.typ(field.typ.ref())} ${field.name};') + g.type_definitions.writeln('\t${g.typ(field.typ.ref())} ${c_name(field.name)};') } } g.type_definitions.writeln('};') diff --git a/vlib/v/tests/struct_field_named_as_c_keyword_test.v b/vlib/v/tests/struct_field_named_as_c_keyword_test.v new file mode 100644 index 00000000000000..7feca99512bf3e --- /dev/null +++ b/vlib/v/tests/struct_field_named_as_c_keyword_test.v @@ -0,0 +1,22 @@ +struct Foo { + do fn () = unsafe { nil } +} + +struct Foo2 { + do fn () = unsafe { nil } +} + +type CallAlias = Foo | Foo2 + +fn get() CallAlias { + return Foo{ + do: fn () { + println('cool') + } + } +} + +fn test_struct_with_a_field_named_with_a_c_keyword() { + a := get().do + a() +}