Skip to content

Commit ed49333

Browse files
committed
GROOVY-9882
1 parent 7ebaf4a commit ed49333

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2020 the original author or authors.
2+
* Copyright 2009-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -5434,4 +5434,50 @@ public void testCompileStatic9872() {
54345434

54355435
runConformTest(sources, "[kv:kv]");
54365436
}
5437+
5438+
@Test
5439+
public void testCompileStatic9882() {
5440+
//@formatter:off
5441+
String[] sources = {
5442+
"Main.groovy",
5443+
"import java.util.function.Supplier\n" +
5444+
"@groovy.transform.CompileStatic\n" +
5445+
"class C {\n" +
5446+
" Supplier<String> p = { 'foo' }\n" +
5447+
" void test() {\n" +
5448+
" Supplier<String> v = { 'bar' }\n" +
5449+
" print(p.get() + v.get())\n" +
5450+
" }\n" +
5451+
"}\n" +
5452+
"new C().test()\n",
5453+
};
5454+
//@formatter:on
5455+
5456+
runConformTest(sources, "foobar");
5457+
}
5458+
5459+
@Test
5460+
public void testCompileStatic9883() {
5461+
//@formatter:off
5462+
String[] sources = {
5463+
"Main.groovy",
5464+
"@groovy.transform.CompileStatic\n" +
5465+
"class C {\n" +
5466+
" java.util.function.Supplier<String> p = {\n" +
5467+
" return java.util.UUID.randomUUID()\n" +
5468+
" }\n" +
5469+
"}\n" +
5470+
"print new C().p.get().class\n",
5471+
};
5472+
//@formatter:on
5473+
5474+
runNegativeTest(sources,
5475+
"----------\n" +
5476+
"1. ERROR in Main.groovy (at line 3)\n" +
5477+
"\tjava.util.function.Supplier<String> p = {\n" +
5478+
"\t ^\n" +
5479+
"Groovy:[Static type checking] - Incompatible generic argument types. " +
5480+
"Cannot assign java.util.function.Supplier <java.util.UUID> to: java.util.function.Supplier <String>\n" +
5481+
"----------\n");
5482+
}
54375483
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,12 @@ public void visitField(final FieldNode node) {
19591959
init
19601960
);
19611961
bexp.setSourcePosition(init);
1962+
/* GRECLIPSE edit -- GROOVY-9882
19621963
typeCheckAssignment(bexp, left, node.getOriginType(), init, getType(init));
1964+
*/
1965+
ClassNode lType = node.getOriginType(), rType = getType(init);
1966+
typeCheckAssignment(bexp, left, lType, init, getResultType(lType, ASSIGN, rType, bexp));
1967+
// GRECLIPSE end
19631968
if (init instanceof ConstructorCallExpression) {
19641969
inferDiamondType((ConstructorCallExpression) init, node.getOriginType());
19651970
}
@@ -4697,15 +4702,15 @@ private ClassNode inferSAMTypeGenericsInAssignment(ClassNode samUsage, MethodNod
46974702

46984703
// extract the generics from the return type
46994704
Map<GenericsTypeName, GenericsType> connections = new HashMap<GenericsTypeName, GenericsType>();
4700-
extractGenericsConnections(connections, getInferredReturnType(closureExpression), sam.getReturnType());
4705+
extractGenericsConnections(connections, getWrapper(getInferredReturnType(closureExpression)), sam.getReturnType());
47014706

47024707
// next we get the block parameter types and set the generics
47034708
// information just like before
47044709
// TODO: add vargs handling
47054710
if (closureExpression.isParameterSpecified()) {
47064711
Parameter[] closureParams = closureExpression.getParameters();
47074712
Parameter[] methodParams = sam.getParameters();
4708-
for (int i = 0; i < closureParams.length; i++) {
4713+
for (int i = 0, n = Math.min(closureParams.length, methodParams.length); i < n; i += 1) {
47094714
ClassNode fromClosure = closureParams[i].getType();
47104715
ClassNode fromMethod = methodParams[i].getType();
47114716
extractGenericsConnections(connections, fromClosure, fromMethod);

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1847,10 +1847,20 @@ public void visitField(final FieldNode node) {
18471847
init
18481848
);
18491849
bexp.setSourcePosition(init);
1850+
/* GRECLIPSE edit -- GROOVY-9882
18501851
typeCheckAssignment(bexp, left, node.getOriginType(), init, getType(init));
1852+
*/
1853+
ClassNode lType = node.getOriginType(), rType = getType(init);
1854+
typeCheckAssignment(bexp, left, lType, init, getResultType(lType, ASSIGN, rType, bexp));
1855+
// GRECLIPSE end
18511856
if (init instanceof ConstructorCallExpression) {
18521857
inferDiamondType((ConstructorCallExpression) init, node.getOriginType());
18531858
}
1859+
// GRECLIPSE add
1860+
else if (init instanceof ClosureExpression && isFunctionalInterface(lType)) {
1861+
inferParameterAndReturnTypesOfClosureOnRHS(lType, (ClosureExpression) init);
1862+
}
1863+
// GRECLIPSE end
18541864
}
18551865
} finally {
18561866
currentField = null;
@@ -4465,15 +4475,15 @@ private ClassNode inferSAMTypeGenericsInAssignment(final ClassNode samType, fina
44654475

44664476
// extract the generics from the return type
44674477
Map<GenericsTypeName, GenericsType> connections = new HashMap<>();
4468-
extractGenericsConnections(connections, getInferredReturnType(closureExpression), abstractMethod.getReturnType());
4478+
extractGenericsConnections(connections, getWrapper(getInferredReturnType(closureExpression)), abstractMethod.getReturnType());
44694479

44704480
// next we get the block parameter types and set the generics
44714481
// information just like before
44724482
// TODO: add vargs handling
44734483
if (closureExpression.isParameterSpecified()) {
44744484
Parameter[] closureParams = closureExpression.getParameters();
44754485
Parameter[] methodParams = abstractMethod.getParameters();
4476-
for (int i = 0, n = closureParams.length; i < n; i += 1) {
4486+
for (int i = 0, n = Math.min(closureParams.length, methodParams.length); i < n; i += 1) {
44774487
ClassNode closureParamType = closureParams[i].getType();
44784488
ClassNode methodParamType = methodParams[i].getType();
44794489
extractGenericsConnections(connections, closureParamType, methodParamType);

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1837,10 +1837,20 @@ public void visitField(final FieldNode node) {
18371837
init
18381838
);
18391839
bexp.setSourcePosition(init);
1840+
/* GRECLIPSE edit -- GROOVY-9882
18401841
typeCheckAssignment(bexp, left, node.getOriginType(), init, getType(init));
1842+
*/
1843+
ClassNode lType = node.getOriginType(), rType = getType(init);
1844+
typeCheckAssignment(bexp, left, lType, init, getResultType(lType, ASSIGN, rType, bexp));
1845+
// GRECLIPSE end
18411846
if (init instanceof ConstructorCallExpression) {
18421847
inferDiamondType((ConstructorCallExpression) init, node.getOriginType());
18431848
}
1849+
// GRECLIPSE add
1850+
else if (init instanceof ClosureExpression && isFunctionalInterface(lType)) {
1851+
inferParameterAndReturnTypesOfClosureOnRHS(lType, (ClosureExpression) init);
1852+
}
1853+
// GRECLIPSE end
18441854
}
18451855
} finally {
18461856
currentField = null;
@@ -4434,15 +4444,15 @@ private ClassNode inferSAMTypeGenericsInAssignment(final ClassNode samType, fina
44344444

44354445
// extract the generics from the return type
44364446
Map<GenericsTypeName, GenericsType> connections = new HashMap<>();
4437-
extractGenericsConnections(connections, getInferredReturnType(closureExpression), abstractMethod.getReturnType());
4447+
extractGenericsConnections(connections, getWrapper(getInferredReturnType(closureExpression)), abstractMethod.getReturnType());
44384448

44394449
// next we get the block parameter types and set the generics
44404450
// information just like before
44414451
// TODO: add vargs handling
44424452
if (closureExpression.isParameterSpecified()) {
44434453
Parameter[] closureParams = closureExpression.getParameters();
44444454
Parameter[] methodParams = abstractMethod.getParameters();
4445-
for (int i = 0, n = closureParams.length; i < n; i += 1) {
4455+
for (int i = 0, n = Math.min(closureParams.length, methodParams.length); i < n; i += 1) {
44464456
ClassNode closureParamType = closureParams[i].getType();
44474457
ClassNode methodParamType = methodParams[i].getType();
44484458
extractGenericsConnections(connections, closureParamType, methodParamType);

0 commit comments

Comments
 (0)