From 878d269268c1a53ab192cf0cbbf4c1b28e5b2c97 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Sat, 5 Nov 2022 13:53:51 -0500 Subject: [PATCH] GROOVY-7789 --- .../core/tests/xform/TypeCheckedTests.java | 33 +++++++++++++++++++ .../stc/StaticTypeCheckingVisitor.java | 8 ++++- .../stc/StaticTypeCheckingVisitor.java | 8 ++++- .../stc/StaticTypeCheckingVisitor.java | 9 ++++- 4 files changed, 55 insertions(+), 3 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 278ea76cc0..e2c394f3b9 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 @@ -1578,6 +1578,39 @@ public void testTypeChecked7753() { runConformTest(sources, "[X]"); } + @Test + public void testTypeChecked7789() { + //@formatter:off + String[] sources = { + "Main.groovy", + "import groovy.transform.stc.*\n" + + "@groovy.transform.TypeChecked\n" + + "void test() {\n" + + " def list_size = this.wrap({ list -> list.size() })\n" + + " print list_size([])\n" + + "}\n" + + "def Monad wrap(@ClosureParams(value=FromString, options='U') Closure c) {\n" + + " new Monad<>(c)\n" + + "}\n" + + "test()\n", + + "Monad.groovy", + "import groovy.transform.stc.*\n" + + "class Monad {\n" + + " private final Closure c\n" + + " Monad(@ClosureParams(value=FromString, options='T') Closure c) {\n" + + " this.c = c\n" + + " }\n" + + " def call(T t) {\n" + + " c.call(t)\n" + + " }\n" + + "}\n", + }; + //@formatter:on + + runConformTest(sources, "0"); + } + @Test public void testTypeChecked7804() { //@formatter:off 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 18c20b7307..8df1c23b82 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 @@ -3702,7 +3702,13 @@ private ClassNode[] resolveGenericsFromTypeHint(final ClassNode receiver, final dummyMN.setDeclaringClass(orig.getDeclaringClass()); dummyMN.setGenericsTypes(orig.getGenericsTypes()); } - ClassNode returnType = inferReturnTypeGenerics(receiver, dummyMN, arguments); + GenericsType[] typeArguments = null; // GROOVY-7789 + Expression emc = typeCheckingContext.getEnclosingMethodCall(); + if (emc instanceof MethodCallExpression) { + MethodCallExpression call = (MethodCallExpression) emc; + if (arguments == call.getArguments()) typeArguments = call.getGenericsTypes(); + } + ClassNode returnType = inferReturnTypeGenerics(receiver, dummyMN, arguments, typeArguments); GenericsType[] returnTypeGenerics = returnType.getGenericsTypes(); ClassNode[] inferred = new ClassNode[returnTypeGenerics.length]; for (int i = 0, n = returnTypeGenerics.length; i < n; i += 1) { 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 27868e3451..e4631965e1 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 @@ -3459,7 +3459,13 @@ private ClassNode[] resolveGenericsFromTypeHint(final ClassNode receiver, final dummyMN.setDeclaringClass(orig.getDeclaringClass()); dummyMN.setGenericsTypes(orig.getGenericsTypes()); } - ClassNode returnType = inferReturnTypeGenerics(receiver, dummyMN, arguments); + GenericsType[] typeArguments = null; // GROOVY-7789 + Expression emc = typeCheckingContext.getEnclosingMethodCall(); + if (emc instanceof MethodCallExpression) { + MethodCallExpression call = (MethodCallExpression) emc; + if (arguments == call.getArguments()) typeArguments = call.getGenericsTypes(); + } + ClassNode returnType = inferReturnTypeGenerics(receiver, dummyMN, arguments, typeArguments); GenericsType[] returnTypeGenerics = returnType.getGenericsTypes(); ClassNode[] inferred = new ClassNode[returnTypeGenerics.length]; for (int i = 0, n = returnTypeGenerics.length; i < n; i += 1) { 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 7c627e3720..b7ece205fd 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 @@ -3194,7 +3194,14 @@ private void resolveGenericsFromTypeHint(final ClassNode receiver, final Express methodNode.setGenericsTypes(selectedMethod.getGenericsTypes()); } - returnType = inferReturnTypeGenerics(receiver, methodNode, arguments); + GenericsType[] typeArguments = null; // GROOVY-7789 + Expression emc = typeCheckingContext.getEnclosingMethodCall(); + if (emc instanceof MethodCallExpression) { + MethodCallExpression call = (MethodCallExpression) emc; + if (arguments == call.getArguments()) typeArguments = call.getGenericsTypes(); + } + + returnType = inferReturnTypeGenerics(receiver, methodNode, arguments, typeArguments); GenericsType[] returnTypeGenerics = returnType.getGenericsTypes(); for (int i = 0, n = returnTypeGenerics.length; i < n; i += 1) { GenericsType gt = returnTypeGenerics[i];