[FE 1.0] Implement deprecation warning for private constructors of sealed classes
^KT-44866 ^KT-49729 Fixed
This commit is contained in:
+12
@@ -26407,6 +26407,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/OperationWhen.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_error.kt")
|
||||
public void testPrivateSealedConstructors_error() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_error.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_warning.kt")
|
||||
public void testPrivateSealedConstructors_warning() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_warning.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateTypeInConstructor.kt")
|
||||
public void testPrivateTypeInConstructor() throws Exception {
|
||||
|
||||
+12
@@ -26407,6 +26407,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/OperationWhen.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_error.kt")
|
||||
public void testPrivateSealedConstructors_error() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_error.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_warning.kt")
|
||||
public void testPrivateSealedConstructors_warning() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_warning.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateTypeInConstructor.kt")
|
||||
public void testPrivateTypeInConstructor() throws Exception {
|
||||
|
||||
+12
@@ -26407,6 +26407,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/OperationWhen.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_error.kt")
|
||||
public void testPrivateSealedConstructors_error() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_error.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_warning.kt")
|
||||
public void testPrivateSealedConstructors_warning() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_warning.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateTypeInConstructor.kt")
|
||||
public void testPrivateTypeInConstructor() throws Exception {
|
||||
|
||||
@@ -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);
|
||||
|
||||
+2
@@ -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(
|
||||
|
||||
+30
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// LANGUAGE: +UseConsistentRulesForPrivateConstructorsOfSealedClasses
|
||||
// ISSUE: KT-44866, KT-49729
|
||||
|
||||
// FILE: base.kt
|
||||
sealed class SealedBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class SealedNested : SealedBase("nested")
|
||||
}
|
||||
class SealedOuter : SealedBase(<!ARGUMENT_TYPE_MISMATCH!>"outer"<!>)
|
||||
|
||||
abstract class RegularBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class RegularNested : RegularBase("nested")
|
||||
}
|
||||
class RegularOuter : RegularBase(<!ARGUMENT_TYPE_MISMATCH!>"outer"<!>)
|
||||
|
||||
// FILE: derived.kt
|
||||
|
||||
class SealedOuterInDifferentFile : SealedBase(<!ARGUMENT_TYPE_MISMATCH!>"other file"<!>)
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// LANGUAGE: +UseConsistentRulesForPrivateConstructorsOfSealedClasses
|
||||
// ISSUE: KT-44866, KT-49729
|
||||
|
||||
// FILE: base.kt
|
||||
sealed class SealedBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class SealedNested : SealedBase("nested")
|
||||
}
|
||||
class SealedOuter : <!INVISIBLE_MEMBER!>SealedBase<!>("outer")
|
||||
|
||||
abstract class RegularBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class RegularNested : RegularBase("nested")
|
||||
}
|
||||
class RegularOuter : <!INVISIBLE_MEMBER!>RegularBase<!>("outer")
|
||||
|
||||
// FILE: derived.kt
|
||||
|
||||
class SealedOuterInDifferentFile : <!INVISIBLE_MEMBER!>SealedBase<!>("other file")
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package
|
||||
|
||||
public abstract class RegularBase {
|
||||
public constructor RegularBase(/*0*/ x: kotlin.Int)
|
||||
private constructor RegularBase(/*0*/ y: kotlin.String)
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class RegularNested : RegularBase {
|
||||
public constructor RegularNested()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
public final class RegularOuter : RegularBase {
|
||||
public constructor RegularOuter()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public sealed class SealedBase {
|
||||
protected constructor SealedBase(/*0*/ x: kotlin.Int)
|
||||
private constructor SealedBase(/*0*/ y: kotlin.String)
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class SealedNested : SealedBase {
|
||||
public constructor SealedNested()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
public final class SealedOuter : SealedBase {
|
||||
public constructor SealedOuter()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final class SealedOuterInDifferentFile : SealedBase {
|
||||
public constructor SealedOuterInDifferentFile()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// LANGUAGE: -UseConsistentRulesForPrivateConstructorsOfSealedClasses
|
||||
// ISSUE: KT-44866, KT-49729
|
||||
|
||||
// FILE: base.kt
|
||||
sealed class SealedBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class SealedNested : SealedBase("nested")
|
||||
}
|
||||
class SealedOuter : SealedBase(<!ARGUMENT_TYPE_MISMATCH!>"outer"<!>)
|
||||
|
||||
abstract class RegularBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class RegularNested : RegularBase("nested")
|
||||
}
|
||||
class RegularOuter : RegularBase(<!ARGUMENT_TYPE_MISMATCH!>"outer"<!>)
|
||||
|
||||
// FILE: derived.kt
|
||||
|
||||
class SealedOuterInDifferentFile : SealedBase(<!ARGUMENT_TYPE_MISMATCH!>"other file"<!>)
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// LANGUAGE: -UseConsistentRulesForPrivateConstructorsOfSealedClasses
|
||||
// ISSUE: KT-44866, KT-49729
|
||||
|
||||
// FILE: base.kt
|
||||
sealed class SealedBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class SealedNested : SealedBase("nested")
|
||||
}
|
||||
class SealedOuter : <!RESOLUTION_TO_PRIVATE_CONSTRUCTOR_OF_SEALED_CLASS!>SealedBase<!>("outer")
|
||||
|
||||
abstract class RegularBase(x: Int) {
|
||||
private constructor(y: String) : this(y.length)
|
||||
|
||||
class RegularNested : RegularBase("nested")
|
||||
}
|
||||
class RegularOuter : <!INVISIBLE_MEMBER!>RegularBase<!>("outer")
|
||||
|
||||
// FILE: derived.kt
|
||||
|
||||
class SealedOuterInDifferentFile : <!INVISIBLE_MEMBER, RESOLUTION_TO_PRIVATE_CONSTRUCTOR_OF_SEALED_CLASS!>SealedBase<!>("other file")
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package
|
||||
|
||||
public abstract class RegularBase {
|
||||
public constructor RegularBase(/*0*/ x: kotlin.Int)
|
||||
private constructor RegularBase(/*0*/ y: kotlin.String)
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class RegularNested : RegularBase {
|
||||
public constructor RegularNested()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
public final class RegularOuter : RegularBase {
|
||||
public constructor RegularOuter()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public sealed class SealedBase {
|
||||
protected constructor SealedBase(/*0*/ x: kotlin.Int)
|
||||
private constructor SealedBase(/*0*/ y: kotlin.String)
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class SealedNested : SealedBase {
|
||||
public constructor SealedNested()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
public final class SealedOuter : SealedBase {
|
||||
public constructor SealedOuter()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final class SealedOuterInDifferentFile : SealedBase {
|
||||
public constructor SealedOuterInDifferentFile()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ sealed class Case3 private constructor(val x: Int) {
|
||||
class Inheritor2 : Case3("Hello")
|
||||
}
|
||||
|
||||
class Case3Inheritor3 : Case3(<!ARGUMENT_TYPE_MISMATCH!>20<!>) // should be an error in 1.6 (?)
|
||||
class Case3Inheritor3 : Case3(<!ARGUMENT_TYPE_MISMATCH!>20<!>) // should be an error in 1.8
|
||||
|
||||
sealed class Case4 {
|
||||
protected constructor(x: Int)
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ sealed class Case3 private constructor(val x: Int) {
|
||||
class Inheritor2 : Case3("Hello")
|
||||
}
|
||||
|
||||
class Case3Inheritor3 : Case3(20) // should be an error in 1.6 (?)
|
||||
class Case3Inheritor3 : Case3(20) // should be an error in 1.8
|
||||
|
||||
sealed class Case4 {
|
||||
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected<!> constructor(x: Int)
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ sealed class Case3 private constructor(val x: Int) {
|
||||
class Inheritor2 : Case3("Hello")
|
||||
}
|
||||
|
||||
class Case3Inheritor3 : Case3(<!ARGUMENT_TYPE_MISMATCH!>20<!>) // should be an error in 1.6 (?)
|
||||
class Case3Inheritor3 : Case3(<!ARGUMENT_TYPE_MISMATCH!>20<!>) // should be an error in 1.8
|
||||
|
||||
sealed class Case4 {
|
||||
protected constructor(x: Int)
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ sealed class Case3 private constructor(val x: Int) {
|
||||
class Inheritor2 : Case3("Hello")
|
||||
}
|
||||
|
||||
class Case3Inheritor3 : Case3(20) // should be an error in 1.6 (?)
|
||||
class Case3Inheritor3 : <!RESOLUTION_TO_PRIVATE_CONSTRUCTOR_OF_SEALED_CLASS!>Case3<!>(20) // should be an error in 1.8
|
||||
|
||||
sealed class Case4 {
|
||||
protected constructor(x: Int)
|
||||
|
||||
Generated
+12
@@ -26497,6 +26497,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/OperationWhen.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_error.kt")
|
||||
public void testPrivateSealedConstructors_error() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_error.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateSealedConstructors_warning.kt")
|
||||
public void testPrivateSealedConstructors_warning() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/sealed/privateSealedConstructors_warning.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("privateTypeInConstructor.kt")
|
||||
public void testPrivateTypeInConstructor() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user