Skip to content

Commit c51fb47

Browse files
committed
GROOVY-9790, GROOVY-10089, GROOVY-10217
1 parent 1415de8 commit c51fb47

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.util.Iterator;
2727
import java.util.Map;
28+
import java.util.stream.Stream;
2829

2930
import org.codehaus.jdt.groovy.internal.compiler.ast.EventListener;
3031
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyClassScope;
@@ -597,6 +598,28 @@ public void testLambdaScope2() {
597598
runConformTest(sources, "0f1f2f3f4f");
598599
}
599600

601+
@Test // GROOVY-9790
602+
public void testLambdaTypes1() {
603+
assumeTrue(isParrotParser());
604+
605+
for (Object sig : Stream.of("i", "(int i)", "(Integer i)").skip(isAtLeastGroovy(40) ? 0 : 1).toArray()) { // bare name not supported until Groovy 3.0.10
606+
//@formatter:off
607+
String[] sources = {
608+
"Script.groovy",
609+
"@groovy.transform.CompileStatic\n" +
610+
"void test() {\n" +
611+
" java.util.stream.IntStream.range(0, 2).forEach(\n" +
612+
" " + sig + " -> { assert i >= 0 && i < 2 }\n" +
613+
" )\n" +
614+
"}\n" +
615+
"test()\n",
616+
};
617+
//@formatter:on
618+
619+
runConformTest(sources);
620+
}
621+
}
622+
600623
@Test
601624
public void testMultiCatch() {
602625
//@formatter:off

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,31 @@ public void testTypeChecked10088() {
29482948
"----------\n");
29492949
}
29502950

2951+
@Test
2952+
public void testTypeChecked10089() {
2953+
//@formatter:off
2954+
String[] sources = {
2955+
"C.groovy",
2956+
"@groovy.transform.TypeChecked\n" +
2957+
"def test(... attributes) {\n" +
2958+
" List one = [\n" +
2959+
" [id:'x', options:[count:42]]\n" +
2960+
" ]\n" +
2961+
" List two = attributes.collect {\n" +
2962+
" def node = Collections.singletonMap('children', one)\n" +
2963+
" if (node) {\n" +
2964+
" node = node.get('children').find { child -> child['id'] == 'x' }\n" +
2965+
" }\n" +
2966+
" [id: it['id'], name: node['name'], count: node['options']['count']]\n" +
2967+
" }\n" + // ^^^^^^^^^^^^^^^ GroovyCastException (map ctor for Collection)
2968+
"}\n" +
2969+
"print test( [id:'x'] ).first().count\n",
2970+
};
2971+
//@formatter:on
2972+
2973+
runConformTest(sources, "42");
2974+
}
2975+
29512976
@Test
29522977
public void testTypeChecked10091() {
29532978
//@formatter:off
@@ -3094,4 +3119,24 @@ public void testTypeChecked10180() {
30943119

30953120
runConformTest(sources, "a1b2c3.14");
30963121
}
3122+
3123+
@Test
3124+
public void testTypeChecked10217() {
3125+
//@formatter:off
3126+
String[] sources = {
3127+
"Main.groovy",
3128+
"@groovy.transform.TypeChecked\n" +
3129+
"void test(Object o) {\n" +
3130+
" if (o instanceof List) {\n" +
3131+
" print o[0]\n" +
3132+
" def x = (List) o\n" +
3133+
" print x[0]\n" +
3134+
" }\n" +
3135+
"}\n" +
3136+
"test([1])\n",
3137+
};
3138+
//@formatter:on
3139+
3140+
runConformTest(sources, "11");
3141+
}
30973142
}

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,14 +879,18 @@ public void visitBinaryExpression(final BinaryExpression expression) {
879879
}
880880
lType = getType(leftExpression);
881881
} else {
882-
// GRECLIPSE add -- GROOVY-9977, GROOVY-9995
883-
lType = getType(leftExpression);
882+
// GRECLIPSE add -- GROOVY-9953, GROOVY-9977, GROOVY-9995, GROOVY-10089, GROOVY-10217
884883
if (op == ASSIGN) {
884+
lType = getOriginalDeclarationType(leftExpression);
885885
if (isFunctionalInterface(lType)) {
886886
processFunctionalInterfaceAssignment(lType, rightExpression);
887887
} else if (isClosureWithType(lType) && rightExpression instanceof ClosureExpression) {
888888
storeInferredReturnType(rightExpression, getCombinedBoundType(lType.getGenericsTypes()[0]));
889889
}
890+
} else if (leftExpression instanceof VariableExpression && hasInferredReturnType(leftExpression)) {
891+
lType = getInferredReturnType(leftExpression);
892+
} else {
893+
lType = getType(leftExpression);
890894
}
891895
// GRECLIPSE end
892896
rightExpression.visit(this);

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ public void visitVariableExpression(final VariableExpression vexp) {
651651
ClassNode inferredType = localVariable.getNodeMetaData(INFERRED_TYPE);
652652
inferredType = getInferredTypeFromTempInfo(localVariable, inferredType);
653653
if (inferredType != null && !inferredType.equals(OBJECT_TYPE)) {
654+
// GRECLIPSE add -- GROOVY-9790, GROOVY-10217
655+
if (!inferredType.equals(accessedVariable.getType()))
656+
// GRECLIPSE end
654657
vexp.putNodeMetaData(INFERRED_RETURN_TYPE, inferredType);
655658
}
656659
}
@@ -766,6 +769,11 @@ public void visitBinaryExpression(final BinaryExpression expression) {
766769
lType = getType(leftExpression);
767770
} else {
768771
if (op != ASSIGN && op != ELVIS_EQUAL) {
772+
// GRECLIPSE add -- GROOVY-10217
773+
if (leftExpression instanceof VariableExpression && hasInferredReturnType(leftExpression)) {
774+
lType = getInferredReturnType(leftExpression);
775+
} else
776+
// GRECLIPSE end
769777
lType = getType(leftExpression);
770778
} else {
771779
lType = getOriginalDeclarationType(leftExpression);

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,6 @@ public void visitVariableExpression(final VariableExpression vexp) {
587587
final Variable accessedVariable = vexp.getAccessedVariable();
588588
final TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
589589

590-
if (accessedVariable == null) {
591-
return;
592-
}
593-
594590
if (accessedVariable instanceof DynamicVariable) {
595591
// a dynamic variable is either a closure property, a class member referenced from a closure, or an undeclared variable
596592

@@ -657,18 +653,18 @@ public void visitVariableExpression(final VariableExpression vexp) {
657653
}
658654
}
659655
}
660-
} else {
656+
} else if (accessedVariable != null) {
661657
VariableExpression localVariable;
662658
if (accessedVariable instanceof Parameter) {
663-
Parameter parameter = (Parameter) accessedVariable;
664-
localVariable = new ParameterVariableExpression(parameter);
659+
Parameter prm = (Parameter) accessedVariable;
660+
localVariable = new ParameterVariableExpression(prm);
665661
} else {
666662
localVariable = (VariableExpression) accessedVariable;
667663
}
668664

669665
ClassNode inferredType = localVariable.getNodeMetaData(INFERRED_TYPE);
670666
inferredType = getInferredTypeFromTempInfo(localVariable, inferredType);
671-
if (inferredType != null && !isObjectType(inferredType)) {
667+
if (inferredType != null && !isObjectType(inferredType) && !inferredType.equals(accessedVariable.getType())) {
672668
vexp.putNodeMetaData(INFERRED_RETURN_TYPE, inferredType);
673669
}
674670
}
@@ -784,6 +780,11 @@ public void visitBinaryExpression(final BinaryExpression expression) {
784780
lType = getType(leftExpression);
785781
} else {
786782
if (op != ASSIGN && op != ELVIS_EQUAL) {
783+
// GRECLIPSE add -- GROOVY-10217
784+
if (leftExpression instanceof VariableExpression && hasInferredReturnType(leftExpression)) {
785+
lType = getInferredReturnType(leftExpression);
786+
} else
787+
// GRECLIPSE end
787788
lType = getType(leftExpression);
788789
} else {
789790
lType = getOriginalDeclarationType(leftExpression);

0 commit comments

Comments
 (0)