Skip to content

Commit

Permalink
GROOVY-11181: STC: include parameter type in error
Browse files Browse the repository at this point in the history
4_0_X backport
  • Loading branch information
eric-milles committed Oct 5, 2024
1 parent 6f7898d commit d1182f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3800,12 +3800,12 @@ private void inferMethodReferenceType(final ClassNode receiver, final ArgumentLi
if (i >= nthParameter && paramType.isArray())
paramType = paramType.getComponentType();

if (!isFunctionalInterface(paramType.redirect())) {
addError("The argument is a method reference, but the parameter type is not a functional interface", argumentExpression);
newArgumentExpressions.add(argumentExpression);
} else {
if (isFunctionalInterface(paramType)) {
methodReferencePositions.add(i);
newArgumentExpressions.add(constructLambdaExpressionForMethodReference(paramType, (MethodReferenceExpression) argumentExpression));
} else {
newArgumentExpressions.add(argumentExpression);
addStaticTypeError("Argument is a method reference, but parameter type '" + prettyPrintTypeName(paramType) + "' is not a functional interface", argumentExpression);
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions src/test/groovy/transform/stc/MethodReferenceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ final class MethodReferenceTest {
baz(this::foo) // not yet supported!
}
'''
assert err =~ /The argument is a method reference, but the parameter type is not a functional interface/
assert err =~ /Argument is a method reference, but parameter type 'java.lang.Object' is not a functional interface/
}

@Test // GROOVY-10336
Expand All @@ -1506,7 +1506,27 @@ final class MethodReferenceTest {
}
}
'''
assert err =~ /The argument is a method reference, but the parameter type is not a functional interface/
assert err =~ /Argument is a method reference, but parameter type 'java.lang.Object' is not a functional interface/
}

@Test // GROOVY-10979
void testNotFunctionalInterface3() {
def err = shouldFail shell, '''
Integer m(String x) {
return 1
}
@CompileStatic
void test() {
java.util.stream.Stream<Number> x = null
BiFunction<Function<String, Integer>, Number, Function<String, Integer>> y = null
BinaryOperator<Function<String, Integer>> z = null
// reduce number(s) to string-to-integer functions
x.<Function<String, Integer>>reduce(this::m, y, z)
x.reduce(this::m, y, z)
x.reduce((s) -> 1, y, z)
}
'''
assert err =~ /Argument is a method reference, but parameter type 'U' is not a functional interface/
}

@Test // GROOVY-11254
Expand Down

0 comments on commit d1182f6

Please sign in to comment.