cgen: fix error for cross assign of the reserved name variable (#13884)

pull/13861/head
yuyi 2022-04-01 00:55:21 +08:00 committed by GitHub
parent a87cd9663e
commit db3bbb58cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -584,10 +584,10 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
left_sym := g.table.sym(left_typ) left_sym := g.table.sym(left_typ)
if left_sym.kind == .function { if left_sym.kind == .function {
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos') g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos')
g.writeln(' = $left.name;') g.writeln(' = ${c_name(left.name)};')
} else { } else {
styp := g.typ(left_typ) styp := g.typ(left_typ)
g.writeln('$styp _var_$left.pos.pos = $left.name;') g.writeln('$styp _var_$left.pos.pos = ${c_name(left.name)};')
} }
} }
ast.IndexExpr { ast.IndexExpr {

View File

@ -168,3 +168,24 @@ fn test_cross_assign_of_big_int() {
println(a) println(a)
assert a == big.one_int assert a == big.one_int
} }
fn test_cross_assign_of_reserved_name_variable() {
mut small := 1
mut big := 2
mut sum := 2
for big < 4_000_000 {
small, big = big, small + big
if big % 2 == 0 {
sum += big
}
}
println(small)
assert small == 3524578
println(big)
assert big == 5702887
println(sum)
assert sum == 4613732
}