mirror of
https://github.com/vlang/v.git
synced 2025-09-15 23:42:28 +03:00
cgen: fix sumtype field naming (when they are the same as a C keyword) (#21527)
This commit is contained in:
parent
9e7e323701
commit
cfd23f90fd
2 changed files with 25 additions and 3 deletions
|
@ -2420,9 +2420,9 @@ fn (mut g Gen) write_sumtype_casting_fn(fun SumtypeCastingFn) {
|
||||||
field_styp := g.typ(field.typ)
|
field_styp := g.typ(field.typ)
|
||||||
if got_sym.kind in [.sum_type, .interface_] {
|
if got_sym.kind in [.sum_type, .interface_] {
|
||||||
// the field is already a wrapped pointer; we shouldn't wrap it once again
|
// 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 {
|
} 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}')
|
sb.writeln('};\n}')
|
||||||
|
@ -6579,7 +6579,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
|
||||||
if sym.info.fields.len > 0 {
|
if sym.info.fields.len > 0 {
|
||||||
g.writeln('\t// pointers to common sumtype fields')
|
g.writeln('\t// pointers to common sumtype fields')
|
||||||
for field in sym.info.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('};')
|
g.type_definitions.writeln('};')
|
||||||
|
|
22
vlib/v/tests/struct_field_named_as_c_keyword_test.v
Normal file
22
vlib/v/tests/struct_field_named_as_c_keyword_test.v
Normal 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()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue