[ 
https://issues.apache.org/jira/browse/GROOVY-12469?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18130607#comment-18130607
 ] 

ASF GitHub Bot commented on GROOVY-12469:
-----------------------------------------

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.





> SC: static compilation inserts wrong cast, derived from generic return type 
> when assigned to a variable
> -------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12469
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12469
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.23, 3.0.25, 5.0.3, 4.0.33, 5.1.3, 6.0.0
>            Reporter: Björn Kautler
>            Priority: Major
>
> Given the following snippet:
> {code:groovy}
> import java.lang.annotation.Annotation
> import java.lang.annotation.Retention
> import java.lang.annotation.Target
> import groovy.transform.CompileStatic
> @CompileStatic
> <T extends Annotation> T getRetention(Class<T> clazz) {
>   Target.getAnnotation(Target)
> }
> @CompileStatic
> def foo() {
>   Annotation x = getRetention(Retention) /*as Annotation*/
> }
> foo().toString()
> {code}
> This compiles fine under all versions, but fails with a 
> {{GroovyCastException}} in all versions.
>  
> The generated byte-code under SC has a cast on {{Retention}} in this, even 
> though the variable is of type {{Annotation}} explicitly.
> Commenting in the {{as Annotation}} cast makes it work.
> Removing the {{Annotation x =}} also makes it work.
> Removing the {{@CompileStatic}} or replacing it by {{@TypeChecked}} of course 
> also makes it work.
>  
> I'd say static-compilation should here not insert an unnecessary cast to 
> {{{}Retention{}}}, but at most a cast to {{Annotation}} as that is, what the 
> variable needs and which should be consistent with JVM type-erasure, and also 
> with the variant without the variable. Even with the method returning 
> {{Annotation}} instead of {{def}} it works just fine. Just if the method 
> declares to return {{Retention}} explicitly, the cast exception comes, but 
> then rightfully.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to