Implement warning when non-abstract classes containing abstract members invisible from that classes

#KT-27825 fixed
This commit is contained in:
Ilya Chernikov
2019-11-08 21:09:22 +01:00
parent eae3688c61
commit 808000e458
13 changed files with 370 additions and 2 deletions
@@ -23555,6 +23555,34 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok
}
}
@TestMetadata("compiler/testData/diagnostics/tests/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Visibility extends AbstractFirDiagnosticsSmokeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("abstractInvisibleMemberFromJava.kt")
public void testAbstractInvisibleMemberFromJava() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromJava.kt");
}
@TestMetadata("abstractInvisibleMemberFromKotlin.kt")
public void testAbstractInvisibleMemberFromKotlin() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromKotlin.kt");
}
@TestMetadata("abstractInvisibleMemberFromKotlinWarning.kt")
public void testAbstractInvisibleMemberFromKotlinWarning() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromKotlinWarning.kt");
}
public void testAllFilesPresentInVisibility() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.+)\\.kt$"), true);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/when")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -512,7 +512,10 @@ public interface Errors {
DiagnosticFactory2.create(ERROR, DECLARATION_NAME);
DiagnosticFactory2<KtClassOrObject, KtClassOrObject, CallableMemberDescriptor> MANY_INTERFACES_MEMBER_NOT_IMPLEMENTED =
DiagnosticFactory2.create(ERROR, DECLARATION_NAME);
DiagnosticFactory2<KtClassOrObject, ClassDescriptor, Collection<CallableMemberDescriptor>> INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER =
DiagnosticFactory2.create(ERROR, DECLARATION_NAME);
DiagnosticFactory2<KtClassOrObject, ClassDescriptor, Collection<CallableMemberDescriptor>> INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER_WARNING =
DiagnosticFactory2.create(WARNING, DECLARATION_NAME);
DiagnosticFactory1<KtDeclaration, Collection<KotlinType>> AMBIGUOUS_ANONYMOUS_TYPE_INFERRED =
DiagnosticFactory1.create(ERROR, DECLARATION_SIGNATURE);
@@ -805,6 +805,8 @@ public class DefaultErrorMessages {
RENDER_CLASS_OR_OBJECT, FQ_NAMES_IN_TYPES);
MAP.put(MANY_INTERFACES_MEMBER_NOT_IMPLEMENTED, "{0} must override {1} because it inherits multiple interface methods of it",
RENDER_CLASS_OR_OBJECT, FQ_NAMES_IN_TYPES);
MAP.put(INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER, "{0} inherits invisible abstract members: {1}", NAME, commaSeparated(FQ_NAMES_IN_TYPES));
MAP.put(INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER_WARNING, "{0} inherits invisible abstract members: {1}", NAME, commaSeparated(FQ_NAMES_IN_TYPES));
MAP.put(CONFLICTING_OVERLOADS, "Conflicting overloads: {0}", commaSeparated(FQ_NAMES_IN_TYPES));
@@ -85,6 +85,7 @@ class OverrideResolver(
fun multipleImplementationsMemberNotImplemented(descriptor: CallableMemberDescriptor)
fun conflictingInterfaceMemberNotImplemented(descriptor: CallableMemberDescriptor)
fun typeMismatchOnInheritance(descriptor1: CallableMemberDescriptor, descriptor2: CallableMemberDescriptor)
fun abstractInvisibleMember(descriptor: CallableMemberDescriptor)
}
private class CollectMissingImplementationsStrategy : CheckInheritedSignaturesReportStrategy {
@@ -112,6 +113,10 @@ class OverrideResolver(
// don't care
}
override fun abstractInvisibleMember(descriptor: CallableMemberDescriptor) {
// don't care
}
override fun abstractMemberWithMoreSpecificType(
abstractMember: CallableMemberDescriptor,
concreteMember: CallableMemberDescriptor
@@ -127,6 +132,7 @@ class OverrideResolver(
private val abstractNoImpl = linkedSetOf<CallableMemberDescriptor>()
private val abstractInBaseClassNoImpl = linkedSetOf<CallableMemberDescriptor>()
private val abstractInvisibleSuper = linkedSetOf<CallableMemberDescriptor>()
private val multipleImplementations = linkedSetOf<CallableMemberDescriptor>()
private val conflictingInterfaceMembers = linkedSetOf<CallableMemberDescriptor>()
private val conflictingReturnTypes = linkedSetOf<CallableMemberDescriptor>()
@@ -164,6 +170,10 @@ class OverrideResolver(
}
}
override fun abstractInvisibleMember(descriptor: CallableMemberDescriptor) {
abstractInvisibleSuper.add(descriptor)
}
override fun abstractMemberWithMoreSpecificType(
abstractMember: CallableMemberDescriptor,
concreteMember: CallableMemberDescriptor
@@ -232,6 +242,14 @@ class OverrideResolver(
trace.report(ABSTRACT_MEMBER_NOT_IMPLEMENTED.on(klass, klass, abstractNoImpl.first()))
}
if (abstractInvisibleSuper.isNotEmpty() && !canHaveAbstractMembers) {
if (languageVersionSettings.supportsFeature(LanguageFeature.ProhibitInvisibleAbstractMethodsInSuperclasses)) {
trace.report(INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER.on(klass, classDescriptor, abstractInvisibleSuper))
} else {
trace.report(INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER_WARNING.on(klass, classDescriptor, abstractInvisibleSuper))
}
}
conflictingInterfaceMembers.removeAll(conflictingReturnTypes)
multipleImplementations.removeAll(conflictingReturnTypes)
if (!conflictingInterfaceMembers.isEmpty()) {
@@ -566,7 +584,6 @@ class OverrideResolver(
) {
val kind = descriptor.kind
if (kind != FAKE_OVERRIDE && kind != DELEGATION) return
if (descriptor.visibility === Visibilities.INVISIBLE_FAKE) return
val directOverridden = descriptor.overriddenDescriptors
assert(!directOverridden.isEmpty()) { kind.toString() + " " + descriptor.name.asString() + " must override something" }
@@ -584,6 +601,11 @@ class OverrideResolver(
val relevantDirectlyOverridden =
getRelevantDirectlyOverridden(overriddenDeclarationsByDirectParent, allFilteredOverriddenDeclarations)
if (descriptor.visibility === Visibilities.INVISIBLE_FAKE) {
checkInvisibleFakeOverride(descriptor, relevantDirectlyOverridden, reportingStrategy)
return
}
checkInheritedDescriptorsGroup(descriptor, relevantDirectlyOverridden, reportingStrategy)
if (kind == DELEGATION && overrideReportStrategyForDelegates != null) {
@@ -621,6 +643,23 @@ class OverrideResolver(
}
}
private fun checkInvisibleFakeOverride(
descriptor: CallableMemberDescriptor,
overriddenDescriptors: Collection<CallableMemberDescriptor>,
reportingStrategy: CheckInheritedSignaturesReportStrategy
) {
// the checks below are only relevant for non-abstract classes or objects
if ((descriptor.containingDeclaration as? ClassDescriptor)?.modality === Modality.ABSTRACT) return
val abstractOverrides = overriddenDescriptors.filter { it.modality === Modality.ABSTRACT }
if (abstractOverrides.size != overriddenDescriptors.size) return // has non-abstract override
for (override in abstractOverrides) {
reportingStrategy.abstractInvisibleMember(override)
}
}
private fun checkMissingOverridesByJava8Restrictions(
relevantDirectlyOverridden: Set<CallableMemberDescriptor>,
reportingStrategy: CheckInheritedSignaturesReportStrategy
@@ -0,0 +1,21 @@
// !LANGUAGE: +ProhibitInvisibleAbstractMethodsInSuperclasses
// FILE: base/Base.java
package base;
public abstract class Base {
public void foo() {
packagePrivateFoo();
}
/* package-private */ abstract void packagePrivateFoo();
}
// FILE: Impl.kt
package impl
import base.*
<!INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER!>class Impl<!> : Base()
fun foo() {
Impl().foo()
}
@@ -0,0 +1,26 @@
package
package base {
public abstract class Base {
public constructor Base()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public/*package*/ abstract fun packagePrivateFoo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
package impl {
public fun foo(): kotlin.Unit
public final class Impl : base.Base {
public constructor Impl()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake abstract override /*1*/ /*fake_override*/ fun packagePrivateFoo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,47 @@
// !LANGUAGE: +ProhibitInvisibleAbstractMethodsInSuperclasses
// MODULE: base
// FILE: Base.kt
package base
abstract class Base {
fun foo(): String {
return internalFoo()
}
internal abstract fun internalFoo(): String
}
open class BaseWithOverride : Base() {
override fun internalFoo(): String = ""
}
// MODULE: intermediate(base)
// FILE: Intermediate.kt
package intermediate
import base.*
abstract class Intermediate : Base()
// MODULE: impl(base, intermediate)
// FILE: Impl.kt
package impl
import base.*
import intermediate.*
<!INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER!>class ImplDirectFromBase<!> : Base()
<!INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER!>object ImplObjDirectFromBase<!> : Base()
class ImplDirectFromBaseWithOverride : BaseWithOverride()
class ImplDirectFromBaseWithOverrid : Base() {
<!CANNOT_OVERRIDE_INVISIBLE_MEMBER!>override<!> fun internalFoo(): String = ""
}
<!INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER!>class ImplViaIntermediate<!> : Intermediate()
fun foo() {
ImplDirectFromBase().foo()
ImplObjDirectFromBase.foo()
ImplDirectFromBaseWithOverride().foo()
ImplViaIntermediate().foo()
}
@@ -0,0 +1,92 @@
// -- Module: <base> --
package
package base {
public abstract class Base {
public constructor Base()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal abstract fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class BaseWithOverride : base.Base {
public constructor BaseWithOverride()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal open override /*1*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
// -- Module: <impl> --
package
package impl {
public fun foo(): kotlin.Unit
public final class ImplDirectFromBase : base.Base {
public constructor ImplDirectFromBase()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake abstract override /*1*/ /*fake_override*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class ImplDirectFromBaseWithOverrid : base.Base {
public constructor ImplDirectFromBaseWithOverrid()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class ImplDirectFromBaseWithOverride : base.BaseWithOverride {
public constructor ImplDirectFromBaseWithOverride()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake open override /*1*/ /*fake_override*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object ImplObjDirectFromBase : base.Base {
private constructor ImplObjDirectFromBase()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake abstract override /*1*/ /*fake_override*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class ImplViaIntermediate : intermediate.Intermediate {
public constructor ImplViaIntermediate()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake abstract override /*1*/ /*fake_override*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
// -- Module: <intermediate> --
package
package intermediate {
public abstract class Intermediate : base.Base {
public constructor Intermediate()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake abstract override /*1*/ /*fake_override*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,22 @@
// !LANGUAGE: -ProhibitInvisibleAbstractMethodsInSuperclasses
// MODULE: base
// FILE: Base.kt
package base
abstract class Base {
fun foo(): String {
return internalFoo()
}
internal abstract fun internalFoo(): String
}
// MODULE: impl(base)
// FILE: Impl.kt
package impl
import base.*
<!INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER_WARNING!>class Impl<!> : Base()
fun foo() {
Impl().foo()
}
@@ -0,0 +1,31 @@
// -- Module: <base> --
package
package base {
public abstract class Base {
public constructor Base()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal abstract fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
// -- Module: <impl> --
package
package impl {
public fun foo(): kotlin.Unit
public final class Impl : base.Base {
public constructor Impl()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
invisible_fake abstract override /*1*/ /*fake_override*/ fun internalFoo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -23637,6 +23637,34 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
}
}
@TestMetadata("compiler/testData/diagnostics/tests/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Visibility extends AbstractDiagnosticsTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("abstractInvisibleMemberFromJava.kt")
public void testAbstractInvisibleMemberFromJava() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromJava.kt");
}
@TestMetadata("abstractInvisibleMemberFromKotlin.kt")
public void testAbstractInvisibleMemberFromKotlin() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromKotlin.kt");
}
@TestMetadata("abstractInvisibleMemberFromKotlinWarning.kt")
public void testAbstractInvisibleMemberFromKotlinWarning() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromKotlinWarning.kt");
}
public void testAllFilesPresentInVisibility() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.*)\\.kts?$"), true);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/when")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -23557,6 +23557,34 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
}
}
@TestMetadata("compiler/testData/diagnostics/tests/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Visibility extends AbstractDiagnosticsUsingJavacTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("abstractInvisibleMemberFromJava.kt")
public void testAbstractInvisibleMemberFromJava() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromJava.kt");
}
@TestMetadata("abstractInvisibleMemberFromKotlin.kt")
public void testAbstractInvisibleMemberFromKotlin() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromKotlin.kt");
}
@TestMetadata("abstractInvisibleMemberFromKotlinWarning.kt")
public void testAbstractInvisibleMemberFromKotlinWarning() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/abstractInvisibleMemberFromKotlinWarning.kt");
}
public void testAllFilesPresentInVisibility() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.+)\\.kt$"), true);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/when")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -112,6 +112,7 @@ enum class LanguageFeature(
ProperComputationOrderOfTailrecDefaultParameters(KOTLIN_1_4),
AllowNullableArrayArgsInMain(KOTLIN_1_4),
TrailingCommas(KOTLIN_1_4),
ProhibitInvisibleAbstractMethodsInSuperclasses(KOTLIN_1_4, kind = BUG_FIX),
ProperVisibilityForCompanionObjectInstanceField(sinceVersion = null, kind = BUG_FIX),
// Temporarily disabled, see KT-27084/KT-22379