Skip to content

Commit

Permalink
GROOVY-10699, GROOVY-10700, GROOVY-10701
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 23, 2022
1 parent 6d80c05 commit 8cf63f5
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6021,4 +6021,77 @@ public void testTypeChecked10673() {

runConformTest(sources, "1");
}

@Test
public void testTypeChecked10699() {
for (String type : new String[] {"java.util.function.Function<T,T>", "java.util.function.UnaryOperator<T>"}) {
//@formatter:off
String[] sources = {
"Main.groovy",
"def <T> void m(" + type + " f) {\n" +
" print(f.apply(null))\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" m { Double d -> d }\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources, "null");
}
}

@Test
public void testTypeChecked10700() {
//@formatter:off
String[] sources = {
"Main.groovy",
"class D extends C {\n" + // implements A
" @groovy.transform.TypeChecked\n" +
" void test() {\n" +
" print(decode('works'))\n" +
" }\n" +
"}\n" +
"new D().test()\n",

"Peer.groovy",
"interface A {\n" +
" String decode(String s)\n" +
"}\n" +
"class B implements A {\n" +
" String decode(String s) { s }\n" +
"}\n" +
"class C implements A {\n" +
" @Delegate(interfaces=false)\n" +
" private final B b = new B()\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "works");
}

@Test
public void testTypeChecked10701() {
//@formatter:off
String[] sources = {
"Main.groovy",
"def <T> T m1(java.util.function.UnaryOperator<T> op) {\n" +
" return op.apply(null)\n" +
"}\n" +
"double m2(double d) {\n" +
" return Math.PI\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" print(m1(true ? { Double d -> 42.0d } : this.&m2))\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources, "42.0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4840,13 +4840,14 @@ public void visitTernaryExpression(final TernaryExpression expression) {
falseExpression.visit(this);
ClassNode resultType;
ClassNode typeOfFalse = getType(falseExpression);
// handle instanceof cases
/* GRECLIPSE edit -- GROOVY-10701
if (hasInferredReturnType(falseExpression)) {
typeOfFalse = getInferredReturnType(falseExpression);
}
if (hasInferredReturnType(trueExpression)) {
typeOfTrue = getInferredReturnType(trueExpression);
}
*/
/* GRECLIPSE edit -- GROOVY-9972
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
typeOfTrue = checkForTargetType(trueExpression, typeOfTrue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4654,8 +4654,11 @@ && isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
* @param type the inferred type of {@code expr}
*/
private ClassNode checkForTargetType(final Expression expr, final ClassNode type) {
/* GRECLIPSE edit -- GROOVY-10701
ClassNode sourceType = Optional.ofNullable(getInferredReturnType(expr)).orElse(type);

*/
ClassNode sourceType = type;
// GRECLIPSE end
ClassNode targetType = null;
MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod();
BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ public static boolean isGeneratedFunction(final ClassNode type) {
* @return the method node if type is a SAM type, null otherwise
*/
public static MethodNode findSAM(final ClassNode type) {
if (type == null) return null;
if (type.isInterface()) {
MethodNode sam = null;
for (MethodNode mn : type.getAbstractMethods()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ private static boolean equalIncludingGenerics(final ClassNode one, final ClassNo
* Should the target not have any generics this method does nothing.
*/
static void extractGenericsConnections(final Map<GenericsTypeName, GenericsType> connections, final ClassNode type, final ClassNode target) {
if (target == null || target == type || !isUsingGenericsOrIsArrayUsingGenerics(target)) return;
if (target == null || target == type || (!target.isGenericsPlaceHolder() && !isUsingGenericsOrIsArrayUsingGenerics(target))) return;
if (type == null || type == UNKNOWN_PARAMETER_TYPE) return;

if (target.isGenericsPlaceHolder()) {
Expand Down
Loading

0 comments on commit 8cf63f5

Please sign in to comment.