cgen: fix clash in generated variant and method names (#10581)

pull/10584/head^2
spaceface 2021-06-27 18:05:32 +02:00 committed by GitHub
parent ea3983a91b
commit 4bed4afef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 6 deletions

View File

@ -326,7 +326,7 @@ fn (mut g Gen) gen_str_for_enum(info ast.Enum, styp string, str_fn_name string)
} else if info.is_multi_allowed {
seen << val
}
g.auto_str_funcs.writeln('\t\tcase ${s}_$val: return _SLIT("$val");')
g.auto_str_funcs.writeln('\t\tcase ${s}__$val: return _SLIT("$val");')
}
g.auto_str_funcs.writeln('\t\tdefault: return _SLIT("unknown enum value");')
g.auto_str_funcs.writeln('\t}')

View File

@ -1201,7 +1201,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
mut cur_enum_expr := ''
mut cur_enum_offset := 0
for i, field in node.fields {
g.enum_typedefs.write_string('\t${enum_name}_$field.name')
g.enum_typedefs.write_string('\t${enum_name}__$field.name')
if field.has_expr {
g.enum_typedefs.write_string(' = ')
expr_str := g.expr_string(field.expr)
@ -3176,7 +3176,7 @@ fn (mut g Gen) expr(node ast.Expr) {
// g.write('${it.mod}${it.enum_name}_$it.val')
// g.enum_expr(node)
styp := g.typ(node.typ)
g.write('${styp}_$node.val')
g.write('${styp}__$node.val')
}
ast.FloatLiteral {
g.write(node.val)
@ -4071,9 +4071,9 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) {
g.write(', ')
}
if is_push[i] {
g.write('sync__Direction_push')
g.write('sync__Direction__push')
} else {
g.write('sync__Direction_pop')
g.write('sync__Direction__pop')
}
}
g.writeln('}));\n')

View File

@ -519,7 +519,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
g.writeln('\t${node.val_var}.name = _SLIT("$attr.name");')
g.writeln('\t${node.val_var}.has_arg = $attr.has_arg;')
g.writeln('\t${node.val_var}.arg = _SLIT("$attr.arg");')
g.writeln('\t${node.val_var}.kind = AttributeKind_$attr.kind;')
g.writeln('\t${node.val_var}.kind = AttributeKind__$attr.kind;')
g.writeln('}')
}

View File

@ -133,3 +133,14 @@ fn test_enum_instance() {
s := 'x $filetype z'
assert s == 'x unknown z'
}
enum Bar {
baz
}
fn (_ Bar) baz() {}
fn test_enum_variant_and_method_name_clash() {
x := Bar.baz
println(x)
}