Skip to content

Commit

Permalink
fix when "x[i] op= y" evaluates x[i] more than once
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tsqua committed May 10, 2022
1 parent 6a33e7e commit 1dd2ac8
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions go/statements.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,18 @@ Assignment_operation_statement::do_lower(Gogo*, Named_object*,
go_unreachable();
}

// While we encountering "lhs op= func", we need keep return value to
// temporary var to avoid func affecting lhs.
// var val_temp VAL_TYPE = RHS
if (this->rhs_->classification() ==
Expression::Expression_classification::EXPRESSION_CALL)
{
Temporary_statement *ts =
make_temporary(this->rhs_->type(), this->rhs_, loc);
b->add_statement(ts);
this->rhs_ = Expression::make_temporary_reference(ts, loc);
}

Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
if (b->statements()->empty())
Expand Down

0 comments on commit 1dd2ac8

Please sign in to comment.