Skip to content

Commit cfd23f9

Browse files
authored
cgen: fix sumtype field naming (when they are the same as a C keyword) (#21527)
1 parent 9e7e323 commit cfd23f9

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,9 +2420,9 @@ fn (mut g Gen) write_sumtype_casting_fn(fun SumtypeCastingFn) {
24202420
field_styp := g.typ(field.typ)
24212421
if got_sym.kind in [.sum_type, .interface_] {
24222422
// the field is already a wrapped pointer; we shouldn't wrap it once again
2423-
sb.write_string(', .${field.name} = ptr->${field.name}')
2423+
sb.write_string(', .${c_name(field.name)} = ptr->${field.name}')
24242424
} else {
2425-
sb.write_string(', .${field.name} = (${field_styp}*)((char*)${ptr} + __offsetof_ptr(${ptr}, ${type_cname}, ${field.name}))')
2425+
sb.write_string(', .${c_name(field.name)} = (${field_styp}*)((char*)${ptr} + __offsetof_ptr(${ptr}, ${type_cname}, ${c_name(field.name)}))')
24262426
}
24272427
}
24282428
sb.writeln('};\n}')
@@ -6579,7 +6579,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
65796579
if sym.info.fields.len > 0 {
65806580
g.writeln('\t// pointers to common sumtype fields')
65816581
for field in sym.info.fields {
6582-
g.type_definitions.writeln('\t${g.typ(field.typ.ref())} ${field.name};')
6582+
g.type_definitions.writeln('\t${g.typ(field.typ.ref())} ${c_name(field.name)};')
65836583
}
65846584
}
65856585
g.type_definitions.writeln('};')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Foo {
2+
do fn () = unsafe { nil }
3+
}
4+
5+
struct Foo2 {
6+
do fn () = unsafe { nil }
7+
}
8+
9+
type CallAlias = Foo | Foo2
10+
11+
fn get() CallAlias {
12+
return Foo{
13+
do: fn () {
14+
println('cool')
15+
}
16+
}
17+
}
18+
19+
fn test_struct_with_a_field_named_with_a_c_keyword() {
20+
a := get().do
21+
a()
22+
}

0 commit comments

Comments
 (0)