cgen: fix sumtype field naming (when they are the same as a C keyword) (#21527)

This commit is contained in:
Felipe Pena 2024-05-20 02:58:04 -03:00 committed by GitHub
parent 9e7e323701
commit cfd23f90fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 3 deletions

View file

@ -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('};')

View file

@ -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()
}