Skip to content

Commit

Permalink
GROOVY-11181: STC: include parameter type in error
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 5, 2024
1 parent 8873fa5 commit 6c31727
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,7 @@ protected void visitMethodCallArguments(final ClassNode receiver, final Argument
expression.putNodeMetaData(PARAMETER_TYPE, lambda.getNodeMetaData(PARAMETER_TYPE));
expression.putNodeMetaData(CLOSURE_ARGUMENTS, lambda.getNodeMetaData(CLOSURE_ARGUMENTS));
} else {
addError("The argument is a method reference, but the parameter type is not a functional interface", expression);
addStaticTypeError("Argument is a method reference, but parameter type '" + prettyPrintTypeName(targetType) + "' is not a functional interface", expression);
}
}
}
Expand Down Expand Up @@ -3500,7 +3500,7 @@ private void checkClosureWithDelegatesTo(final ClassNode receiver, final MethodN
resolveGenericsFromTypeHint(receiver, arguments, mn, resolved);
expression.putNodeMetaData(DELEGATION_METADATA, newDelegationMetadata(resolved[0], stInt));
} else {
addStaticTypeError("Incorrect type hint found in method " + (mn), type);
addStaticTypeError("Incorrect type hint found in method " + mn, type);
}
}
} else {
Expand Down Expand Up @@ -5925,12 +5925,8 @@ public void addError(final String msg, final ASTNode node) {
protected void addStaticTypeError(final String msg, final ASTNode node) {
if (node.getColumnNumber() > 0 && node.getLineNumber() > 0) {
addError(StaticTypesTransformation.STATIC_ERROR_PREFIX + msg, node);
} else {
if (DEBUG_GENERATED_CODE) {
addError(StaticTypesTransformation.STATIC_ERROR_PREFIX + "Error in generated code [" + node.getText() + "] - " + msg, node);
}
// ignore errors which are related to unknown source locations
// because they are likely related to generated code
} else if (DEBUG_GENERATED_CODE) { // if not debug, ignore errors which are related to unknown source locations (generated code)
addError(StaticTypesTransformation.STATIC_ERROR_PREFIX + "Error in generated code [" + node.getText() + "] - " + msg, node);
}
}

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 @@ -1456,7 +1456,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 @@ -1473,7 +1473,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 6c31727

Please sign in to comment.