cgen: fix comptime selector reserved field names (#11199)

pull/11208/head
Louis Schmieder 2021-08-16 13:58:23 +02:00 committed by GitHub
parent b417d3c043
commit 482eecbc13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -20,7 +20,7 @@ fn (mut g Gen) comptime_selector(node ast.ComptimeSelector) {
if node.field_expr.expr is ast.Ident {
if node.field_expr.expr.name == g.comp_for_field_var
&& node.field_expr.field_name == 'name' {
g.write(g.comp_for_field_value.name)
g.write(c_name(g.comp_for_field_value.name))
return
}
}

View File

@ -0,0 +1,25 @@
struct StructWithCReservedWord {
error string
while int
extern int
switch bool
}
fn test_structs_that_have_fields_that_are_reserved_c_words_can_be_iterated() {
foo := StructWithCReservedWord{
error: 'this is an error message'
while: 123
extern: 456
switch: true
}
$for field in StructWithCReservedWord.fields {
$if field.typ is string {
println(foo.$(field.name))
}
}
assert foo.switch
assert foo.extern == 456
assert foo.while == 123
assert foo.error == 'this is an error message'
println(foo)
}