[
https://issues.apache.org/jira/browse/GROOVY-11889?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18070306#comment-18070306
]
ASF GitHub Bot commented on GROOVY-11889:
-----------------------------------------
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.
> 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)