Skip to content

Commit 2827e14

Browse files
committed
GROOVY-11181: STC: include parameter type in error
3_0_X backport
1 parent 56b4d41 commit 2827e14

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3747,12 +3747,12 @@ private void inferMethodReferenceType(final ClassNode receiver, final ArgumentLi
37473747
if (i >= nthParameter && paramType.isArray())
37483748
paramType = paramType.getComponentType();
37493749

3750-
if (!isFunctionalInterface(paramType.redirect())) {
3751-
addError("The argument is a method reference, but the parameter type is not a functional interface", argumentExpression);
3752-
newArgumentExpressions.add(argumentExpression);
3753-
} else {
3750+
if (isFunctionalInterface(paramType)) {
37543751
methodReferencePositions.add(i);
37553752
newArgumentExpressions.add(constructLambdaExpressionForMethodReference(paramType, (MethodReferenceExpression) argumentExpression));
3753+
} else {
3754+
newArgumentExpressions.add(argumentExpression);
3755+
addStaticTypeError("Argument is a method reference, but parameter type '" + prettyPrintTypeName(paramType) + "' is not a functional interface", argumentExpression);
37563756
}
37573757
}
37583758
}

src/test/groovy/transform/stc/MethodReferenceTest.groovy

+22-2
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ final class MethodReferenceTest {
14941494
baz(this::foo) // not yet supported!
14951495
}
14961496
'''
1497-
assert err =~ /The argument is a method reference, but the parameter type is not a functional interface/
1497+
assert err =~ /Argument is a method reference, but parameter type 'java.lang.Object' is not a functional interface/
14981498
}
14991499

15001500
@Test // GROOVY-10336
@@ -1511,7 +1511,27 @@ final class MethodReferenceTest {
15111511
}
15121512
}
15131513
'''
1514-
assert err =~ /The argument is a method reference, but the parameter type is not a functional interface/
1514+
assert err =~ /Argument is a method reference, but parameter type 'java.lang.Object' is not a functional interface/
1515+
}
1516+
1517+
@Test // GROOVY-10979
1518+
void testNotFunctionalInterface3() {
1519+
def err = shouldFail imports + '''
1520+
Integer m(String x) {
1521+
return 1
1522+
}
1523+
@CompileStatic
1524+
void test() {
1525+
java.util.stream.Stream<Number> x = null
1526+
BiFunction<Function<String, Integer>, Number, Function<String, Integer>> y = null
1527+
BinaryOperator<Function<String, Integer>> z = null
1528+
// reduce number(s) to string-to-integer functions
1529+
x.<Function<String, Integer>>reduce(this::m, y, z)
1530+
x.reduce(this::m, y, z)
1531+
x.reduce((s) -> 1, y, z)
1532+
}
1533+
'''
1534+
assert err =~ /Argument is a method reference, but parameter type 'U' is not a functional interface/
15151535
}
15161536

15171537
@Test // GROOVY-11254

0 commit comments

Comments
 (0)