Skip to content

Commit ee73f40

Browse files
committed
GROOVY-10082
1 parent bff147b commit ee73f40

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,26 @@ public void testTypeChecked10080() {
26842684
runConformTest(sources, "42");
26852685
}
26862686

2687+
@Test
2688+
public void testTypeChecked10082() {
2689+
//@formatter:off
2690+
String[] sources = {
2691+
"Main.groovy",
2692+
"class A {}\n" +
2693+
"class B extends A {}\n" +
2694+
"@groovy.transform.TypeChecked\n" +
2695+
"void test() {\n" +
2696+
" Closure<A> c = { -> new B() }\n" +
2697+
" print(c() instanceof A)\n" +
2698+
" print(c() instanceof B)\n" +
2699+
"}\n" +
2700+
"test()\n",
2701+
};
2702+
//@formatter:on
2703+
2704+
runConformTest(sources, "truetrue");
2705+
}
2706+
26872707
@Test
26882708
public void testTypeChecked10088() {
26892709
//@formatter:off

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@ private ClassNode infer(ClassNode target, ClassNode source) {
26392639

26402640
protected ClassNode checkReturnType(final ReturnStatement statement) {
26412641
Expression expression = statement.getExpression();
2642-
/* GRECLIPSE edit -- GROOVY-9907, GROOVY-9971, GROOVY-9995, GROOVY-10080
2642+
/* GRECLIPSE edit -- GROOVY-9907, GROOVY-9971, GROOVY-9995, GROOVY-10080, GROOVY-10082
26432643
ClassNode type = getType(expression);
26442644
26452645
if (typeCheckingContext.getEnclosingClosure() != null) {
@@ -2663,6 +2663,10 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
26632663
}
26642664
if (STRING_TYPE.equals(inferredReturnType) && StaticTypeCheckingSupport.isGStringOrGStringStringLUB(type)) {
26652665
type = STRING_TYPE; // implicit "toString()" before return
2666+
} else if (inferredReturnType != null && !inferredReturnType.isGenericsPlaceHolder()
2667+
&& !type.isUsingGenerics() && !type.equals(inferredReturnType) && (inferredReturnType.isInterface()
2668+
? type.implementsInterface(inferredReturnType) : type.isDerivedFrom(inferredReturnType))) {
2669+
type = inferredReturnType; // allow simple covariance
26662670
}
26672671
return type;
26682672
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -2392,13 +2392,17 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
23922392
type = getType(expression);
23932393
}
23942394
if (typeCheckingContext.getEnclosingClosure() != null) {
2395-
// GRECLIPSE add -- GROOVY-9971, GROOVY-9995, GROOVY-10080
2395+
// GRECLIPSE add -- GROOVY-9971, GROOVY-9995, GROOVY-10080, GROOVY-10082
23962396
ClassNode inferredReturnType = getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression());
23972397
if (expression instanceof ConstructorCallExpression) {
23982398
inferDiamondType((ConstructorCallExpression) expression, inferredReturnType != null ? inferredReturnType : DYNAMIC_TYPE);
23992399
}
24002400
if (STRING_TYPE.equals(inferredReturnType) && StaticTypeCheckingSupport.isGStringOrGStringStringLUB(type)) {
24012401
type = STRING_TYPE; // implicit "toString()" before return
2402+
} else if (inferredReturnType != null && !inferredReturnType.isGenericsPlaceHolder()
2403+
&& !type.isUsingGenerics() && !type.equals(inferredReturnType) && (inferredReturnType.isInterface()
2404+
? type.implementsInterface(inferredReturnType) : type.isDerivedFrom(inferredReturnType))) {
2405+
type = inferredReturnType; // allow simple covariance
24022406
}
24032407
// GRECLIPSE end
24042408
return type;

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,7 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
23052305
type = getType(expression);
23062306
}
23072307
if (typeCheckingContext.getEnclosingClosure() != null) {
2308-
/* GRECLIPSE edit -- GROOVY-9971, GROOVY-9995, GROOVY-10080
2308+
/* GRECLIPSE edit -- GROOVY-9971, GROOVY-9995, GROOVY-10080, GROOVY-10082
23092309
if (expression instanceof ConstructorCallExpression) {
23102310
ClassNode inferredClosureReturnType = getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression());
23112311
if (inferredClosureReturnType != null) inferDiamondType((ConstructorCallExpression) expression, inferredClosureReturnType);
@@ -2317,6 +2317,10 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
23172317
}
23182318
if (STRING_TYPE.equals(inferredReturnType) && isGStringOrGStringStringLUB(type)) {
23192319
type = STRING_TYPE; // convert GString to String at the point of return
2320+
} else if (inferredReturnType != null && !inferredReturnType.isGenericsPlaceHolder()
2321+
&& !type.isUsingGenerics() && !type.equals(inferredReturnType) && (inferredReturnType.isInterface()
2322+
? type.implementsInterface(inferredReturnType) : type.isDerivedFrom(inferredReturnType))) {
2323+
type = inferredReturnType; // allow simple covariance
23202324
}
23212325
// GRECLIPSE end
23222326
return type;

0 commit comments

Comments
 (0)