Skip to content

Commit fc4caf4

Browse files
committed
GROOVY-9948
1 parent 412a64f commit fc4caf4

File tree

4 files changed

+80
-18
lines changed

4 files changed

+80
-18
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -891,4 +891,25 @@ public void testTypeChecked9945() {
891891

892892
runConformTest(sources, "42");
893893
}
894+
895+
@Test
896+
public void testTypeChecked9948() {
897+
//@formatter:off
898+
String[] sources = {
899+
"Main.groovy",
900+
"@groovy.transform.TupleConstructor\n" +
901+
"class C<T> {\n" +
902+
" T p\n" +
903+
"}\n" +
904+
"@groovy.transform.TypeChecked\n" +
905+
"void test() {\n" +
906+
" C<Integer> c = new C<>(1)\n" +
907+
" print(c.p < 10)\n" +
908+
"}\n" +
909+
"test()\n",
910+
};
911+
//@formatter:on
912+
913+
runConformTest(sources, "true");
914+
}
894915
}

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -1111,20 +1111,33 @@ protected ClassNode getOriginalDeclarationType(Expression lhs) {
11111111
}
11121112

11131113
protected void inferDiamondType(final ConstructorCallExpression cce, final ClassNode lType) {
1114+
ClassNode cceType = cce.getType(), inferredType = lType;
11141115
// check if constructor call expression makes use of the diamond operator
1115-
ClassNode node = cce.getType();
1116-
if (node.isUsingGenerics() && node.getGenericsTypes() != null && node.getGenericsTypes().length == 0) {
1116+
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
11171117
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
1118+
/* GRECLIPSE edit -- GROOVY-9948
11181119
if (argumentListExpression.getExpressions().isEmpty()) {
1119-
adjustGenerics(lType, node);
1120+
adjustGenerics(lType, cceType);
11201121
} else {
11211122
ClassNode type = getType(argumentListExpression.getExpression(0));
11221123
if (type.isUsingGenerics()) {
1123-
adjustGenerics(type, node);
1124+
adjustGenerics(type, cceType);
11241125
}
11251126
}
11261127
// store inferred type on CCE
1127-
storeType(cce, node);
1128+
storeType(cce, cceType);
1129+
*/
1130+
ConstructorNode constructor = cce.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
1131+
if (!argumentListExpression.getExpressions().isEmpty() && constructor != null) {
1132+
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
1133+
type = inferReturnTypeGenerics(type, constructor, argumentListExpression);
1134+
if (type.isUsingGenerics()) {
1135+
inferredType = type;
1136+
}
1137+
}
1138+
adjustGenerics(inferredType, cceType);
1139+
storeType(cce, cceType);
1140+
// GRECLIPSE end
11281141
}
11291142
}
11301143

@@ -5441,7 +5454,7 @@ protected ClassNode inferReturnTypeGenerics(
54415454
MethodNode method,
54425455
Expression arguments,
54435456
GenericsType[] explicitTypeHints) {
5444-
ClassNode returnType = method.getReturnType();
5457+
ClassNode returnType = method instanceof ConstructorNode ? method.getDeclaringClass() : method.getReturnType(); // GRECLIPSE edit -- GROOVY-9948
54455458
if (method instanceof ExtensionMethodNode
54465459
&& (isUsingGenericsOrIsArrayUsingGenerics(returnType))) {
54475460
// check if the placeholder corresponds to the placeholder of the first parameter

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -1046,20 +1046,34 @@ protected ClassNode getOriginalDeclarationType(final Expression lhs) {
10461046
}
10471047

10481048
protected void inferDiamondType(final ConstructorCallExpression cce, final ClassNode lType) {
1049+
ClassNode cceType = cce.getType(), inferredType = lType;
10491050
// check if constructor call expression makes use of the diamond operator
1050-
ClassNode node = cce.getType();
1051-
if (node.isUsingGenerics() && node.getGenericsTypes() != null && node.getGenericsTypes().length == 0) {
1051+
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
1052+
/* GRECLIPSE edit -- GROOVY-9948
10521053
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
10531054
if (argumentListExpression.getExpressions().isEmpty()) {
1054-
adjustGenerics(lType, node);
1055+
adjustGenerics(lType, cceType);
10551056
} else {
10561057
ClassNode type = getType(argumentListExpression.getExpression(0));
10571058
if (type.isUsingGenerics()) {
1058-
adjustGenerics(type, node);
1059+
adjustGenerics(type, cceType);
10591060
}
10601061
}
10611062
// store inferred type on CCE
1062-
storeType(cce, node);
1063+
storeType(cce, cceType);
1064+
*/
1065+
ArgumentListExpression argumentList = InvocationWriter.makeArgumentList(cce.getArguments());
1066+
ConstructorNode constructor = cce.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
1067+
if (!argumentList.getExpressions().isEmpty() && constructor != null) {
1068+
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
1069+
type = inferReturnTypeGenerics(type, constructor, argumentList);
1070+
if (type.isUsingGenerics()) {
1071+
inferredType = type;
1072+
}
1073+
}
1074+
adjustGenerics(inferredType, cceType);
1075+
storeType(cce, cceType);
1076+
// GRECLIPSE end
10631077
}
10641078
}
10651079

@@ -5226,7 +5240,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth
52265240
* @return parameterized, infered, class node
52275241
*/
52285242
protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final MethodNode method, final Expression arguments, final GenericsType[] explicitTypeHints) {
5229-
ClassNode returnType = method.getReturnType();
5243+
ClassNode returnType = method instanceof ConstructorNode ? method.getDeclaringClass() : method.getReturnType(); // GRECLIPSE edit -- GROOVY-9948
52305244
if (getGenericsWithoutArray(returnType) == null) {
52315245
return returnType;
52325246
}

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -1044,20 +1044,34 @@ protected ClassNode getOriginalDeclarationType(final Expression lhs) {
10441044
}
10451045

10461046
protected void inferDiamondType(final ConstructorCallExpression cce, final ClassNode lType) {
1047+
ClassNode cceType = cce.getType(), inferredType = lType;
10471048
// check if constructor call expression makes use of the diamond operator
1048-
ClassNode node = cce.getType();
1049-
if (node.isUsingGenerics() && node.getGenericsTypes() != null && node.getGenericsTypes().length == 0) {
1049+
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
1050+
/* GRECLIPSE edit -- GROOVY-9948
10501051
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
10511052
if (argumentListExpression.getExpressions().isEmpty()) {
1052-
adjustGenerics(lType, node);
1053+
adjustGenerics(lType, cceType);
10531054
} else {
10541055
ClassNode type = getType(argumentListExpression.getExpression(0));
10551056
if (type.isUsingGenerics()) {
1056-
adjustGenerics(type, node);
1057+
adjustGenerics(type, cceType);
10571058
}
10581059
}
10591060
// store inferred type on CCE
1060-
storeType(cce, node);
1061+
storeType(cce, cceType);
1062+
*/
1063+
ArgumentListExpression argumentList = InvocationWriter.makeArgumentList(cce.getArguments());
1064+
ConstructorNode constructor = cce.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
1065+
if (!argumentList.getExpressions().isEmpty() && constructor != null) {
1066+
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
1067+
type = inferReturnTypeGenerics(type, constructor, argumentList);
1068+
if (type.isUsingGenerics()) {
1069+
inferredType = type;
1070+
}
1071+
}
1072+
adjustGenerics(inferredType, cceType);
1073+
storeType(cce, cceType);
1074+
// GRECLIPSE end
10611075
}
10621076
}
10631077

@@ -5192,7 +5206,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth
51925206
* @return parameterized, infered, class node
51935207
*/
51945208
protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final MethodNode method, final Expression arguments, final GenericsType[] explicitTypeHints) {
5195-
ClassNode returnType = method.getReturnType();
5209+
ClassNode returnType = method instanceof ConstructorNode ? method.getDeclaringClass() : method.getReturnType(); // GRECLIPSE edit -- GROOVY-9948
51965210
if (getGenericsWithoutArray(returnType) == null) {
51975211
return returnType;
51985212
}

0 commit comments

Comments
 (0)