Skip to content

Commit

Permalink
GROOVY-7333, GROOVY-9769
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 3, 2020
1 parent 15659f6 commit b141d4e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3457,6 +3457,24 @@ public void testInstanceOf16() {
assertType(contents, "value", "java.lang.Object");
}

@Test // GROOVY-9769
public void testInstanceOf17() {
String contents =
"interface A {\n" +
"}\n" +
"interface B extends A {\n" +
" def foo()\n" +
"}\n" +
"@groovy.transform.CompileStatic\n" +
"void test(A a) {\n" +
" if (a instanceof B) {\n" +
" a.foo()\n" +
" }\n" +
"}\n";

assertType(contents, "a", "B"); // not <UnionType:A+B>
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1101
public void testEqualsClassTest1() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,27 @@ public void testTypeChecked9() {
runNegativeTest(sources, "");
}

@Test // GROOVY-7363: don't match bridge method
public void testTypeChecked10() {
@Test
public void testTypeChecked7333() {
//@formatter:off
String[] sources = {
"Main.groovy",
"int len(byte[] bytes) { bytes.length }\n" +
"@groovy.transform.TypeChecked\n" +
"void test(arg) {\n" +
" if (arg instanceof byte[]) {\n" +
" print(len(arg))\n" +
" }\n" +
"}\n" +
"test(new byte[3])\n",
};
//@formatter:on

runConformTest(sources, "3");
}

@Test // don't match bridge method
public void testTypeChecked7363() {
//@formatter:off
String[] sources = {
"Face.java",
Expand Down Expand Up @@ -244,8 +263,8 @@ public void testTypeChecked10() {
runNegativeTest(sources, "");
}

@Test // GROOVY-8103
public void testTypeChecked11() {
@Test
public void testTypeChecked8103() {
//@formatter:off
String[] sources = {
"Script.groovy",
Expand Down Expand Up @@ -282,8 +301,8 @@ public void testTypeChecked11() {
runNegativeTest(sources, "");
}

@Test // GROOVY-9460
public void testTypeChecked12() {
@Test
public void testTypeChecked9460() {
//@formatter:off
String[] sources = {
"G.groovy",
Expand All @@ -305,8 +324,8 @@ public void testTypeChecked12() {
runNegativeTest(sources, "");
}

@Test // GROOVY-9570
public void testTypeChecked13() {
@Test
public void testTypeChecked9570() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand All @@ -332,8 +351,8 @@ public void testTypeChecked13() {
runNegativeTest(sources, "");
}

@Test // GROOVY-9735
public void testTypeChecked14() {
@Test
public void testTypeChecked9735() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down Expand Up @@ -362,8 +381,8 @@ public void testTypeChecked14() {
runNegativeTest(sources, "");
}

@Test // GROOVY-9735
public void testTypeChecked15() {
@Test
public void testTypeChecked9735a() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down Expand Up @@ -392,8 +411,8 @@ public void testTypeChecked15() {
runNegativeTest(sources, "");
}

@Test // GROOVY-9751
public void testTypeChecked16() {
@Test
public void testTypeChecked9751() {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,7 @@ protected ClassNode[] getArgumentTypes(ArgumentListExpression args) {
return ret;
}

/* GRECLIPSE edit -- GROOVY-9769
private ClassNode getInferredTypeFromTempInfo(Expression exp, ClassNode result) {
Map<Object, List<ClassNode>> info = typeCheckingContext.temporaryIfBranchTypeInformation.empty() ? null : typeCheckingContext.temporaryIfBranchTypeInformation.peek();
if (exp instanceof VariableExpression && info != null) {
Expand Down Expand Up @@ -2502,6 +2503,31 @@ private ClassNode getInferredTypeFromTempInfo(Expression exp, ClassNode result)
}
return result;
}
*/
private ClassNode getInferredTypeFromTempInfo(final Expression expression, final ClassNode expressionType) {
Map<Object, List<ClassNode>> info = (!typeCheckingContext.temporaryIfBranchTypeInformation.empty() ? typeCheckingContext.temporaryIfBranchTypeInformation.peek() : null);
if (expression instanceof VariableExpression && info != null) {
List<ClassNode> tempTypes = getTemporaryTypesForExpression(expression);
if (tempTypes != null && !tempTypes.isEmpty()) {
List<ClassNode> types = new ArrayList<ClassNode>(tempTypes.size() + 1);
if (expressionType != null && !expressionType.equals(ClassHelper.OBJECT_TYPE) // GROOVY-7333
/*&& tempTypes.stream().noneMatch(t -> implementsInterfaceOrIsSubclassOf(t, expressionType))*/) {
types.add(expressionType);
}
types.addAll(tempTypes);

if (types.isEmpty()) {
return ClassHelper.OBJECT_TYPE;
} else if (types.size() == 1) {
return types.get(0);
} else {
return new UnionTypeClassNode(types.toArray(ClassNode.EMPTY_ARRAY));
}
}
}
return expressionType;
}
// GRECLIPSE end

@Override
public void visitClosureExpression(final ClosureExpression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2292,10 +2292,19 @@ private ClassNode getInferredTypeFromTempInfo(final Expression exp, ClassNode re
List<ClassNode> classNodes = getTemporaryTypesForExpression(exp);
if (classNodes != null && !classNodes.isEmpty()) {
List<ClassNode> types = new ArrayList<>(classNodes.size() + 1);
/* GRECLIPSE edit -- GROOVY-9769
if (result != null && !classNodes.contains(result)) types.add(result);
types.addAll(classNodes);
// GROOVY-7333: filter out Object
types.removeIf(OBJECT_TYPE::equals);
*/
ClassNode expressionType = result;
if (expressionType != null && !expressionType.equals(ClassHelper.OBJECT_TYPE) // GROOVY-7333
&& classNodes.stream().noneMatch(t -> implementsInterfaceOrIsSubclassOf(t, expressionType))) {
types.add(expressionType);
}
types.addAll(classNodes);
// GRECLIPSE end

if (types.isEmpty()) {
result = OBJECT_TYPE.getPlainNodeReference();
Expand Down

0 comments on commit b141d4e

Please sign in to comment.