Skip to content

Commit 0fab592

Browse files
committed
GROOVY-10114
1 parent d4fac05 commit 0fab592

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -3090,7 +3090,8 @@ public void testTypeChecked9983() {
30903090
" C.m(new A<>(new B()))\n" +
30913091
" C.m(new A<>(new D()))\n" +
30923092
" C.m(flag ? new A<>(new B()) : new A<>(new B()))\n" +
3093-
" C.m(flag ? new A<>(new B()) : new A<>(new D()))\n" + // Cannot call m(A<B>) with arguments [A<? extends B>]\n" +
3093+
" C.m(flag ? new A<>(new B()) : new A<>((B)null))\n" +
3094+
" C.m(flag ? new A<>(new B()) : new A<>((B)new D()))\n" + // Cannot call m(A<B>) with arguments [A<? extends B>]\n" +
30943095
"}\n" +
30953096
"test()\n",
30963097
};
@@ -3995,6 +3996,36 @@ public void testTypeChecked10111a() {
39953996
runConformTest(sources);
39963997
}
39973998

3999+
@Test
4000+
public void testTypeChecked10114() {
4001+
//@formatter:off
4002+
String[] sources = {
4003+
"Main.groovy",
4004+
"class C<T> {\n" +
4005+
" T p\n" +
4006+
" C(T p) {\n" +
4007+
" this.p = p\n" +
4008+
" }\n" +
4009+
"}\n" +
4010+
"class D {\n" +
4011+
" Character m() {\n" +
4012+
" (Character) '!'\n" +
4013+
" }\n" +
4014+
"}\n" +
4015+
"@groovy.transform.TypeChecked\n" +
4016+
"void test() {\n" +
4017+
" print((false ? new C<D>(new D()) : new C<>(new D())).p.m())\n" +
4018+
" print((false ? new C< >(new D()) : new C<>(new D())).p.m())\n" +
4019+
" def c = (true ? new C<>(new D()) : new C<>(new D()))\n" +
4020+
" print c.p.m()\n" +
4021+
"}\n" +
4022+
"test()\n",
4023+
};
4024+
//@formatter:on
4025+
4026+
runConformTest(sources, "!!!");
4027+
}
4028+
39984029
@Test
39994030
public void testTypeChecked10128() {
40004031
//@formatter:off

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -4938,7 +4938,7 @@ && isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
49384938
}
49394939

49404940
private ClassNode checkForTargetType(final Expression expr, final ClassNode type) {
4941-
/* GRECLIPSE edit -- GROOVY-9972, GROOVY-9983
4941+
/* GRECLIPSE edit -- GROOVY-9972, GROOVY-9983, GROOVY-10114
49424942
BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
49434943
if (enclosingBinaryExpression instanceof DeclarationExpression
49444944
&& isEmptyCollection(expr) && isAssignment(enclosingBinaryExpression.getOperation().getType())) {
@@ -4971,11 +4971,14 @@ && isTypeSource(expr, enclosingMethod)) {
49714971
targetType = enclosingMethod.getReturnType();
49724972
}
49734973

4974-
if (targetType == null) return sourceType;
4975-
49764974
if (expr instanceof ConstructorCallExpression) {
4975+
if (targetType == null) targetType = OBJECT_TYPE;
49774976
inferDiamondType((ConstructorCallExpression) expr, targetType);
4978-
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
4977+
}
4978+
4979+
if (targetType == null) return sourceType;
4980+
4981+
if (!isPrimitiveType(getUnwrapper(targetType))
49794982
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
49804983
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
49814984
// the inferred type is the RHS type "completed" with generics information from LHS

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -4823,7 +4823,7 @@ && isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
48234823
}
48244824

48254825
private ClassNode checkForTargetType(final Expression expr, final ClassNode type) {
4826-
/* GRECLIPSE edit -- GROOVY-9972, GROOVY-9983
4826+
/* GRECLIPSE edit -- GROOVY-9972, GROOVY-9983, GROOVY-10114
48274827
BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
48284828
if (enclosingBinaryExpression instanceof DeclarationExpression
48294829
&& isEmptyCollection(expr) && isAssignment(enclosingBinaryExpression.getOperation().getType())) {
@@ -4856,11 +4856,15 @@ && isTypeSource(expr, enclosingMethod)) {
48564856
targetType = enclosingMethod.getReturnType();
48574857
}
48584858

4859-
if (targetType == null) return sourceType;
4860-
48614859
if (expr instanceof ConstructorCallExpression) {
4860+
if (targetType == null) targetType = OBJECT_TYPE;
48624861
inferDiamondType((ConstructorCallExpression) expr, targetType);
4863-
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
4862+
return sourceType;
4863+
}
4864+
4865+
if (targetType == null) return sourceType;
4866+
4867+
if (!isPrimitiveType(getUnwrapper(targetType))
48644868
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
48654869
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
48664870
// the inferred type is the RHS type "completed" with generics information from LHS

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -4219,11 +4219,16 @@ && isTypeSource(expr, enclosingMethod)) {
42194219
targetType = enclosingMethod.getReturnType();
42204220
}
42214221

4222+
if (expr instanceof ConstructorCallExpression) { // GROOVY-9972, GROOVY-9983
4223+
// GROOVY-10114: type parameter(s) could be inferred from call arguments
4224+
if (targetType == null) targetType = sourceType.getPlainNodeReference();
4225+
inferDiamondType((ConstructorCallExpression) expr, targetType);
4226+
return sourceType;
4227+
}
4228+
42224229
if (targetType == null) return sourceType;
42234230

4224-
if (expr instanceof ConstructorCallExpression) {
4225-
inferDiamondType((ConstructorCallExpression) expr, targetType);
4226-
} else if (!isPrimitiveType(getUnwrapper(targetType))
4231+
if (!isPrimitiveType(getUnwrapper(targetType))
42274232
&& !isObjectType(targetType) && missesGenericsTypes(sourceType)) {
42284233
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
42294234
// the inferred type is the RHS type "completed" with generics information from LHS

0 commit comments

Comments
 (0)