Skip to content

Commit

Permalink
Fix for #1327: closure parameter type-checking: declared vs inferred
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 30, 2021
1 parent 9577649 commit df8dc75
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,28 @@ public void testTypeChecked20() {
runConformTest(sources);
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1327
public void testTypeChecked21() {
//@formatter:off
String[] sources = {
"Main.groovy",
"def <T> T m(java.util.function.Consumer<? super T> c) {\n" +
" c.accept(null)\n" +
" null\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" this.m { Number n ->\n" +
" n?.toBigInteger()\n" +
" }\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources);
}

@Test
public void testTypeChecked5450() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3651,7 +3651,7 @@ protected void inferClosureParameterTypes(final ClassNode receiver, final Expres
} else {
paramTypes = new ClassNode[n];
for (int i = 0; i < n; i += 1) {
paramTypes[i] = i < samParamTypes.length ? samParamTypes[i] : null;
paramTypes[i] = !p[i].isDynamicTyped() ? p[i].getOriginType() : (i < samParamTypes.length ? samParamTypes[i] : null);
}
}
expression.putNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS, paramTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3335,7 +3335,7 @@ protected void inferClosureParameterTypes(final ClassNode receiver, final Expres
} else {
paramTypes = new ClassNode[n];
for (int i = 0; i < n; i += 1) {
paramTypes[i] = i < samParamTypes.length ? samParamTypes[i] : null;
paramTypes[i] = !p[i].isDynamicTyped() ? p[i].getOriginType() : (i < samParamTypes.length ? samParamTypes[i] : null);
}
}
expression.putNodeMetaData(CLOSURE_ARGUMENTS, paramTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,7 @@ protected void inferClosureParameterTypes(final ClassNode receiver, final Expres
} else { // TODO: error for length mismatch
paramTypes = Arrays.copyOf(samParamTypes, n);
for (int i = 0; i < Math.min(n, samParamTypes.length); i += 1) {
if (!p[i].isDynamicTyped() && isObjectType(paramTypes[i])) paramTypes[i] = p[i].getOriginType(); else //#1327
checkParamType(p[i], paramTypes[i], i == n-1, expression instanceof LambdaExpression);
}
}
Expand Down

0 comments on commit df8dc75

Please sign in to comment.