Skip to content

Commit

Permalink
Fix for #1122: check for "... && ... && x instanceof T" flow typing
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 16, 2020
1 parent e57f3a4 commit 6b1cc50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Expand Up @@ -3426,8 +3426,25 @@ public void testInstanceOf13() {
assertType(contents, offset + 5, offset + 8, "java.lang.Number");
}

@Test
@Test // https://github.com/groovy/groovy-eclipse/issues/1122
public void testInstanceOf14() {
String contents =
"void test(flag, x) {\n" +
" if (flag && x instanceof java.util.regex.Matcher) {\n" +
" x.find()\n" +
" }\n" +
" x\n" +
"}\n";

int offset = contents.indexOf("x.find");
assertType(contents, offset, offset + 1, "java.util.regex.Matcher");

offset = contents.lastIndexOf("x");
assertType(contents, offset, offset + 1, "java.lang.Object");
}

@Test
public void testInstanceOf15() {
String contents =
"@groovy.transform.CompileStatic\n" +
"def test(value) {\n" +
Expand All @@ -3442,7 +3459,7 @@ public void testInstanceOf14() {
}

@Test // GROOVY-7971
public void testInstanceOf15() {
public void testInstanceOf16() {
String contents =
"@groovy.transform.CompileStatic\n" +
"def test(value) {\n" +
Expand Down
Expand Up @@ -2992,7 +2992,10 @@ private static Map<String, ClassNode[]> inferInstanceOfType(final Expression exp
switch (be.getOperation().getType()) {
case Types.LOGICAL_AND:
// check for "x instanceof T && ... && ..." flow typing
return inferInstanceOfType(be.getLeftExpression(), scope);
Map<String, ClassNode[]> types = inferInstanceOfType(be.getLeftExpression(), scope);
// or "... && ... && x instanceof T" flow typing
if (types.isEmpty()) types = inferInstanceOfType(be.getRightExpression(), scope);
return types;
case Types.KEYWORD_IN:
case 129/*Types.COMPARE_NOT_IN*/:
if (!(be.getRightExpression() instanceof ClassExpression)) {
Expand Down

0 comments on commit 6b1cc50

Please sign in to comment.