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
@@ -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
}