Skip to content

Commit

Permalink
GROOVY-9790, GROOVY-10089, GROOVY-10217
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 9, 2021
1 parent 1415de8 commit c51fb47
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;

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

@Test // GROOVY-9790
public void testLambdaTypes1() {
assumeTrue(isParrotParser());

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
//@formatter:off
String[] sources = {
"Script.groovy",
"@groovy.transform.CompileStatic\n" +
"void test() {\n" +
" java.util.stream.IntStream.range(0, 2).forEach(\n" +
" " + sig + " -> { assert i >= 0 && i < 2 }\n" +
" )\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources);
}
}

@Test
public void testMultiCatch() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2948,6 +2948,31 @@ public void testTypeChecked10088() {
"----------\n");
}

@Test
public void testTypeChecked10089() {
//@formatter:off
String[] sources = {
"C.groovy",
"@groovy.transform.TypeChecked\n" +
"def test(... attributes) {\n" +
" List one = [\n" +
" [id:'x', options:[count:42]]\n" +
" ]\n" +
" List two = attributes.collect {\n" +
" def node = Collections.singletonMap('children', one)\n" +
" if (node) {\n" +
" node = node.get('children').find { child -> child['id'] == 'x' }\n" +
" }\n" +
" [id: it['id'], name: node['name'], count: node['options']['count']]\n" +
" }\n" + // ^^^^^^^^^^^^^^^ GroovyCastException (map ctor for Collection)
"}\n" +
"print test( [id:'x'] ).first().count\n",
};
//@formatter:on

runConformTest(sources, "42");
}

@Test
public void testTypeChecked10091() {
//@formatter:off
Expand Down Expand Up @@ -3094,4 +3119,24 @@ public void testTypeChecked10180() {

runConformTest(sources, "a1b2c3.14");
}

@Test
public void testTypeChecked10217() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"void test(Object o) {\n" +
" if (o instanceof List) {\n" +
" print o[0]\n" +
" def x = (List) o\n" +
" print x[0]\n" +
" }\n" +
"}\n" +
"test([1])\n",
};
//@formatter:on

runConformTest(sources, "11");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,18 @@ public void visitBinaryExpression(final BinaryExpression expression) {
}
lType = getType(leftExpression);
} else {
// GRECLIPSE add -- GROOVY-9977, GROOVY-9995
lType = getType(leftExpression);
// GRECLIPSE add -- GROOVY-9953, GROOVY-9977, GROOVY-9995, GROOVY-10089, GROOVY-10217
if (op == ASSIGN) {
lType = getOriginalDeclarationType(leftExpression);
if (isFunctionalInterface(lType)) {
processFunctionalInterfaceAssignment(lType, rightExpression);
} else if (isClosureWithType(lType) && rightExpression instanceof ClosureExpression) {
storeInferredReturnType(rightExpression, getCombinedBoundType(lType.getGenericsTypes()[0]));
}
} else if (leftExpression instanceof VariableExpression && hasInferredReturnType(leftExpression)) {
lType = getInferredReturnType(leftExpression);
} else {
lType = getType(leftExpression);
}
// GRECLIPSE end
rightExpression.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ public void visitVariableExpression(final VariableExpression vexp) {
ClassNode inferredType = localVariable.getNodeMetaData(INFERRED_TYPE);
inferredType = getInferredTypeFromTempInfo(localVariable, inferredType);
if (inferredType != null && !inferredType.equals(OBJECT_TYPE)) {
// GRECLIPSE add -- GROOVY-9790, GROOVY-10217
if (!inferredType.equals(accessedVariable.getType()))
// GRECLIPSE end
vexp.putNodeMetaData(INFERRED_RETURN_TYPE, inferredType);
}
}
Expand Down Expand Up @@ -766,6 +769,11 @@ public void visitBinaryExpression(final BinaryExpression expression) {
lType = getType(leftExpression);
} else {
if (op != ASSIGN && op != ELVIS_EQUAL) {
// GRECLIPSE add -- GROOVY-10217
if (leftExpression instanceof VariableExpression && hasInferredReturnType(leftExpression)) {
lType = getInferredReturnType(leftExpression);
} else
// GRECLIPSE end
lType = getType(leftExpression);
} else {
lType = getOriginalDeclarationType(leftExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,6 @@ public void visitVariableExpression(final VariableExpression vexp) {
final Variable accessedVariable = vexp.getAccessedVariable();
final TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();

if (accessedVariable == null) {
return;
}

if (accessedVariable instanceof DynamicVariable) {
// a dynamic variable is either a closure property, a class member referenced from a closure, or an undeclared variable

Expand Down Expand Up @@ -657,18 +653,18 @@ public void visitVariableExpression(final VariableExpression vexp) {
}
}
}
} else {
} else if (accessedVariable != null) {
VariableExpression localVariable;
if (accessedVariable instanceof Parameter) {
Parameter parameter = (Parameter) accessedVariable;
localVariable = new ParameterVariableExpression(parameter);
Parameter prm = (Parameter) accessedVariable;
localVariable = new ParameterVariableExpression(prm);
} else {
localVariable = (VariableExpression) accessedVariable;
}

ClassNode inferredType = localVariable.getNodeMetaData(INFERRED_TYPE);
inferredType = getInferredTypeFromTempInfo(localVariable, inferredType);
if (inferredType != null && !isObjectType(inferredType)) {
if (inferredType != null && !isObjectType(inferredType) && !inferredType.equals(accessedVariable.getType())) {
vexp.putNodeMetaData(INFERRED_RETURN_TYPE, inferredType);
}
}
Expand Down Expand Up @@ -784,6 +780,11 @@ public void visitBinaryExpression(final BinaryExpression expression) {
lType = getType(leftExpression);
} else {
if (op != ASSIGN && op != ELVIS_EQUAL) {
// GRECLIPSE add -- GROOVY-10217
if (leftExpression instanceof VariableExpression && hasInferredReturnType(leftExpression)) {
lType = getInferredReturnType(leftExpression);
} else
// GRECLIPSE end
lType = getType(leftExpression);
} else {
lType = getOriginalDeclarationType(leftExpression);
Expand Down

0 comments on commit c51fb47

Please sign in to comment.