From 46a3f7420ce200760359ef2a771bd634aef4ef09 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 26 Jun 2018 12:40:33 +0300 Subject: [PATCH] When in debugger context, access private companion object directly --- .../kotlin/codegen/ExpressionCodegen.java | 9 +++------ .../kotlin/codegen/JvmCodegenUtil.java | 4 ++++ ...ivateCompanionObjectAccessedFromInitBlock.kt | 15 +++++++++++++++ ...nObjectAccessedFromInitBlockOfNestedClass.kt | 17 +++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 ++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 ++++++++++ .../codegen/LightAnalysisModeTestGenerated.java | 10 ++++++++++ .../semantics/IrJsCodegenBoxTestGenerated.java | 10 ++++++++++ .../semantics/JsCodegenBoxTestGenerated.java | 10 ++++++++++ 9 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt create mode 100644 compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index cf7222d7a82..aeb906ebfe9 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2651,12 +2651,9 @@ public class ExpressionCodegen extends KtVisitor impleme "Containing declaration of the companion object " + companionObjectDescriptor + ": expected a class, actual: " + hostClassDescriptor; - CodegenContext hostClassContext = context; - while (hostClassContext.getContextDescriptor() != hostClassDescriptor) { - hostClassContext = hostClassContext.getParentContext(); - assert hostClassContext != null : - "Host class context for " + hostClassDescriptor + " not found in context hierarchy for " + context; - } + CodegenContext hostClassContext = context.findParentContextWithDescriptor(hostClassDescriptor); + assert hostClassContext != null : + "Host class context for " + hostClassDescriptor + " not found in context hierarchy for " + context; hostClassContext.markCompanionObjectDescriptorWithAccessorRequired(companionObjectDescriptor); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java index 1524d434783..9f135d75c83 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java @@ -159,6 +159,10 @@ public class JvmCodegenUtil { return true; } + if (isDebuggerContext(contextBeforeInline)) { + return true; + } + CodegenContext context = contextBeforeInline.getFirstCrossInlineOrNonInlineContext(); if (context.isInlineMethodContext()) { // Inline method can be called from a nested class. diff --git a/compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt b/compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt new file mode 100644 index 00000000000..3e013c26c52 --- /dev/null +++ b/compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt @@ -0,0 +1,15 @@ +// !LANGUAGE: +ProperVisibilityForCompanionObjectInstanceField + +class Outer { + private companion object { + val result = "OK" + } + + val test: String + + init { + test = result + } +} + +fun box() = Outer().test \ No newline at end of file diff --git a/compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt b/compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt new file mode 100644 index 00000000000..203625bdf85 --- /dev/null +++ b/compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt @@ -0,0 +1,17 @@ +// !LANGUAGE: +ProperVisibilityForCompanionObjectInstanceField + +class Outer { + private companion object { + val result = "OK" + } + + class Nested { + val test: String + + init { + test = result + } + } +} + +fun box() = Outer.Nested().test \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 41422ebfb4d..3efdc892519 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -14039,6 +14039,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromAnonymousObjectInNestedClass.kt"); } + @TestMetadata("privateCompanionObjectAccessedFromInitBlock.kt") + public void testPrivateCompanionObjectAccessedFromInitBlock() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt"); + } + + @TestMetadata("privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt") + public void testPrivateCompanionObjectAccessedFromInitBlockOfNestedClass() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt"); + } + @TestMetadata("privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt") public void testPrivateCompanionObjectAccessedFromInlineLambdaInNestedClass() throws Exception { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 70a9cec1a94..5b4916d4408 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -14039,6 +14039,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromAnonymousObjectInNestedClass.kt"); } + @TestMetadata("privateCompanionObjectAccessedFromInitBlock.kt") + public void testPrivateCompanionObjectAccessedFromInitBlock() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt"); + } + + @TestMetadata("privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt") + public void testPrivateCompanionObjectAccessedFromInitBlockOfNestedClass() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt"); + } + @TestMetadata("privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt") public void testPrivateCompanionObjectAccessedFromInlineLambdaInNestedClass() throws Exception { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 8ccb1383d31..cc234803da4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -14039,6 +14039,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromAnonymousObjectInNestedClass.kt"); } + @TestMetadata("privateCompanionObjectAccessedFromInitBlock.kt") + public void testPrivateCompanionObjectAccessedFromInitBlock() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt"); + } + + @TestMetadata("privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt") + public void testPrivateCompanionObjectAccessedFromInitBlockOfNestedClass() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt"); + } + @TestMetadata("privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt") public void testPrivateCompanionObjectAccessedFromInlineLambdaInNestedClass() throws Exception { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index 5d2f1e12409..4945a7d9d80 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -13369,6 +13369,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromAnonymousObjectInNestedClass.kt"); } + @TestMetadata("privateCompanionObjectAccessedFromInitBlock.kt") + public void testPrivateCompanionObjectAccessedFromInitBlock() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt"); + } + + @TestMetadata("privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt") + public void testPrivateCompanionObjectAccessedFromInitBlockOfNestedClass() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt"); + } + @TestMetadata("privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt") public void testPrivateCompanionObjectAccessedFromInlineLambdaInNestedClass() throws Exception { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index a3d019e4fae..322ad031ab9 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -12241,6 +12241,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromAnonymousObjectInNestedClass.kt"); } + @TestMetadata("privateCompanionObjectAccessedFromInitBlock.kt") + public void testPrivateCompanionObjectAccessedFromInitBlock() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlock.kt"); + } + + @TestMetadata("privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt") + public void testPrivateCompanionObjectAccessedFromInitBlockOfNestedClass() throws Exception { + runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInitBlockOfNestedClass.kt"); + } + @TestMetadata("privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt") public void testPrivateCompanionObjectAccessedFromInlineLambdaInNestedClass() throws Exception { runTest("compiler/testData/codegen/box/objects/companionObjectAccess/privateCompanionObjectAccessedFromInlineLambdaInNestedClass.kt");