Skip to content

Commit

Permalink
cgen: fix sumtype field naming (when they are the same as a C keyword) (
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored May 20, 2024
1 parent 9e7e323 commit cfd23f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down Expand Up @@ -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('};')
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/tests/struct_field_named_as_c_keyword_test.v
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit cfd23f9

Please sign in to comment.