From a0d11412d97ab885e72724a93ac0a09cd6f3e0f2 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Thu, 4 Nov 2021 10:14:27 -0500 Subject: [PATCH] GROOVY-10230, GROOVY-10344 --- .../core/tests/xform/TypeCheckedTests.java | 51 +++++++++++++++++++ .../stc/StaticTypeCheckingVisitor.java | 5 +- .../stc/StaticTypeCheckingVisitor.java | 5 +- .../stc/StaticTypeCheckingVisitor.java | 3 ++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java index 84f0fe6e35..e1065c6ac1 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java @@ -3936,6 +3936,37 @@ public void testTypeChecked10228() { runConformTest(sources, "works"); } + @Test + public void testTypeChecked10230() { + //@formatter:off + String[] sources = { + "Main.groovy", + "@groovy.transform.TypeChecked\n" + + "class A {\n" + + " def > T m(T t) {\n" + + " return t\n" + + " }\n" + + "}\n" + + "@groovy.transform.TypeChecked\n" + + "class B extends A {\n" + + " @Override\n" + + " def > T m(T t) {\n" + + " T x = null; super.m(true ? t : x)\n" + + " }\n" + + "}\n" + + "class C {\n" + + "}\n" + + "@groovy.transform.TypeChecked\n" + + "void test() {\n" + + " new B().m(new C<>())\n" + + "}\n" + + "test()\n", + }; + //@formatter:on + + runConformTest(sources); + } + @Test public void testTypeChecked10234() { //@formatter:off @@ -4607,4 +4638,24 @@ public void testTypeChecked10339() { "Groovy:[Static type checking] - Cannot assign value of type java.io.Serializable> to variable of type java.lang.Integer\n" + "----------\n"); } + + @Test + public void testTypeChecked10344() { + //@formatter:off + String[] sources = { + "Main.groovy", + "class C {\n" + + "}\n" + + "def > void m(T t1, T t2) {\n" + + "}\n" + + "@groovy.transform.TypeChecked\n" + + "void test() {\n" + + " m(new C<>(), new C<>())\n" + + "}\n" + + "test()\n", + }; + //@formatter:on + + runConformTest(sources); + } } diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 4be790c688..e0ad19bb5a 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -1217,7 +1217,7 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class // check if constructor call expression makes use of the diamond operator if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) { ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments()); - /* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291 + /* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, et al. if (argumentListExpression.getExpressions().isEmpty()) { adjustGenerics(lType, cceType); } else { @@ -1251,6 +1251,9 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class } inferredType = type; } + if (inferredType.isGenericsPlaceHolder()) // GROOVY-10344: "T t = new C<>()" + inferredType = getCombinedBoundType(inferredType.getGenericsTypes()[0]); + adjustGenerics(inferredType, cceType); storeType(cce, cceType); // GRECLIPSE end diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 62b388490d..03663661da 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -1114,7 +1114,7 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class ClassNode cceType = cce.getType(), inferredType = lType; // check if constructor call expression makes use of the diamond operator if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) { - /* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291 + /* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, et al. ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments()); if (argumentListExpression.getExpressions().isEmpty()) { adjustGenerics(lType, cceType); @@ -1150,6 +1150,9 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class } inferredType = type; } + if (inferredType.isGenericsPlaceHolder()) // GROOVY-10344: "T t = new C<>()" + inferredType = getCombinedBoundType(inferredType.getGenericsTypes()[0]); + adjustGenerics(inferredType, cceType); storeType(cce, cceType); // GRECLIPSE end diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index b82a6bea12..c5725bad73 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -1115,6 +1115,9 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class } inferredType = type; } + if (inferredType.isGenericsPlaceHolder()) // GROOVY-10344: "T t = new C<>()" + inferredType = getCombinedBoundType(inferredType.getGenericsTypes()[0]); + adjustGenerics(inferredType, cceType); storeType(cce, cceType); }