Skip to content

Commit

Permalink
Fix for #1281: partial rollback of raw return type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 23, 2021
1 parent 5e8afc4 commit 9553b6c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ public void testSet8() {
assertType(contents, "xxx", "java.util.List<java.lang.Short>");
}

@Test
public void testSet9() {
String contents =
"@groovy.transform.TypeChecked\n" +
"void test(obj) {\n" +
" if (obj instanceof Map)\n" +
" def xxx = obj.entrySet()\n" +
"}\n";

assertType(contents, "xxx", "java.util.Set<java.util.Map$Entry<java.lang.Object,java.lang.Object>>");
}

@Test
public void testMap0() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public void testTypeChecked1() {
//@formatter:off
String[] sources = {
"Main.groovy",
"import groovy.transform.TypeChecked\n"+
"@TypeChecked\n"+
"@groovy.transform.TypeChecked\n"+
"void method(String message) {\n"+
" if (rareCondition) {\n"+
" println \"Did you spot the error in this ${message.toUppercase()}?\"\n"+
Expand All @@ -47,12 +46,12 @@ public void testTypeChecked1() {

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 4)\n" +
"1. ERROR in Main.groovy (at line 3)\n" +
"\tif (rareCondition) {\n" +
"\t ^^^^^^^^^^^^^\n" +
"Groovy:[Static type checking] - The variable [rareCondition] is undeclared.\n" +
"----------\n" +
"2. ERROR in Main.groovy (at line 5)\n" +
"2. ERROR in Main.groovy (at line 4)\n" +
"\tprintln \"Did you spot the error in this ${message.toUppercase()}?\"\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^\n" +
"Groovy:[Static type checking] - Cannot find matching method java.lang.String#toUppercase()." +
Expand All @@ -65,8 +64,7 @@ public void testTypeChecked2() {
//@formatter:off
String[] sources = {
"Main.groovy",
"import groovy.transform.TypeChecked\n" +
"@TypeChecked\n" +
"@groovy.transform.TypeChecked\n" +
"void method(String message) {\n" +
" List<Integer> ls = new ArrayList<Integer>()\n" +
" ls.add(123)\n" +
Expand All @@ -77,37 +75,15 @@ public void testTypeChecked2() {

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 6)\n" +
"1. ERROR in Main.groovy (at line 5)\n" +
"\tls.add(\'abc\')\n" +
"\t^^^^^^^^^^^^^\n" +
"Groovy:[Static type checking] - Cannot find matching method java.util.ArrayList#add(java.lang.String). Please check if the declared type is correct and if the method exists.\n" +
"----------\n");
}

@Test // https://issues.apache.org/jira/browse/GROOVY-9412
public void testTypeChecked3() {
//@formatter:off
String[] sources = {
"Main.groovy",
"interface I {\n" +
"}\n" +
"enum E implements I {\n" +
" X\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" List<I> list = []\n" +
" list.add(E.X)\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources);
}

@Test
public void testTypeChecked4() {
public void testTypeChecked3() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -125,7 +101,7 @@ public void testTypeChecked4() {
}

@Test
public void testTypeChecked5() {
public void testTypeChecked4() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -143,7 +119,7 @@ public void testTypeChecked5() {
}

@Test
public void testTypeChecked6() {
public void testTypeChecked5() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -161,7 +137,7 @@ public void testTypeChecked6() {
}

@Test
public void testTypeChecked7() {
public void testTypeChecked6() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -181,7 +157,7 @@ public void testTypeChecked7() {
}

@Test
public void testTypeChecked8() {
public void testTypeChecked7() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -202,7 +178,7 @@ public void testTypeChecked8() {
}

@Test
public void testTypeChecked9() {
public void testTypeChecked8() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -222,7 +198,7 @@ public void testTypeChecked9() {
}

@Test
public void testTypeChecked10() {
public void testTypeChecked9() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -237,8 +213,49 @@ public void testTypeChecked10() {
runConformTest(sources);
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1281
public void testTypeChecked10() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" Object v1 = 'a'\n" +
" Object v2 = 'b'\n" +
" [v1, v2].each { v ->\n" +
" if (v instanceof Map) {\n" +
" v.entrySet().each { e ->\n" +
" def s = e.value\n" + // No such property "value" for Object
" if (s instanceof String)\n" +
" e.value = s.toUpperCase()\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");
}

@Test
public void testTypeChecked11() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"@SuppressWarnings('rawtypes')\n" +
"void test(Map args) {\n" +
" Set<String> keys = args.keySet()\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");
}

@Test
public void testTypeChecked12() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down Expand Up @@ -267,7 +284,7 @@ public void testTypeChecked11() {
}

@Test
public void testTypeChecked12() {
public void testTypeChecked13() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -287,7 +304,7 @@ public void testTypeChecked12() {
}

@Test
public void testTypeChecked13() {
public void testTypeChecked14() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -311,7 +328,7 @@ public void testTypeChecked13() {
}

@Test
public void testTypeChecked14() {
public void testTypeChecked15() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down Expand Up @@ -343,7 +360,7 @@ public void testTypeChecked14() {
}

@Test
public void testTypeChecked15() {
public void testTypeChecked16() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down Expand Up @@ -1211,7 +1228,7 @@ public void testTypeChecked9033a() {
" def map = [key: []]\n" +
" map.add('foo','bar')\n" +
"}\n" +
"test()",
"test()\n",
};
//@formatter:on

Expand All @@ -1237,7 +1254,29 @@ public void testTypeChecked9033b() {
" })\n" +
" Iterable<String> list = new LinkedList()\n" +
"}\n" +
"test()",
"test()\n",
};
//@formatter:on

runConformTest(sources);
}

@Test
public void testTypeChecked9412() {
//@formatter:off
String[] sources = {
"Main.groovy",
"interface I {\n" +
"}\n" +
"enum E implements I {\n" +
" X\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" List<I> list = []\n" +
" list.add(E.X)\n" +
"}\n" +
"test()\n",
};
//@formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6079,13 +6079,7 @@ protected ClassNode inferReturnTypeGenerics(

// 2) resolve type parameters of method's enclosing context

if (context != null) {
returnType = applyGenericsContext(context, returnType);

if (receiver.getGenericsTypes() == null && receiver.redirect().getGenericsTypes() != null && GenericsUtils.hasUnresolvedGenerics(returnType)) {
returnType = returnType.getPlainNodeReference(); // GROOVY-10049: do not return "Stream<E>" for raw type "List#stream()"
}
}
if (context != null) returnType = applyGenericsContext(context, returnType);

// 3) resolve bounds of type parameters from calling context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5804,13 +5804,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth

// 2) resolve type parameters of method's enclosing context

if (context != null) {
returnType = applyGenericsContext(context, returnType);

if (receiver.getGenericsTypes() == null && receiver.redirect().getGenericsTypes() != null && GenericsUtils.hasUnresolvedGenerics(returnType)) {
returnType = returnType.getPlainNodeReference(); // GROOVY-10049: do not return "Stream<E>" for raw type "List#stream()"
}
}
if (context != null) returnType = applyGenericsContext(context, returnType);

// 3) resolve bounds of type parameters from calling context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5636,13 +5636,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth

// 2) resolve type parameters of method's enclosing context

if (context != null) {
returnType = applyGenericsContext(context, returnType);

if (receiver.getGenericsTypes() == null && receiver.redirect().getGenericsTypes() != null && GenericsUtils.hasUnresolvedGenerics(returnType)) {
returnType = returnType.getPlainNodeReference(); // GROOVY-10049: do not return "Stream<E>" for raw type "List#stream()"
}
}
if (context != null) returnType = applyGenericsContext(context, returnType);

// 3) resolve bounds of type parameters from calling context

Expand Down

0 comments on commit 9553b6c

Please sign in to comment.