This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new bee95c0 GROOVY-9997: infer closure/lambda params + return types from
cast/coerce
bee95c0 is described below
commit bee95c09a7881ef3b22bdfd3c56fcb1090160824
Author: Eric Milles <[email protected]>
AuthorDate: Mon Mar 22 21:54:30 2021 -0500
GROOVY-9997: infer closure/lambda params + return types from cast/coerce
Conflicts:
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
---
.../transform/stc/StaticTypeCheckingVisitor.java | 19 ++++++++++---------
src/test/groovy/transform/stc/LambdaTest.groovy | 15 +++++++++++++++
.../groovy/console/ui/ButtonOrTextEditor.groovy | 4 ++--
3 files changed, 27 insertions(+), 11 deletions(-)
diff --git
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 8e73f86..4172126 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4087,16 +4087,17 @@ public class StaticTypeCheckingVisitor extends
ClassCodeVisitorSupport {
@Override
public void visitCastExpression(final CastExpression expression) {
- super.visitCastExpression(expression);
- if (!expression.isCoerce()) {
- ClassNode targetType = expression.getType();
- Expression source = expression.getExpression();
- ClassNode expressionType = getType(source);
- if (!checkCast(targetType, source) &&
!isDelegateOrOwnerInClosure(source)) {
- addStaticTypeError("Inconvertible types: cannot cast " +
expressionType.toString(false) + " to " + targetType.toString(false),
expression);
- }
+ ClassNode type = expression.getType();
+ Expression target = expression.getExpression();
+ if (isFunctionalInterface(type)) { // GROOVY-9997
+ processFunctionalInterfaceAssignment(type, target);
+ }
+
+ target.visit(this);
+
+ if (!expression.isCoerce() && !checkCast(type, target) &&
!isDelegateOrOwnerInClosure(target)) {
+ addStaticTypeError("Inconvertible types: cannot cast " +
prettyPrintType(getType(target)) + " to " + prettyPrintType(type), expression);
}
- storeType(expression, expression.getType());
}
private boolean isDelegateOrOwnerInClosure(final Expression exp) {
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy
b/src/test/groovy/transform/stc/LambdaTest.groovy
index 9d6763e..77d1adc 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -330,6 +330,21 @@ final class LambdaTest {
'''
}
+ @Test // GROOVY-9997
+ void testComparator3() {
+ assertScript '''
+ @groovy.transform.TypeChecked
+ void test() {
+ def cast = (Comparator<Integer>) (a, b) -> Integer.compare(a,
b)
+ assert cast.compare(0,0) == 0
+
+ def coerce = ((a, b) -> Integer.compare(a, b)) as
Comparator<Integer>
+ assert coerce.compare(0,0) == 0
+ }
+ test()
+ '''
+ }
+
@Test
void testFunctionWithLocalVariables() {
assertScript '''
diff --git
a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ButtonOrTextEditor.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ButtonOrTextEditor.groovy
index ddb97ce..38992ce 100644
---
a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ButtonOrTextEditor.groovy
+++
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ButtonOrTextEditor.groovy
@@ -44,12 +44,12 @@ class ButtonOrTextEditor extends AbstractCellEditor
implements TableCellEditor {
Component getTableCellEditorComponent(JTable table, Object value, boolean
isSelected, int row, int column) {
if (value instanceof JButton) {
editorComponent = value
- ((JButton) editorComponent).addActionListener({
fireEditingStopped() } as ActionListener)
+ ((JButton) editorComponent).addActionListener({ e ->
fireEditingStopped() } as ActionListener)
} else if (value instanceof JTextArea) {
editorComponent = value
} else if (value) {
editorComponent = new JTextArea(value.toString())
- editorComponent.addFocusListener({ fireEditingCanceled() } as
FocusListener)
+ editorComponent.addFocusListener({ e -> fireEditingCanceled() } as
FocusListener)
} else {
editorComponent = null
}