Check lateinit applicability for local variables
TODO probably should be refactored together with DeclarationsChecker
This commit is contained in:
@@ -576,27 +576,19 @@ class DeclarationsChecker(
|
||||
}
|
||||
|
||||
private fun checkPropertyLateInit(property: KtCallableDeclaration, propertyDescriptor: PropertyDescriptor) {
|
||||
val modifierList = property.modifierList ?: return
|
||||
val modifier = modifierList.getModifier(KtTokens.LATEINIT_KEYWORD) ?: return
|
||||
val modifier = property.modifierList?.getModifier(KtTokens.LATEINIT_KEYWORD) ?: return
|
||||
|
||||
if (!propertyDescriptor.isVar) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is allowed only on mutable properties"))
|
||||
}
|
||||
|
||||
var returnTypeIsNullable = true
|
||||
var returnTypeIsPrimitive = true
|
||||
val returnType = propertyDescriptor.type
|
||||
|
||||
val returnType = propertyDescriptor.returnType
|
||||
if (returnType != null) {
|
||||
returnTypeIsNullable = TypeUtils.isNullableType(returnType)
|
||||
returnTypeIsPrimitive = KotlinBuiltIns.isPrimitiveType(returnType)
|
||||
}
|
||||
|
||||
if (returnTypeIsNullable) {
|
||||
if (TypeUtils.isNullableType(returnType)) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is not allowed on nullable properties"))
|
||||
}
|
||||
|
||||
if (returnTypeIsPrimitive) {
|
||||
if (KotlinBuiltIns.isPrimitiveType(returnType)) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is not allowed on primitive type properties"))
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -24,9 +25,7 @@ import org.jetbrains.kotlin.descriptors.impl.*
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.*
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||
import org.jetbrains.kotlin.psi.KtVariableDeclaration
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.context.ContextDependency
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
@@ -35,6 +34,7 @@ import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.source.toSourceElement
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
import org.jetbrains.kotlin.types.expressions.*
|
||||
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.noTypeInfo
|
||||
|
||||
@@ -120,13 +120,42 @@ class LocalVariableResolver(
|
||||
typeInfo = noTypeInfo(context)
|
||||
}
|
||||
|
||||
ExpressionTypingUtils.checkVariableShadowing(context.scope, context.trace, propertyDescriptor)
|
||||
checkLocalVariableDeclaration(context, propertyDescriptor, property)
|
||||
|
||||
modifiersChecker.withTrace(context.trace).checkModifiersForLocalDeclaration(property, propertyDescriptor)
|
||||
identifierChecker.checkDeclaration(property, context.trace)
|
||||
return Pair(typeInfo.replaceType(dataFlowAnalyzer.checkStatementType(property, context)), propertyDescriptor)
|
||||
}
|
||||
|
||||
private fun checkLocalVariableDeclaration(context: ExpressionTypingContext, descriptor: VariableDescriptor, ktProperty: KtProperty) {
|
||||
ExpressionTypingUtils.checkVariableShadowing(context.scope, context.trace, descriptor)
|
||||
|
||||
modifiersChecker.withTrace(context.trace).checkModifiersForLocalDeclaration(ktProperty, descriptor)
|
||||
identifierChecker.checkDeclaration(ktProperty, context.trace)
|
||||
|
||||
checkLocalVariableLateinit(context.trace, descriptor, ktProperty)
|
||||
}
|
||||
|
||||
private fun checkLocalVariableLateinit(trace: BindingTrace, descriptor: VariableDescriptor, ktProperty: KtProperty) {
|
||||
val modifierList = ktProperty.modifierList ?: return
|
||||
val modifier = modifierList.getModifier(KtTokens.LATEINIT_KEYWORD) ?: return
|
||||
val returnType = descriptor.type
|
||||
|
||||
if (!descriptor.isVar) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is allowed only on mutable local variables"))
|
||||
}
|
||||
|
||||
if (TypeUtils.isNullableType(returnType)) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is not allowed on local variables of nullable types"))
|
||||
}
|
||||
|
||||
if (KotlinBuiltIns.isPrimitiveType(returnType)) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is not allowed on local variables of primitive types"))
|
||||
}
|
||||
|
||||
if (ktProperty.hasDelegateExpressionOrInitializer()) {
|
||||
trace.report(INAPPLICABLE_LATEINIT_MODIFIER.on(modifier, "is not allowed on variables with initializer or on local delegated properties"))
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveLocalVariableDescriptor(
|
||||
scope: LexicalScope,
|
||||
variable: KtVariableDeclaration,
|
||||
|
||||
Reference in New Issue
Block a user