[FE] Prohibit inheritors of sealed classes which are declared in different package

#KT-13495
This commit is contained in:
Dmitriy Novozhilov
2020-11-16 16:00:38 +03:00
committed by TeamCityServer
parent e76acc8ee0
commit d605c7e491
8 changed files with 125 additions and 26 deletions
@@ -426,6 +426,7 @@ public interface Errors {
DiagnosticFactory0<KtCallExpression> SEALED_CLASS_CONSTRUCTOR_CALL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> SEALED_SUPERTYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> SEALED_SUPERTYPE_IN_LOCAL_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory2<KtTypeReference, FqName, FqName> SEALED_INHERITOR_IN_DIFFERENT_PACKAGE = DiagnosticFactory2.create(ERROR);
// Companion objects
@@ -633,6 +633,7 @@ public class DefaultErrorMessages {
MAP.put(DATA_CLASS_CANNOT_HAVE_CLASS_SUPERTYPES, "Data class inheritance from other classes is forbidden");
MAP.put(SEALED_SUPERTYPE, "This type is sealed, so it can be inherited by only its own nested classes or objects");
MAP.put(SEALED_SUPERTYPE_IN_LOCAL_CLASS, "Local class cannot extend a sealed class");
MAP.put(SEALED_INHERITOR_IN_DIFFERENT_PACKAGE, "Inheritor of sealed class or interface declared in package {1} but it must be in package {2} where base class is declared", TO_STRING, TO_STRING);
MAP.put(SINGLETON_IN_SUPERTYPE, "Cannot inherit from a singleton");
MAP.put(CLASS_CANNOT_BE_EXTENDED_DIRECTLY, "Class {0} cannot be extended directly", NAME);
@@ -40,7 +40,8 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
FunInterfaceDeclarationChecker(),
DeprecatedSinceKotlinAnnotationChecker,
ContractDescriptionBlockChecker,
PrivateInlineFunctionsReturningAnonymousObjectsChecker
PrivateInlineFunctionsReturningAnonymousObjectsChecker,
SealedInheritorInSamePackageChecker,
)
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.checkers
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.containingPackage
import org.jetbrains.kotlin.descriptors.isSealed
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.bindingContextUtil.getAbbreviatedTypeOrType
object SealedInheritorInSamePackageChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (!context.languageVersionSettings.supportsFeature(LanguageFeature.FreedomForSealedClasses)) return
if (descriptor !is ClassDescriptor || declaration !is KtClassOrObject) return
val classPackage = descriptor.containingPackage() ?: return // local class, SEALED_SUPERTYPE already reported
for (superTypeListEntry in declaration.superTypeListEntries) {
val typeReference = superTypeListEntry.typeReference ?: continue
val superType = typeReference.getAbbreviatedTypeOrType(context.trace.bindingContext)?.unwrap() ?: continue
val superClass = superType.constructor.declarationDescriptor ?: continue
if (!superClass.isSealed()) continue
val superClassPackage = superClass.containingPackage() ?: continue
if (classPackage != superClassPackage) {
context.trace.report(Errors.SEALED_INHERITOR_IN_DIFFERENT_PACKAGE.on(typeReference, classPackage, superClassPackage))
}
}
}
}