Copilot commented on code in PR #2620:
URL: https://github.com/apache/groovy/pull/2620#discussion_r3456796943


##########
src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java:
##########
@@ -417,6 +418,47 @@ protected void assignToArray(final Expression parent, 
final Expression receiver,
             rhsValueLoader.visit(controller.getAcg()); // assignment 
expression value
     }
 
+    /**
+     * Evaluates the given expression and stores its value in a fresh temporary
+     * variable, returning a loader for that variable. Used to evaluate the
+     * receiver and index of a subscript assignment ahead of the right-hand 
side
+     * (GROOVY-12097) while leaving the operand stack clean.
+     */
+    private VariableSlotLoader evaluateIntoTemporary(final Expression 
expression, final String name) {
+        AsmClassGenerator acg = controller.getAcg();
+        OperandStack operandStack = controller.getOperandStack();
+        CompileStack compileStack = controller.getCompileStack();
+        expression.visit(acg);
+        ClassNode type = operandStack.getTopOperand();
+        if (type.isGenericsPlaceHolder() || 
GenericsUtils.hasPlaceHolders(type)) {
+            type = controller.getTypeChooser().resolveType(expression, 
controller.getClassNode());
+        }
+        int index = compileStack.defineTemporaryVariable(name, type, true);
+        return new VariableSlotLoader(type, index, operandStack);
+    }
+
+    /**
+     * Returns {@code true} if {@code target} occurs (by identity) somewhere 
within
+     * {@code container}. This detects synthetic assignments whose right-hand 
side
+     * reuses the left-hand side node, such as the rewrites of {@code a[i] op= 
b} and
+     * {@code a[i] ?= b}, where the receiver and index are evaluated as part 
of the RHS.
+     */

Review Comment:
   The Javadoc for `isReferencedWithin` currently reads like it works for any 
`Expression` target, but the visitor only checks `BinaryExpression` nodes (and 
the method is only correct when `target` is a `BinaryExpression`). Consider 
tightening the wording so it accurately reflects the implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to