From b09553b260dbe9cbd3046f88eff399d2b63c3e61 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 10 May 2022 17:05:08 -0700 Subject: [PATCH] compiler: load LHS subexpressions of op= assignment only once Fixes golang/go#52811 Change-Id: I369415d1188a483cba81257fdfbb0e5c1a7df1c4 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/405617 Reviewed-by: Cherry Mui Reviewed-by: Ian Lance Taylor --- go/statements.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/go/statements.cc b/go/statements.cc index 95fa3c487..b3db84336 100644 --- a/go/statements.cc +++ b/go/statements.cc @@ -1260,6 +1260,16 @@ Assignment_operation_statement::do_lower(Gogo*, Named_object*, Move_ordered_evals moe(b); this->lhs_->traverse_subexpressions(&moe); + // We can still be left with subexpressions that have to be loaded + // even if they don't have side effects themselves, in case the RHS + // changes variables named on the LHS. + int i; + if (this->lhs_->must_eval_subexpressions_in_order(&i)) + { + Move_subexpressions ms(i, b); + this->lhs_->traverse_subexpressions(&ms); + } + Expression* lval = this->lhs_->copy(); Operator op;