MUST_BE_INITIALIZED: take into account containingDeclaration's modality

^KT-58587 Fixed
Review: https://jetbrains.team/p/kt/reviews/10136

This commit is important in scope of KT-57553. It makes the migration
more smooth.

Other related tests:
- testUninitializedOrReassignedVariables
- testAugmentedAssignmentInInitializer
This commit is contained in:
Nikita Bobko
2023-05-10 16:00:54 +02:00
committed by Space Team
parent 4807a714ec
commit 9a65dcb664
11 changed files with 136 additions and 16 deletions
@@ -2948,6 +2948,18 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/backingField/LocalDeclarations.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOff.kt")
public void testMustBeInitializedEffectivelyFinalOff() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOff.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOn.kt")
public void testMustBeInitializedEffectivelyFinalOn() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOn.kt");
}
@Test
@TestMetadata("SetterWithExplicitType.kt")
public void testSetterWithExplicitType() throws Exception {
@@ -2948,6 +2948,18 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/backingField/LocalDeclarations.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOff.kt")
public void testMustBeInitializedEffectivelyFinalOff() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOff.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOn.kt")
public void testMustBeInitializedEffectivelyFinalOn() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOn.kt");
}
@Test
@TestMetadata("SetterWithExplicitType.kt")
public void testSetterWithExplicitType() throws Exception {
@@ -2948,6 +2948,18 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/backingField/LocalDeclarations.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOff.kt")
public void testMustBeInitializedEffectivelyFinalOff() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOff.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOn.kt")
public void testMustBeInitializedEffectivelyFinalOn() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOn.kt");
}
@Test
@TestMetadata("SetterWithExplicitType.kt")
public void testSetterWithExplicitType() throws Exception {
@@ -2954,6 +2954,18 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/backingField/LocalDeclarations.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOff.kt")
public void testMustBeInitializedEffectivelyFinalOff() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOff.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOn.kt")
public void testMustBeInitializedEffectivelyFinalOn() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOn.kt");
}
@Test
@TestMetadata("SetterWithExplicitType.kt")
public void testSetterWithExplicitType() throws Exception {
@@ -372,18 +372,29 @@ class IrBuiltInsOverFir(
override val ieee754equalsFunByOperandType: MutableMap<IrClassifierSymbol, IrSimpleFunctionSymbol>
get() = _ieee754equalsFunByOperandType
override lateinit var eqeqeqSymbol: IrSimpleFunctionSymbol private set
override lateinit var eqeqSymbol: IrSimpleFunctionSymbol private set
override lateinit var throwCceSymbol: IrSimpleFunctionSymbol private set
override lateinit var throwIseSymbol: IrSimpleFunctionSymbol private set
override lateinit var andandSymbol: IrSimpleFunctionSymbol private set
override lateinit var ororSymbol: IrSimpleFunctionSymbol private set
override lateinit var noWhenBranchMatchedExceptionSymbol: IrSimpleFunctionSymbol private set
override lateinit var illegalArgumentExceptionSymbol: IrSimpleFunctionSymbol private set
override lateinit var dataClassArrayMemberHashCodeSymbol: IrSimpleFunctionSymbol private set
override lateinit var dataClassArrayMemberToStringSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var eqeqeqSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var eqeqSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var throwCceSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var throwIseSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var andandSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var ororSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var noWhenBranchMatchedExceptionSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var illegalArgumentExceptionSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var dataClassArrayMemberHashCodeSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var dataClassArrayMemberToStringSymbol: IrSimpleFunctionSymbol private set
override lateinit var checkNotNullSymbol: IrSimpleFunctionSymbol private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var checkNotNullSymbol: IrSimpleFunctionSymbol private set
override val arrayOfNulls: IrSimpleFunctionSymbol by lazy {
findFunctions(kotlinPackage, Name.identifier("arrayOfNulls")).first {
it.owner.dispatchReceiverParameter == null && it.owner.valueParameters.size == 1 &&
@@ -394,10 +405,14 @@ class IrBuiltInsOverFir(
override val linkageErrorSymbol: IrSimpleFunctionSymbol
get() = TODO("Not yet implemented")
override lateinit var lessFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
override lateinit var lessOrEqualFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
override lateinit var greaterOrEqualFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
override lateinit var greaterFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var lessFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var lessOrEqualFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var greaterOrEqualFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
@Suppress("RedundantModalityModifier") // Explicit `final` keyword can be dropped after bootstrap update
final override var greaterFunByOperandType: Map<IrClassifierSymbol, IrSimpleFunctionSymbol> private set
init {
with(this.operatorsPackageFragment) {
@@ -628,7 +628,7 @@ class ControlFlowInformationProviderImpl private constructor(
val property = DescriptorToSourceUtils.descriptorToDeclaration(variableDescriptor) as? KtProperty
?: throw AssertionError("$variableDescriptor is not related to KtProperty")
val setter = property.setter
if (variableDescriptor.modality == Modality.FINAL && (setter == null || !setter.hasBody())) {
if (variableDescriptor.getEffectiveModality(languageVersionSettings) == Modality.FINAL && (setter == null || !setter.hasBody())) {
return false
}
@@ -1472,3 +1472,10 @@ class ControlFlowInformationProviderImpl private constructor(
return result
}
}
private fun PropertyDescriptor.getEffectiveModality(languageVersionSettings: LanguageVersionSettings): Modality =
when (languageVersionSettings.supportsFeature(LanguageFeature.TakeIntoAccountEffectivelyFinalInMustBeInitializedCheck) &&
modality == Modality.OPEN && (containingDeclaration as? ClassDescriptor)?.modality == Modality.FINAL) {
true -> Modality.FINAL
false -> modality
}
@@ -0,0 +1,12 @@
// !LANGUAGE:-TakeIntoAccountEffectivelyFinalInMustBeInitializedCheck
open class Base {
open var x: String = ""
}
class Foo : Base() {
override var x: String
init {
x = ""
}
}
@@ -0,0 +1,12 @@
// !LANGUAGE:-TakeIntoAccountEffectivelyFinalInMustBeInitializedCheck
open class Base {
open var x: String = ""
}
class Foo : Base() {
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>override var x: String<!>
init {
x = ""
}
}
@@ -0,0 +1,13 @@
// FIR_IDENTICAL
// !LANGUAGE:+TakeIntoAccountEffectivelyFinalInMustBeInitializedCheck
open class Base {
open var x: String = ""
}
class Foo : Base() {
override var x: String
init {
x = ""
}
}
@@ -2954,6 +2954,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/backingField/LocalDeclarations.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOff.kt")
public void testMustBeInitializedEffectivelyFinalOff() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOff.kt");
}
@Test
@TestMetadata("MustBeInitializedEffectivelyFinalOn.kt")
public void testMustBeInitializedEffectivelyFinalOn() throws Exception {
runTest("compiler/testData/diagnostics/tests/backingField/MustBeInitializedEffectivelyFinalOn.kt");
}
@Test
@TestMetadata("SetterWithExplicitType.kt")
public void testSetterWithExplicitType() throws Exception {
@@ -275,6 +275,7 @@ enum class LanguageFeature(
RefineTypeCheckingOnAssignmentsToJavaFields(KOTLIN_1_9, kind = BUG_FIX), // KT-46727
ValueClassesSecondaryConstructorWithBody(sinceVersion = KOTLIN_1_9, kind = UNSTABLE_FEATURE), // KT-55333
NativeJsProhibitLateinitIsInitializedIntrinsicWithoutPrivateAccess(KOTLIN_1_9, kind = BUG_FIX), // KT-27002
TakeIntoAccountEffectivelyFinalInMustBeInitializedCheck(KOTLIN_1_9, kind = OTHER), // KT-58587
// End of 1.* language features --------------------------------------------------