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


##########
src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java:
##########
@@ -1140,6 +1141,28 @@ protected void evaluateCompoundAssign(final String 
assignName, final String base
         controller.getCompileStack().popLHS();
     }
 
+    /**
+     * The type chooser may give a declared variable a type narrower than its
+     * declared type (flow typing). When the value on the stack is not already 
of
+     * that narrower type (for example, it was inferred from generics but the
+     * erased type is wider), casting to it could fail where the declared type
+     * would not, so the declared type is used instead. Uses of the variable 
that
+     * rely on the narrower type cast at the point of use.
+     */
+    private static ClassNode declaredTypeIfNarrowingNotGuaranteed(final 
VariableExpression var, final ClassNode lhsType, final ClassNode rhsType) {
+        ClassNode declaredType = var.getOriginType();
+        if (var.isDynamicTyped() || declaredType.equals(lhsType)
+                || ClassHelper.isPrimitiveType(declaredType) || 
ClassHelper.isPrimitiveType(lhsType) || ClassHelper.isPrimitiveType(rhsType)
+                || !isSubtype(lhsType, declaredType) || isSubtype(rhsType, 
lhsType)) {
+            return lhsType;
+        }
+        return declaredType;
+    }
+
+    private static boolean isSubtype(final ClassNode type, final ClassNode 
superType) {
+        return type.isDerivedFrom(superType) || 
type.implementsInterface(superType);
+    }
+

Review Comment:
   `isSubtype` duplicates existing type-relation utilities and is a narrower 
check than STC’s helpers. To keep subtype/assignability logic consistent across 
the static compiler (and avoid subtle mismatches for arrays, placeholders, 
traits, redirects, etc.), consider replacing `isSubtype(...)` with 
`StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(...)` (or 
`isAssignableTo(...)` if that’s the intended relation) and removing the local 
helper.



-- 
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