autofree: generate unique names for each arg

pull/6695/head
Alexander Medvednikov 2020-10-29 08:26:38 +01:00
parent fbd193b27c
commit 71dfec3c4d
2 changed files with 8 additions and 8 deletions

View File

@ -441,7 +441,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if node.free_receiver { if node.free_receiver {
// The receiver expression needs to be freed, use the temp var. // The receiver expression needs to be freed, use the temp var.
fn_name := node.name.replace('.', '_') fn_name := node.name.replace('.', '_')
arg_name := '_arg_expr_${fn_name}_0' arg_name := '_arg_expr_${fn_name}_0_$node.pos.pos'
g.write('/*af receiver arg*/' + arg_name) g.write('/*af receiver arg*/' + arg_name)
} else { } else {
g.expr(node.left) g.expr(node.left)
@ -654,10 +654,10 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
// t := g.new_tmp_var() + '_arg_expr_${name}_$i' // t := g.new_tmp_var() + '_arg_expr_${name}_$i'
fn_name := node.name.replace('.', '_') // can't use name... fn_name := node.name.replace('.', '_') // can't use name...
// t := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i' // t := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i'
t := '_arg_expr_${fn_name}_$i' t := '_arg_expr_${fn_name}_${i}_$node.pos.pos'
// g.called_fn_name = name // g.called_fn_name = name
used := scope.known_var(t) used := false // scope.known_var(t)
mut s := '' mut s := '$t = '
if used { if used {
// This means this tmp var name was already used (the same function was called and // This means this tmp var name was already used (the same function was called and
// `_arg_fnname_1` was already generated). // `_arg_fnname_1` was already generated).
@ -779,7 +779,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
// Use these variables here. // Use these variables here.
fn_name := node.name.replace('.', '_') fn_name := node.name.replace('.', '_')
// name := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i' // name := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i'
name := '_arg_expr_${fn_name}_${i + 1}' name := '_arg_expr_${fn_name}_${i + 1}_$node.pos.pos'
g.write('/*af arg*/' + name) g.write('/*af arg*/' + name)
} }
} else { } else {
@ -790,7 +790,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
// TODO copypasta, move to an inline fn // TODO copypasta, move to an inline fn
fn_name := node.name.replace('.', '_') fn_name := node.name.replace('.', '_')
// name := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i' // name := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i'
name := '_arg_expr_${fn_name}_${i + 1}' name := '_arg_expr_${fn_name}_${i + 1}_$node.pos.pos'
g.write('/*af arg2*/' + name) g.write('/*af arg2*/' + name)
} else { } else {
g.expr(arg.expr) g.expr(arg.expr)

View File

@ -119,10 +119,10 @@ fn optional_str() {
return return
} }
println(pos + 1) println(pos + 1)
// // test assigning an optional to an existing var
mut p := 0 mut p := 0
for { for {
p = opt('foo') or { p = opt('query:$q') or {
break break
} }
break break