Fix nested classes codegen bug

When constructing a closure, codegen in some cases incorrectly determined if
it needed to store a reference from a nested class to the outer
This commit is contained in:
Alexander Udalov
2013-01-17 22:06:11 +04:00
parent 5c56900a09
commit c4a3963925
3 changed files with 26 additions and 8 deletions
@@ -32,8 +32,7 @@ import java.util.HashMap;
import java.util.Map;
import static org.jetbrains.jet.codegen.AsmUtil.CAPTURED_THIS_FIELD;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.CLASS_FOR_FUNCTION;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.FQN;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.*;
import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE;
public abstract class CodegenContext {
@@ -265,12 +264,12 @@ public abstract class CodegenContext {
}
protected void initOuterExpression(JetTypeMapper typeMapper, ClassDescriptor classDescriptor) {
final ClassDescriptor enclosingClass = getEnclosingClass();
outerExpression = enclosingClass != null
? StackValue
.field(typeMapper.mapType(enclosingClass), CodegenBinding.getJvmInternalName(typeMapper.getBindingTrace(), classDescriptor),
CAPTURED_THIS_FIELD,
false)
ClassDescriptor enclosingClass = getEnclosingClass();
outerExpression = enclosingClass != null && canHaveOuter(typeMapper.getBindingContext(), classDescriptor)
? StackValue.field(typeMapper.mapType(enclosingClass),
CodegenBinding.getJvmInternalName(typeMapper.getBindingTrace(), classDescriptor),
CAPTURED_THIS_FIELD,
false)
: null;
}
@@ -0,0 +1,14 @@
fun foo(f: (Int) -> Int) = f(0)
class Outer {
class Nested {
val y = foo { a -> a }
}
fun bar(): String {
val a = Nested()
return "OK"
}
}
fun box() = Outer().bar()
@@ -38,6 +38,11 @@ public class InnerNestedGenTestGenerated extends AbstractCodegenTest {
doTest("compiler/testData/codegen/innerNested/createNestedClass.kt");
}
@TestMetadata("createdNestedInOuterMember.kt")
public void testCreatedNestedInOuterMember() throws Exception {
doTest("compiler/testData/codegen/innerNested/createdNestedInOuterMember.kt");
}
@TestMetadata("dataLocalVariable.kt")
public void testDataLocalVariable() throws Exception {
doTest("compiler/testData/codegen/innerNested/dataLocalVariable.kt");