Skip to content

Commit e2743d4

Browse files
committed
GROOVY-6137, GROOVY-7473, GROOVY-10383
1 parent 2b3299d commit e2743d4

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,16 +807,41 @@ public void testCompileStatic6137() {
807807
String[] sources = {
808808
"Main.groovy",
809809
"@groovy.transform.CompileStatic\n" +
810-
"void test(a, b) {\n" +
811-
" print(a in b)\n" +
810+
"void test() {\n" +
811+
" print(null in null)\n" +
812+
" print(null in 'xx')\n" +
813+
" print('xx' in null)\n" +
814+
" print('xx' in 'xx')\n" +
815+
" print('xx' in ['xx'])\n" +
812816
"}\n" +
813-
"test(null,null)\n" +
814-
"test(null,new Object())\n" +
815-
"test(new Object(),null)\n",
817+
"test()\n",
816818
};
817819
//@formatter:on
818820

819-
runConformTest(sources, "truefalsefalse");
821+
runConformTest(sources, "truefalsefalsetruetrue");
822+
}
823+
824+
@Test
825+
public void testCompileStatic6137a() {
826+
assumeTrue(isParrotParser());
827+
828+
//@formatter:off
829+
String[] sources = {
830+
"Main.groovy",
831+
"@groovy.transform.CompileStatic\n" +
832+
"void test() {\n" +
833+
" print(null !in null)\n" +
834+
" print(null !in 'xx')\n" +
835+
" print('xx' !in null)\n" +
836+
" print('xx' !in 'xx')\n" +
837+
" print('xx' !in [''])\n" +
838+
" print('xx' !in ['xx'])\n" +
839+
"}\n" +
840+
"test()\n",
841+
};
842+
//@formatter:on
843+
844+
runConformTest(sources, "falsetruetruefalsetruefalse");
820845
}
821846

822847
@Test
@@ -1347,7 +1372,10 @@ public void testCompileStatic7473a() {
13471372
runConformTest(sources, "not xyz");
13481373

13491374
String result = disassemble(getOutputFile("Main.class"), 1);
1350-
int pos = result.indexOf("createList");
1375+
int pos = result.indexOf("ScriptBytecodeAdapter.isNotCase");
1376+
if (isAtLeastGroovy(40)) assertTrue(pos < 0); //GROOVY-10383
1377+
1378+
pos = result.indexOf("createList");
13511379
assumeTrue(pos > 0);
13521380

13531381
// the operand should be processed only once

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/sc/transformers/BinaryExpressionTransformer.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
6363
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
6464
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
65-
import static org.codehaus.groovy.ast.tools.GeneralUtils.isNullX;
6665
import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
6766
import static org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX;
6867
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
@@ -116,7 +115,9 @@ public Expression transformBinaryExpression(final BinaryExpression bin) {
116115
}
117116
break;
118117
case Types.KEYWORD_IN:
119-
return transformInOperation(bin);
118+
equal = true; //fallthrough
119+
case Types.COMPARE_NOT_IN:
120+
return transformInOperation(bin, equal);
120121
case Types.COMPARE_EQUAL:
121122
case Types.COMPARE_IDENTICAL:
122123
equal = true; //fallthrough
@@ -227,12 +228,12 @@ private static Expression transformAssignmentToSetterCall(
227228
pos);
228229
}
229230

230-
private Expression transformInOperation(final BinaryExpression bin) {
231+
private Expression transformInOperation(final BinaryExpression bin, final boolean in) {
231232
Expression leftExpression = bin.getLeftExpression();
232233
Expression rightExpression = bin.getRightExpression();
233234

234-
// transform "left in right" into "right.isCase(left)"
235-
MethodCallExpression call = callX(rightExpression, "isCase", leftExpression);
235+
// transform "left [!]in right" into "right.is[Not]Case(left)"
236+
MethodCallExpression call = callX(rightExpression, in ? "isCase" : "isNotCase", leftExpression);
236237
call.setImplicitThis(false); call.setSourcePosition(bin); call.copyNodeMetaData(bin);
237238
call.setMethodTarget(bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
238239
// GROOVY-7473: no null test for simple cases
@@ -246,7 +247,7 @@ private Expression transformInOperation(final BinaryExpression bin) {
246247

247248
// GROOVY-6137, GROOVY-7473: null safety and one-time evaluation
248249
call.setObjectExpression(rightExpression = transformRepeatedReference(rightExpression));
249-
Expression safe = ternaryX(isNullX( rightExpression ), isNullX( leftExpression ), call);
250+
Expression safe = ternaryX(new CompareToNullExpression(rightExpression,true), new CompareToNullExpression(leftExpression,in), call);
250251
safe.putNodeMetaData("classgen.callback", classgenCallback(call.getObjectExpression()));
251252
return staticCompilationTransformer.transform(safe);
252253
}

0 commit comments

Comments
 (0)