diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java index 6c4d49499a2..b02a2fa2f88 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java @@ -810,6 +810,20 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { constructorContext.lookupInContext(descriptor, null, state, true); } } + + @Override + public void visitThisExpression(JetThisExpression expression) { + final DeclarationDescriptor descriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expression.getInstanceReference()); + if (descriptor instanceof ClassDescriptor) { + // @todo for now all our classes are inner so no need to lookup this. change it when we have real inners + } + else { + assert descriptor instanceof CallableDescriptor; + if (context.getCallableDescriptorWithReceiver() != descriptor) { + context.lookupInContext(descriptor, null, state, false); + } + } + } }; for (JetDeclaration declaration : myClass.getDeclarations()) { diff --git a/compiler/testData/codegen/objects/receiverInConstructor.kt b/compiler/testData/codegen/objects/receiverInConstructor.kt new file mode 100644 index 00000000000..4ff54090b2e --- /dev/null +++ b/compiler/testData/codegen/objects/receiverInConstructor.kt @@ -0,0 +1,7 @@ +open class A(open val v: String) + +fun A.a(newv: String) = object: A("fail") { + override val v = this@a.v + newv +} + +fun box() = A("O").a("K").v \ No newline at end of file diff --git a/compiler/testData/codegen/objects/thisInConstructor.kt b/compiler/testData/codegen/objects/thisInConstructor.kt new file mode 100644 index 00000000000..3e804f1dbd7 --- /dev/null +++ b/compiler/testData/codegen/objects/thisInConstructor.kt @@ -0,0 +1,10 @@ +open class A(open val v: String) { +} + +open class B(open val v: String) { + fun a(newv: String) = object: A("fail") { + override val v = this@B.v + newv + } +} + +fun box() = B("O").a("K").v \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/ObjectGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/ObjectGenTest.java index 0d4c821b1e8..aadef5152c9 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ObjectGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ObjectGenTest.java @@ -83,4 +83,12 @@ public class ObjectGenTest extends CodegenTestCase { public void testKt1737() { blackBoxFile("regressions/kt1737.kt"); } + + public void testReceiverInConstructor() { + blackBoxFile("objects/receiverInConstructor.kt"); + } + + public void testThisInConstructor() { + blackBoxFile("objects/thisInConstructor.kt"); + } }