diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java index 10ec479ba0d..e254e8f4f6b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java @@ -652,6 +652,12 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes } } + @Override + protected boolean getShouldReportCyclicScopeWithCompanionWarning() { + return !c.getLanguageVersionSettings() + .supportsFeature(LanguageFeature.ProhibitVisibilityOfNestedClassifiersFromSupertypesOfCompanion); + } + @Override protected void reportScopesLoopError(@NotNull KotlinType type) { PsiElement reportOn = DescriptorToSourceUtils.getSourceFromDescriptor(type.getConstructor().getDeclarationDescriptor()); diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/everythingInOneScope_after.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/everythingInOneScope_after.kt index 0ec91659685..971fc966757 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/everythingInOneScope_after.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/everythingInOneScope_after.kt @@ -2,14 +2,14 @@ // see https://youtrack.jetbrains.com/issue/KT-21515 open class Container { - open class Base { + open class Base { open fun m() {} } // note that Base() supertype will be resolved in scope that was created on recursion - abstract class DerivedAbstract : Base() + abstract class DerivedAbstract : Base() - companion object : DerivedAbstract() { + companion object : DerivedAbstract() { override fun m() {} } } diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/noMembers_after.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/noMembers_after.kt index 114c803484e..5cf15f3455d 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/noMembers_after.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/noMembers_after.kt @@ -1,15 +1,15 @@ // !LANGUAGE: +ProhibitVisibilityOfNestedClassifiersFromSupertypesOfCompanion // see https://youtrack.jetbrains.com/issue/KT-21515 -abstract class DerivedAbstract : C.Base() { +abstract class DerivedAbstract : C.Base() { open class Data } public class C { - open class Base () + open class Base () class Foo : Data() - companion object : DerivedAbstract() + companion object : DerivedAbstract() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/onlyInterfaces_after.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/onlyInterfaces_after.kt index 9418f3babc2..350e095af2c 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/onlyInterfaces_after.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/onlyInterfaces_after.kt @@ -6,13 +6,13 @@ open class Container { // (this is case because we can't know if there are any loops without resolving, but resolving // itself provokes loops) - interface Base { + interface Base { open fun m() {} } - interface DerivedAbstract : Base + interface DerivedAbstract : Base - companion object : DerivedAbstract { + companion object : DerivedAbstract { override fun m() {} } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/typeIsLowEnough.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/typeIsLowEnough.kt index 8d655dc40ae..e7d57338d70 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/typeIsLowEnough.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/typeIsLowEnough.kt @@ -1,20 +1,20 @@ // see https://youtrack.jetbrains.com/issue/KT-21515 -abstract class DerivedAbstract : C.Base() { +abstract class DerivedAbstract : C.Base() { override abstract fun m() } public class C { class Data - open class Base () { + open class Base () { open fun m() {} } // Note that Data is resolved successfully here because we don't step on error-scope val data: Data = Data() - companion object : DerivedAbstract() { + companion object : DerivedAbstract() { override fun m() {} } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withIrrelevantInterface_after.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withIrrelevantInterface_after.kt index 8b19f086a00..b402ba85820 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withIrrelevantInterface_after.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withIrrelevantInterface_after.kt @@ -4,7 +4,7 @@ interface SomeIrrelevantInterface // note that C.Base() supertype will be resolved in normal scope -abstract class DerivedAbstract : C.Base() +abstract class DerivedAbstract : C.Base() class Data @@ -14,7 +14,7 @@ public class C { // Note that any supertype of Base will be resolved in error-scope, even if it absolutely irrelevant // to the types in cycle. - open class Base() : SomeIrrelevantInterface + open class Base() : SomeIrrelevantInterface - companion object : DerivedAbstract() + companion object : DerivedAbstract() } diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withMembers_after.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withMembers_after.kt index f73dccd3e5e..4826e4a9fe1 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withMembers_after.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withMembers_after.kt @@ -2,37 +2,37 @@ // see https://youtrack.jetbrains.com/issue/KT-21515 object WithFunctionInBase { - abstract class DerivedAbstract : C.Base() + abstract class DerivedAbstract : C.Base() class Data public class C { val data: Data = Data() - open class Base() { + open class Base() { fun foo(): Int = 42 } - companion object : DerivedAbstract() + companion object : DerivedAbstract() } } object WithPropertyInBase { // This case is very similar to previous one, but there are subtle differences from POV of implementation - abstract class DerivedAbstract : C.Base() + abstract class DerivedAbstract : C.Base() class Data public class C { - open class Base() { + open class Base() { val foo: Int = 42 } val data: Data = Data() - companion object : DerivedAbstract() + companion object : DerivedAbstract() } } @@ -41,18 +41,18 @@ object WithPropertyInBaseDifferentOrder { // Note how position of property in file affected order of resolve, and, consequently, its results and // diagnostics. - abstract class DerivedAbstract : C.Base() + abstract class DerivedAbstract : C.Base() class Data public class C { val data: Data = Data() - open class Base() { + open class Base() { val foo: Int = 42 } - companion object : DerivedAbstract() + companion object : DerivedAbstract() } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withoutTypeReference.kt b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withoutTypeReference.kt index 97dd7b8dec0..b624ea1f79a 100644 --- a/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withoutTypeReference.kt +++ b/compiler/testData/diagnostics/tests/cyclicHierarchy/withCompanion/withoutTypeReference.kt @@ -1,17 +1,17 @@ // see https://youtrack.jetbrains.com/issue/KT-21515 -abstract class DerivedAbstract : C.Base() +abstract class DerivedAbstract : C.Base() class Data open class C { - open class Base { + open class Base { open fun m() {} } val field = Data() - companion object : DerivedAbstract() { + companion object : DerivedAbstract() { override fun m() {} } } \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/types/AbstractTypeConstructor.kt index 963885c6e6c..e8d41c01853 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/AbstractTypeConstructor.kt @@ -97,11 +97,13 @@ abstract class AbstractTypeConstructor(storageManager: StorageManager) : TypeCon // We also check if there are a loop with additional edges going from owner of companion to // the companion itself. // Note that we use already disconnected types to not report two diagnostics on cyclic supertypes - supertypeLoopChecker.findLoopsInSupertypesAndDisconnect( - this, resultWithoutCycles, - { it.computeNeighbours(useCompanions = true) }, - { reportScopesLoopError(it) } - ) + if (shouldReportCyclicScopeWithCompanionWarning) { + supertypeLoopChecker.findLoopsInSupertypesAndDisconnect( + this, resultWithoutCycles, + { it.computeNeighbours(useCompanions = true) }, + { reportScopesLoopError(it) } + ) + } supertypes.supertypesWithoutCycles = (resultWithoutCycles as? List) ?: resultWithoutCycles.toList() }) @@ -118,6 +120,7 @@ abstract class AbstractTypeConstructor(storageManager: StorageManager) : TypeCon // TODO: overload in AbstractTypeParameterDescriptor? protected open fun reportScopesLoopError(type: KotlinType) {} + protected open val shouldReportCyclicScopeWithCompanionWarning: Boolean = false protected open fun getAdditionalNeighboursInSupertypeGraph(useCompanions: Boolean): Collection = emptyList() protected open fun defaultSupertypeIfEmpty(): KotlinType? = null