[FE 1.0] Implement deprecation warning for private constructors of sealed classes

^KT-44866
^KT-49729 Fixed
This commit is contained in:
Dmitriy Novozhilov
2021-11-16 17:21:47 +03:00
parent 93378b1a04
commit da02d25278
18 changed files with 277 additions and 4 deletions
@@ -810,6 +810,8 @@ public interface Errors {
DiagnosticFactory0<KtParenthesizedExpression> PARENTHESIZED_COMPANION_LHS_DEPRECATION = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<PsiElement> RESOLUTION_TO_PRIVATE_CONSTRUCTOR_OF_SEALED_CLASS = DiagnosticFactory0.create(WARNING);
// Type inference
DiagnosticFactory0<KtParameter> CANNOT_INFER_PARAMETER_TYPE = DiagnosticFactory0.create(ERROR);
@@ -450,6 +450,8 @@ public class DefaultErrorMessages {
MAP.put(PARENTHESIZED_COMPANION_LHS_DEPRECATION, "Access to companion object through parenthesized class name is deprecated. Please, add explicit Companion qualifier.");
MAP.put(RESOLUTION_TO_PRIVATE_CONSTRUCTOR_OF_SEALED_CLASS, "Resolution to private constructor of sealed class. In future it will be allowed to use this constructor only inside declaring sealed class");
MAP.put(LOCAL_EXTENSION_PROPERTY, "Local extension properties are not allowed");
MAP.put(LOCAL_VARIABLE_WITH_GETTER, "Local variables are not allowed to have getters");
MAP.put(LOCAL_VARIABLE_WITH_SETTER, "Local variables are not allowed to have setters");
@@ -63,6 +63,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
UnitConversionCallChecker, FunInterfaceConstructorReferenceChecker, NullableExtensionOperatorWithSafeCallChecker,
ReferencingToUnderscoreNamedParameterOfCatchBlockChecker, VarargWrongExecutionOrderChecker, SelfCallInNestedObjectConstructorChecker,
NewSchemeOfIntegerOperatorResolutionChecker, EnumEntryVsCompanionPriorityCallChecker, CompanionInParenthesesLHSCallChecker,
ResolutionToPrivateConstructorOfSealedClassChecker,
)
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2021 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.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.isSealed
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
object ResolutionToPrivateConstructorOfSealedClassChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
// If AllowSealedInheritorsInDifferentFilesOfSamePackage disabled then all sealed constructors are private by default
if (!context.languageVersionSettings.supportsFeature(LanguageFeature.AllowSealedInheritorsInDifferentFilesOfSamePackage)) return
if (context.languageVersionSettings.supportsFeature(LanguageFeature.UseConsistentRulesForPrivateConstructorsOfSealedClasses)) return
val descriptor = resolvedCall.resultingDescriptor as? ConstructorDescriptor ?: return
if (descriptor.visibility != DescriptorVisibilities.PRIVATE) return
if (!descriptor.constructedClass.isSealed()) return
val containingDescriptor = context.scope.ownerDescriptor
val receiver = resolvedCall.dispatchReceiver ?: DescriptorVisibilities.ALWAYS_SUITABLE_RECEIVER
if (DescriptorVisibilities.findInvisibleMember(receiver, descriptor, containingDescriptor, false) != null) {
context.trace.report(Errors.RESOLUTION_TO_PRIVATE_CONSTRUCTOR_OF_SEALED_CLASS.on(reportOn))
}
}
}