diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java index edd8389d3b0..33f3800c8e4 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java @@ -681,6 +681,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { assert function.getValueParameters().size() == constructor.getValueParameters().size() : "Number of parameters of copy function and constructor are different. Copy: " + function.getValueParameters().size() + ", constructor: " + constructor.getValueParameters().size(); + MutableClosure closure = context.closure; + if (closure != null && closure.getCaptureThis() != null) { + final Type type = typeMapper.mapType(enclosingClassDescriptor(bindingContext, descriptor)); + iv.load(0, classAsmType); + iv.getfield(JvmClassName.byType(classAsmType).getInternalName(), CAPTURED_THIS_FIELD, type.getDescriptor()); + } + int parameterIndex = 0; for (ValueParameterDescriptor parameterDescriptor : function.getValueParameters()) { iv.load(parameterIndex + 1, typeMapper.mapType(parameterDescriptor.getType())); diff --git a/compiler/testData/codegen/dataClasses/copy/copyInNestedDataClass.kt b/compiler/testData/codegen/dataClasses/copy/copyInNestedDataClass.kt new file mode 100644 index 00000000000..bb0bb4ec3ce --- /dev/null +++ b/compiler/testData/codegen/dataClasses/copy/copyInNestedDataClass.kt @@ -0,0 +1,15 @@ +class Bar(val name: String) + +class Baz { + class Foo() { + data class NestedFoo(val bar: Bar) + + fun foo(): String { + return NestedFoo(Bar("FAIL")).copy(bar = Bar("OK")).bar.name + } + } +} + +fun box(): String { + return Baz().Foo().foo() +} \ No newline at end of file diff --git a/compiler/testData/codegen/dataClasses/copy/copyInObjectNestedDataClass.kt b/compiler/testData/codegen/dataClasses/copy/copyInObjectNestedDataClass.kt new file mode 100644 index 00000000000..ccb3e90fbcb --- /dev/null +++ b/compiler/testData/codegen/dataClasses/copy/copyInObjectNestedDataClass.kt @@ -0,0 +1,15 @@ +class Bar(val name: String) + +abstract class Foo { + public abstract fun foo(): String +} + +fun box(): String { + return object: Foo() { + data class NestedFoo(val bar: Bar) + + override fun foo(): String { + return NestedFoo(Bar("Fail")).copy(bar = Bar("OK")).bar.name + } + }.foo() +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/DataClassCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/DataClassCodegenTestGenerated.java index 927a0a02be3..e15cf8f1cf8 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/DataClassCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/DataClassCodegenTestGenerated.java @@ -112,7 +112,16 @@ public class DataClassCodegenTestGenerated extends AbstractDataClassCodegenTest public void testWithGenericParameter() throws Exception { blackBoxFileByFullPath("compiler/testData/codegen/dataClasses/copy/withGenericParameter.kt"); } - + + @TestMetadata("copyInNestedDataClass.kt") + public void testCopyInNestedClasses() throws Exception { + blackBoxFileByFullPath("compiler/testData/codegen/dataClasses/copy/copyInNestedDataClass.kt"); + } + + @TestMetadata("copyInObjectNestedDataClass.kt") + public void testCopyInObjectNestedClasses() throws Exception { + blackBoxFileByFullPath("compiler/testData/codegen/dataClasses/copy/copyInObjectNestedDataClass.kt"); + } } @TestMetadata("compiler/testData/codegen/dataClasses/equals")