Skip to content

Commit 8337794

Browse files
committed
GROOVY-9991
1 parent 1f25ee5 commit 8337794

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
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
@@ -1314,4 +1314,24 @@ public void testTypeChecked9977() {
13141314

13151315
runConformTest(sources, "-1");
13161316
}
1317+
1318+
@Test
1319+
public void testTypeChecked9991() {
1320+
if (Float.parseFloat(System.getProperty("java.specification.version")) > 8)
1321+
vmArguments = new String[] {"--add-opens", "java.base/java.util.function=ALL-UNNAMED"};
1322+
1323+
//@formatter:off
1324+
String[] sources = {
1325+
"Main.groovy",
1326+
"@groovy.transform.TypeChecked\n" +
1327+
"void test() {\n" +
1328+
" java.util.function.Predicate<?> p = { false }\n" +
1329+
" print p.test(null)\n" +
1330+
"}\n" +
1331+
"test()\n",
1332+
};
1333+
//@formatter:on
1334+
1335+
runConformTest(sources, "false");
1336+
}
13171337
}

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
import static org.codehaus.groovy.ast.ClassHelper.short_TYPE;
179179
import static org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE;
180180
import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
181+
import static org.codehaus.groovy.ast.tools.ClosureUtils.hasImplicitParameter;
181182
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
182183
import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
183184
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
@@ -1051,19 +1052,19 @@ private void processFunctionalInterfaceAssignment(final ClassNode lhsType, final
10511052
Map<GenericsType, GenericsType> mappings = GenericsUtils.makeDeclaringAndActualGenericsTypeMapOfExactType(abstractMethod.getDeclaringClass(), lhsType);
10521053
Function<ClassNode, ClassNode> resolver = t -> t.isGenericsPlaceHolder() ? GenericsUtils.findActualTypeByGenericsPlaceholderName(t.getUnresolvedName(), mappings) : t;
10531054

1054-
ClassNode[] samParameterTypes = Arrays.stream(abstractMethod.getParameters()).map(Parameter::getType).map(resolver).toArray(ClassNode[]::new);
10551055
Parameter[] closureParameters = getParametersSafe((ClosureExpression) rhsExpression);
1056-
int n = closureParameters.length;
1057-
if (n == samParameterTypes.length) {
1058-
for (int i = 0; i < n; i += 1) {
1056+
ClassNode[] samParameterTypes = Arrays.stream(abstractMethod.getParameters()).map(Parameter::getType).map(resolver).toArray(ClassNode[]::new);
1057+
if (closureParameters.length == samParameterTypes.length || (1 == samParameterTypes.length && hasImplicitParameter((ClosureExpression) rhsExpression))) {
1058+
for (int i = 0; i < closureParameters.length; i += 1) {
10591059
Parameter parameter = closureParameters[i];
10601060
if (parameter.isDynamicTyped()) {
10611061
parameter.setType(samParameterTypes[i]);
10621062
parameter.setOriginType(samParameterTypes[i]);
10631063
}
10641064
}
10651065
} else {
1066-
addStaticTypeError("Wrong number of parameters: ", rhsExpression);
1066+
String descriptor = toMethodParametersString(findSAM(lhsType).getName(), samParameterTypes);
1067+
addStaticTypeError("Wrong number of parameters for method target " + descriptor, rhsExpression);
10671068
}
10681069

10691070
storeInferredReturnType(rhsExpression, resolver.apply(abstractMethod.getReturnType()));

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

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
import static org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE;
181181
import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
182182
import static org.codehaus.groovy.ast.tools.ClosureUtils.getResolveStrategyName;
183+
import static org.codehaus.groovy.ast.tools.ClosureUtils.hasImplicitParameter;
183184
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
184185
import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
185186
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
@@ -967,9 +968,15 @@ private void inferParameterAndReturnTypesOfClosureOnRHS(final ClassNode lhsType,
967968
Parameter[] closureParameters = getParametersSafe(rhsExpression);
968969
ClassNode[] parameterTypes = typeInfo.getV1();
969970

971+
/* GRECLIPSE edit -- GROOVY-9991
970972
int n = closureParameters.length;
971973
if (n == parameterTypes.length) {
972974
for (int i = 0; i < n; i += 1) {
975+
*/
976+
if (closureParameters.length == parameterTypes.length
977+
|| (1 == parameterTypes.length && hasImplicitParameter(rhsExpression))) {
978+
for (int i = 0; i < closureParameters.length; i += 1) {
979+
// GRECLIPSE end
973980
Parameter parameter = closureParameters[i];
974981
if (parameter.isDynamicTyped()) {
975982
parameter.setType(parameterTypes[i]);

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

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
import static org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE;
181181
import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
182182
import static org.codehaus.groovy.ast.tools.ClosureUtils.getResolveStrategyName;
183+
import static org.codehaus.groovy.ast.tools.ClosureUtils.hasImplicitParameter;
183184
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
184185
import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
185186
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
@@ -965,9 +966,15 @@ private void inferParameterAndReturnTypesOfClosureOnRHS(final ClassNode lhsType,
965966
Parameter[] closureParameters = getParametersSafe(rhsExpression);
966967
ClassNode[] parameterTypes = typeInfo.getV1();
967968

969+
/* GRECLIPSE edit -- GROOVY-9991
968970
int n = closureParameters.length;
969971
if (n == parameterTypes.length) {
970972
for (int i = 0; i < n; i += 1) {
973+
*/
974+
if (closureParameters.length == parameterTypes.length
975+
|| (1 == parameterTypes.length && hasImplicitParameter(rhsExpression))) {
976+
for (int i = 0; i < closureParameters.length; i += 1) {
977+
// GRECLIPSE end
971978
Parameter parameter = closureParameters[i];
972979
if (parameter.isDynamicTyped()) {
973980
parameter.setType(parameterTypes[i]);

0 commit comments

Comments
 (0)