From 7203499b7aab6f37003b97e4734c14f88acc25c5 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 16 Mar 2020 12:50:42 +0300 Subject: [PATCH] FIR: Fix visibility check for protected within a companion --- .../kotlin/fir/resolve/calls/ResolverParts.kt | 6 ++++- .../expresssions/protectedVisibility.kt | 4 +-- .../expresssions/protectedVisibility.txt | 2 +- .../visibility/protectedInCompanion.kt | 9 +++++++ .../visibility/protectedInCompanion.txt | 26 +++++++++++++++++++ .../fir/FirDiagnosticsTestGenerated.java | 18 +++++++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 18 +++++++++++++ .../scopes/VisibilityInClassObject.fir.kt | 4 +-- .../CallCompanionProtectedNonStatic.fir.kt | 12 ++++----- 9 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.kt create mode 100644 compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.txt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolverParts.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolverParts.kt index ed4313ac7ea..c40545b3885 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolverParts.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolverParts.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.descriptors.Visibilities -import org.jetbrains.kotlin.fir.FirAnnotationContainer import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.expressions.FirExpression @@ -353,6 +352,11 @@ internal object CheckVisibility : CheckerStage() { if (classId in visited) return false visited += classId if (classId.isSame(ownerId)) return true + + fir.companionObject?.let { companion -> + if (companion.classId.isSame(ownerId)) return true + } + val superTypes = fir.superConeTypes for (superType in superTypes) { val superTypeSymbol = superType.lookupTag.toSymbol(session) as? FirRegularClassSymbol ?: continue diff --git a/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.kt b/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.kt index 2bcc96e899f..278a3280834 100644 --- a/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.kt +++ b/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.kt @@ -34,7 +34,7 @@ class Derived : Protected() { Nested().bar() // hidden fromCompanion() - protectedFromCompanion() + protectedFromCompanion() } private class NestedDerived : Nested() { @@ -60,4 +60,4 @@ class DerivedGeneric : Generic(1) { override fun foo(): Int { return super.foo() } -} \ No newline at end of file +} diff --git a/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.txt b/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.txt index 469fb88b6ee..266339ab398 100644 --- a/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.txt +++ b/compiler/fir/resolve/testData/resolve/expresssions/protectedVisibility.txt @@ -61,7 +61,7 @@ FILE: protectedVisibility.kt R|/Protected.Nested.Nested|().R|/Protected.Nested.foo|() R|/Protected.Nested.Nested|().#() this@R|/Protected.Companion|.R|/Protected.Companion.fromCompanion|() - #() + this@R|/Protected.Companion|.R|/Protected.Companion.protectedFromCompanion|() } private final class NestedDerived : R|Protected.Nested| { diff --git a/compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.kt b/compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.kt new file mode 100644 index 00000000000..77269b84e77 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.kt @@ -0,0 +1,9 @@ +abstract class A { + companion object { + protected val PROTECTED_CONST: String = "" + } +} + +class B : A() { + val y: String = PROTECTED_CONST +} diff --git a/compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.txt b/compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.txt new file mode 100644 index 00000000000..d0195fff566 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.txt @@ -0,0 +1,26 @@ +FILE: protectedInCompanion.kt + public abstract class A : R|kotlin/Any| { + public constructor(): R|A| { + super() + } + + public final companion object Companion : R|kotlin/Any| { + private constructor(): R|A.Companion| { + super() + } + + protected final val PROTECTED_CONST: R|kotlin/String| = String() + protected get(): R|kotlin/String| + + } + + } + public final class B : R|A| { + public constructor(): R|B| { + super() + } + + public final val y: R|kotlin/String| = this@R|/A.Companion|.R|/A.Companion.PROTECTED_CONST| + public get(): R|kotlin/String| + + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index 89b91d1fff0..65db06cfee0 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -1935,4 +1935,22 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { } } } + + @TestMetadata("compiler/fir/resolve/testData/resolve/visibility") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Visibility extends AbstractFirDiagnosticsTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInVisibility() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/resolve/testData/resolve/visibility"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @TestMetadata("protectedInCompanion.kt") + public void testProtectedInCompanion() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.kt"); + } + } } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index 7f9ce3b3f54..278508815e2 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -1935,4 +1935,22 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos } } } + + @TestMetadata("compiler/fir/resolve/testData/resolve/visibility") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Visibility extends AbstractFirDiagnosticsWithLightTreeTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInVisibility() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/resolve/testData/resolve/visibility"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @TestMetadata("protectedInCompanion.kt") + public void testProtectedInCompanion() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/visibility/protectedInCompanion.kt"); + } + } } diff --git a/compiler/testData/diagnostics/tests/scopes/VisibilityInClassObject.fir.kt b/compiler/testData/diagnostics/tests/scopes/VisibilityInClassObject.fir.kt index 24f2f70e685..9ec96d5cc79 100644 --- a/compiler/testData/diagnostics/tests/scopes/VisibilityInClassObject.fir.kt +++ b/compiler/testData/diagnostics/tests/scopes/VisibilityInClassObject.fir.kt @@ -28,6 +28,6 @@ class B: A() { devNull(A.internal_val) devNull(A.public_val) devNull(A.private_val) - devNull(A.protected_val) + devNull(A.protected_val) } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/CallCompanionProtectedNonStatic.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/CallCompanionProtectedNonStatic.fir.kt index 18847d59ada..949c96d4cdf 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/CallCompanionProtectedNonStatic.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/CallCompanionProtectedNonStatic.fir.kt @@ -46,8 +46,8 @@ open class Base { class Derived : Base() { fun test() { foo() // Ok - gav() // Ok - bar() + gav() // Ok + bar() baz() prop = 0 } @@ -55,8 +55,8 @@ class Derived : Base() { inner class DerivedInner { fun fromDerivedInner() { foo() // Ok - gav() // Ok - bar() + gav() // Ok + bar() baz() prop = 0 } @@ -64,8 +64,8 @@ class Derived : Base() { companion object { fun test2() { - gav() // Ok - bar() + gav() // Ok + bar() baz() prop = 0 }