Skip to content

Commit

Permalink
GROOVY-6954
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 22, 2021
1 parent 2b4f3ca commit c66e026
Show file tree
Hide file tree
Showing 6 changed files with 1,971 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,30 @@ public void testCompileStatic7363() {
runConformTest(sources, "42");
}

@Test
public void testCompileStatic7526() {
//@formatter:off
String[] sources = {
"Main.groovy",
"boolean check(String s) { true }\n" +
"@groovy.transform.CompileStatic\n" +
"void test(Pogo pogo) {\n" +
" if (check(pogo?.field)) {\n" + // VerifyError: Bad type on operand stack
" print 'works'\n" +
" }\n" +
"}\n" +
"test(new Pogo())\n",

"Pogo.groovy",
"class Pogo {\n" +
" public String field\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "works");
}

@Test
public void testCompileStatic7549() {
//@formatter:off
Expand Down Expand Up @@ -5136,42 +5160,24 @@ public void testCompileStatic9555() {

@Test
public void testCompileStatic9558() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"void test() {\n" +
" def config = new org.codehaus.groovy.control.CompilerConfiguration()\n" +
" config.tap {\n" +
" optimizationOptions['indy'] = true\n" + // Cannot cast object '...' with class 'CompilerConfiguration' to class 'Map'
" optimizationOptions.indy = true\n" +
" }\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources, "");
}

@Test
public void testCompileStatic9558a() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"void test() {\n" +
" def config = new org.codehaus.groovy.control.CompilerConfiguration()\n" +
" config.with {\n" +
" optimizationOptions['indy'] = true\n" + // Cannot cast object '...' with class 'CompilerConfiguration' to class 'Map'
" optimizationOptions.indy = true\n" +
" }\n" +
"}\n" +
"test()\n",
};
//@formatter:on
for (String dgm : new String[] {"tap", "with"}) {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"void test() {\n" +
" def config = new org.codehaus.groovy.control.CompilerConfiguration()\n" +
" config." + dgm + " {\n" +
" optimizationOptions['indy'] = true\n" + // Cannot cast object '...' with class 'CompilerConfiguration' to class 'Map'
" optimizationOptions.indy = true\n" +
" }\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources, "");
runConformTest(sources);
}
}

@Test
Expand Down Expand Up @@ -5272,7 +5278,7 @@ public void testCompileStatic9603() {
};
//@formatter:on

runConformTest(sources, "");
runConformTest(sources);
}

@Test
Expand Down Expand Up @@ -6348,7 +6354,7 @@ public void testCompileStatic10089() {

@Test
public void testCompileStatic10197() {
for (String override : new String[]{"int getBaz() {1}", "final int baz = 1"}) {
for (String override : new String[] {"int getBaz() {1}", "final int baz = 1"}) {
//@formatter:off
String[] sources = {
"Main.groovy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ private void writeNumberNumberCall(final Expression receiver, final String messa

@Override
public void fallbackAttributeOrPropertySite(PropertyExpression expression, Expression objectExpression, String name, MethodCallerMultiAdapter adapter) {
/* GRECLIPSE edit -- GROOVY-7304, GROOVY-9892
/* GRECLIPSE edit -- GROOVY-6954, GROOVY-7304, GROOVY-9892
if (name!=null &&
(adapter == AsmClassGenerator.setField || adapter == AsmClassGenerator.setGroovyObjectField)
) {
Expand All @@ -833,22 +833,27 @@ public void fallbackAttributeOrPropertySite(PropertyExpression expression, Expre
}
}
*/
if (name != null && controller.getCompileStack().isLHS()) {
ClassNode classNode = controller.getClassNode();
ClassNode receiverType = getPropertyOwnerType(objectExpression);
CompileStack compileStack = controller.getCompileStack();
OperandStack operandStack = controller.getOperandStack();

if (name != null && compileStack.isLHS()) {
boolean[] isClassReceiver = new boolean[1];
ClassNode receiverType = getPropertyOwnerType(objectExpression, isClassReceiver);
if (adapter == AsmClassGenerator.setField || adapter == AsmClassGenerator.setGroovyObjectField) {
if (setField(expression, objectExpression, receiverType, name)) return;
} else if (isThisExpression(objectExpression)) {
}
if (isThisExpression(objectExpression)) {
ClassNode classNode = controller.getClassNode();
FieldNode fieldNode = receiverType.getField(name);
if (fieldNode != null && fieldNode.isPrivate() && !receiverType.equals(classNode)
&& StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode)) {
Map<String, MethodNode> mutators = receiverType.redirect().getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_MUTATORS);
if (mutators != null) {
MethodNode methodNode = mutators.get(name);
if (methodNode != null) {
ClassNode rhsType = controller.getOperandStack().getTopOperand();
int i = controller.getCompileStack().defineTemporaryVariable("$rhsValue", rhsType, true);
VariableSlotLoader rhsValue = new VariableSlotLoader(rhsType, i, controller.getOperandStack());
ClassNode rhsType = operandStack.getTopOperand();
int i = compileStack.defineTemporaryVariable("$rhs", rhsType, true);
VariableSlotLoader rhsValue = new VariableSlotLoader(rhsType, i, operandStack);

MethodCallExpression call = new MethodCallExpression(objectExpression, methodNode.getName(), new ArgumentListExpression(fieldNode.isStatic() ? new ConstantExpression(null) : objectExpression, rhsValue));
call.setImplicitThis(expression.isImplicitThis());
Expand All @@ -857,13 +862,45 @@ public void fallbackAttributeOrPropertySite(PropertyExpression expression, Expre
call.setMethodTarget(methodNode);
call.visit(controller.getAcg());

controller.getCompileStack().removeVar(i);
controller.getOperandStack().pop();
compileStack.removeVar(i);
operandStack.pop();
return;
}
}
}
}
if (isOrImplements(receiverType, MAP_TYPE) && !isClassReceiver[0]) {
MethodVisitor mv = controller.getMethodVisitor();

// store value in temporary variable
ClassNode rhsType = operandStack.getTopOperand();
int rhs = compileStack.defineTemporaryVariable("$rhs", rhsType, true);

// push receiver on stack
compileStack.pushLHS(false);
objectExpression.visit(controller.getAcg());
compileStack.popLHS();

// check if receiver null
Label skip = new Label();
if (expression.isSafe()) {
mv.visitInsn(DUP);
mv.visitJumpInsn(IFNULL, skip);
}

mv.visitLdcInsn(name);
BytecodeHelper.load(mv, rhsType, rhs);
if (isPrimitiveType(rhsType)) BytecodeHelper.doCastToWrappedType(mv, rhsType, getWrapper(rhsType));
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);

if (expression.isSafe()) {
mv.visitLabel(skip);
}
compileStack.removeVar(rhs);
operandStack.pop();
return;
}

}
// GRECLIPSE end
super.fallbackAttributeOrPropertySite(expression, objectExpression, name, adapter);
Expand Down
2 changes: 1 addition & 1 deletion base/org.codehaus.groovy30/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<file-match-pattern match-pattern="groovy/classgen/asm/StatementWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticInvocationWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypes(Closure|MethodReferenceExpression)Writer.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypes(CallSite|Closure|MethodReferenceExpression)Writer.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/ErrorCollector.java" include-pattern="false" />
Expand Down
Loading

0 comments on commit c66e026

Please sign in to comment.