From 8852323a76eb6f528f361a9415f9df422489a1ee Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Mon, 22 Feb 2021 14:22:10 +0100 Subject: [PATCH] [PSI2IR] Do not generate property reference setter if inaccessible. Fixes KT-45064. --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../ReflectionReferencesGenerator.kt | 7 ++----- .../accessorForPropertyWithPrivateSetter.kt | 19 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++++ 10 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index c9046994e5f..532580a9a9d 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -3534,6 +3534,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @Test + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + @Test public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ReflectionReferencesGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ReflectionReferencesGenerator.kt index 21a067338ca..27990538b3a 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ReflectionReferencesGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ReflectionReferencesGenerator.kt @@ -16,10 +16,7 @@ package org.jetbrains.kotlin.psi2ir.generators -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.builtins.createFunctionType -import org.jetbrains.kotlin.builtins.isKFunctionType -import org.jetbrains.kotlin.builtins.isKSuspendFunctionType +import org.jetbrains.kotlin.builtins.* import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.ir.declarations.DescriptorMetadataSource import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin @@ -382,7 +379,7 @@ class ReflectionReferencesGenerator(statementGenerator: StatementGenerator) : St generateFunctionReference(startOffset, endOffset, type, symbol, callableDescriptor, typeArguments, origin) } is PropertyDescriptor -> { - val mutable = get(BindingContext.VARIABLE, ktElement)?.isVar ?: true + val mutable = ReflectionTypes.isNumberedKMutablePropertyType(type) generatePropertyReference(startOffset, endOffset, type, callableDescriptor, typeArguments, origin, mutable) } else -> diff --git a/compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt b/compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt new file mode 100644 index 00000000000..49a2bbc572e --- /dev/null +++ b/compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt @@ -0,0 +1,19 @@ +// IGNORE_BACKEND: WASM + +// WITH_RUNTIME +// FILE: b.kt +import a.A + +class B { + fun getValue() = sequenceOf(A()).map(A::value).first() +} + +fun box() = B().getValue() + +// FILE: a.kt +package a + +class A { + var value: String = "OK" + private set +} \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 76b9c83a3a0..88b81a7831c 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -3534,6 +3534,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @Test + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + @Test public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index a611f81af60..4707c9fa24f 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -3534,6 +3534,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @Test + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + @Test public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index fe021bbcca9..16d66a423a8 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -3094,6 +3094,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 3cc777b392d..3a93afa47d2 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -2324,6 +2324,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true); } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 9279ac96e45..14741b08f12 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -2324,6 +2324,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 7025699905a..47c49b6608a 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -2324,6 +2324,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/callableReference/property/accessViaSubclass.kt"); } + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index e8831dec8ca..50846c28fd8 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -1714,6 +1714,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath); } + @TestMetadata("accessorForPropertyWithPrivateSetter.kt") + public void testAccessorForPropertyWithPrivateSetter() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/property/accessorForPropertyWithPrivateSetter.kt"); + } + public void testAllFilesPresentInProperty() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/callableReference/property"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); }