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

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

eric-milles commented on code in PR #2419:
URL: https://github.com/apache/groovy/pull/2419#discussion_r3017798319


##########
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java:
##########
@@ -982,7 +982,16 @@ && 
isAssignment(enclosingBinaryExpression.getOperation().getType())) {
             } else if (op == KEYWORD_INSTANCEOF) {
                 rType = rightExpression.getType();
                 if (lType instanceof 
WideningCategories.LowestUpperBoundClassNode) lType = 
lType.getUnresolvedSuperClass();
-                if (!lType.isInterface() && !rType.isInterface() && 
!(lType.isDerivedFrom(rType) || rType.isDerivedFrom(lType))) {
+                if (lType instanceof UnionTypeClassNode union) {
+                    boolean compatible = rType.isInterface();
+                    for (int i = 0; !compatible && i < 
union.getDelegates().length; i += 1) {
+                        ClassNode delegate = union.getDelegates()[i];
+                        compatible = delegate.isInterface() || 
delegate.isDerivedFrom(rType) || rType.isDerivedFrom(delegate);
+                    }
+                    if (!compatible) {
+                        addError("Incompatible instanceof types: " + 
prettyPrintTypeName(lType) + " and " + prettyPrintTypeName(rType), expression);
+                    }
+                } else if (!lType.isInterface() && !rType.isInterface() && 
!(lType.isDerivedFrom(rType) || rType.isDerivedFrom(lType))) {

Review Comment:
   Not sure how advanced you want to make this.  I added "if (lType instanceof 
UnionTypeClassNode) lType = getOriginalDeclarationType(leftExpression);" above 
the LUB check and had no other fails.





> STC: instanceof check rejects UnionTypeClassNode members as incompatible types
> ------------------------------------------------------------------------------
>
>                 Key: GROOVY-11889
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11889
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to