paulk-asert commented on code in PR #2419:
URL: https://github.com/apache/groovy/pull/2419#discussion_r3021425245


##########
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:
   I had trouble with `testInstanceOf29` doing what you said but maybe I 
misunderstood. It gives negative case detection but seems slightly less 
critical than the others. So I merge the above as is. We can refine if you have 
major concerns.



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