Skip to content

Commit 1dd2ac8

Browse files
committed
fix when "x[i] op= y" evaluates x[i] more than once
When evaluates x twice in "x op= y", which was detectable if evaluating y affects x. We should identify such cases and evaluate y first. For reference see this bug in the go git repository: Fix golang/go#52811
1 parent 6a33e7e commit 1dd2ac8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

go/statements.cc

+12
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,18 @@ Assignment_operation_statement::do_lower(Gogo*, Named_object*,
13021302
go_unreachable();
13031303
}
13041304

1305+
// While we encountering "lhs op= func", we need keep return value to
1306+
// temporary var to avoid func affecting lhs.
1307+
// var val_temp VAL_TYPE = RHS
1308+
if (this->rhs_->classification() ==
1309+
Expression::Expression_classification::EXPRESSION_CALL)
1310+
{
1311+
Temporary_statement *ts =
1312+
make_temporary(this->rhs_->type(), this->rhs_, loc);
1313+
b->add_statement(ts);
1314+
this->rhs_ = Expression::make_temporary_reference(ts, loc);
1315+
}
1316+
13051317
Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
13061318
Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
13071319
if (b->statements()->empty())

0 commit comments

Comments
 (0)