Skip to content

Commit 3a3d5b7

Browse files
eric-millesdaniellansun
authored andcommitted
GROOVY-9953: lookup instanceof type for variable on right of binary expr
1 parent 666627b commit 3a3d5b7

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,9 @@ public void visitBinaryExpression(final BinaryExpression expression) {
763763
}
764764

765765
if (lType == null) lType = getType(leftExpression);
766-
ClassNode rType = (isNullConstant(rightExpression) && !isPrimitiveType(lType)
767-
// primitive types should be ignored as they will result in another failure
768-
? UNKNOWN_PARAMETER_TYPE
769-
: getType(rightExpression)
770-
);
766+
ClassNode rType = isNullConstant(rightExpression) && !isPrimitiveType(lType)
767+
? UNKNOWN_PARAMETER_TYPE // null to primitive type is handled elsewhere
768+
: getInferredTypeFromTempInfo(rightExpression, getType(rightExpression));
771769

772770
BinaryExpression reversedBinaryExpression = binX(rightExpression, expression.getOperation(), leftExpression);
773771
ClassNode resultType = (op == KEYWORD_IN || op == COMPARE_NOT_IN)

src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,23 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
424424
''', 'No such property: y for class: A'
425425
}
426426

427+
// GROOVY-9953
428+
void testInstanceOfPropagatesToLocalVariable() {
429+
assertScript '''
430+
class A {
431+
}
432+
A test(Object x) {
433+
if (x instanceof A) {
434+
def y = x
435+
return y
436+
} else {
437+
new A()
438+
}
439+
}
440+
new A().with { assert test(it) === it }
441+
'''
442+
}
443+
427444
void testShouldNotFailWithWith() {
428445
assertScript '''
429446
class A {

0 commit comments

Comments
 (0)