Resolve lateinit variables

Do not report UNINITIALIZED_VARIABLE on lateinit variables
Provide delegating constructors for descriptors for compatibility.
This commit is contained in:
Dmitry Petrov
2017-05-31 13:52:07 +03:00
parent f88cd5ed3d
commit 3bae430d49
19 changed files with 86 additions and 13 deletions
@@ -354,7 +354,9 @@ class ControlFlowInformationProvider private constructor(
}
}
is VariableDescriptor ->
report(Errors.UNINITIALIZED_VARIABLE.on(element, variableDescriptor), ctxt)
if (!variableDescriptor.isLateInit) {
report(Errors.UNINITIALIZED_VARIABLE.on(element, variableDescriptor), ctxt)
}
}
}
}
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.types.TypeSubstitutor;
public class LocalVariableDescriptor extends VariableDescriptorWithInitializerImpl implements VariableDescriptorWithAccessors {
private final boolean isDelegated;
private final boolean isLateInit;
private LocalVariableAccessorDescriptor.Getter getter;
private LocalVariableAccessorDescriptor.Setter setter;
@@ -36,10 +37,34 @@ public class LocalVariableDescriptor extends VariableDescriptorWithInitializerIm
@Nullable KotlinType type,
boolean mutable,
boolean isDelegated,
boolean isLateInit,
@NotNull SourceElement source
) {
super(containingDeclaration, annotations, name, type, mutable, source);
this.isDelegated = isDelegated;
this.isLateInit = isLateInit;
}
public LocalVariableDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull Name name,
@Nullable KotlinType type,
boolean mutable,
boolean isDelegated,
@NotNull SourceElement source
) {
this(containingDeclaration, annotations, name, type, mutable, isDelegated, false, source);
}
public LocalVariableDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull Name name,
@Nullable KotlinType type,
@NotNull SourceElement source
) {
this(containingDeclaration, annotations, name, type, false, false, false, source);
}
@Override
@@ -89,4 +114,9 @@ public class LocalVariableDescriptor extends VariableDescriptorWithInitializerIm
public boolean isDelegated() {
return isDelegated;
}
@Override
public boolean isLateInit() {
return isLateInit;
}
}
@@ -29,7 +29,8 @@ class SyntheticFieldDescriptor private constructor(
accessorDescriptor: PropertyAccessorDescriptor,
property: KtProperty
): LocalVariableDescriptor(accessorDescriptor, Annotations.EMPTY, SyntheticFieldDescriptor.NAME,
propertyDescriptor.type, propertyDescriptor.isVar, false, property.toSourceElement()) {
propertyDescriptor.type, propertyDescriptor.isVar, false, false,
property.toSourceElement()) {
constructor(accessorDescriptor: PropertyAccessorDescriptor,
property: KtProperty): this(accessorDescriptor.correspondingProperty, accessorDescriptor, property)
@@ -675,8 +675,6 @@ public class DescriptorResolver {
annotationResolver.resolveAnnotationsWithArguments(scope, parameter.getModifierList(), trace),
KtPsiUtil.safeName(parameter.getName()),
approximatedType,
false,
false,
KotlinSourceElementKt.toSourceElement(parameter)
);
trace.record(BindingContext.VALUE_PARAMETER, parameter, variableDescriptor);
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
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
@@ -195,6 +196,7 @@ class LocalVariableResolver(
trace: BindingTrace
): LocalVariableDescriptor {
val hasDelegate = variable is KtProperty && variable.hasDelegate()
val hasLateinit = variable.hasModifier(KtTokens.LATEINIT_KEYWORD)
val variableDescriptor = LocalVariableDescriptor(
scope.ownerDescriptor,
annotationResolver.resolveAnnotationsWithArguments(scope, variable.modifierList, trace),
@@ -207,6 +209,7 @@ class LocalVariableResolver(
type,
variable.isVar,
hasDelegate,
hasLateinit,
variable.toSourceElement()
)
trace.record(BindingContext.VARIABLE, variable, variableDescriptor)
@@ -275,6 +275,8 @@ class TypeResolver(
override fun isVar() = false
override fun isLateInit() = false
override fun getCompileTimeInitializer() = null
override fun <R : Any?, D : Any?> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R {
@@ -592,8 +592,8 @@ class DoubleColonExpressionResolver(
internal fun bindPropertyReference(expression: KtCallableReferenceExpression, referenceType: KotlinType, context: ResolutionContext<*>) {
val localVariable = LocalVariableDescriptor(
context.scope.ownerDescriptor, Annotations.EMPTY, Name.special("<anonymous>"), referenceType, /* mutable = */ false,
/* isDelegated = */ false, expression.toSourceElement()
context.scope.ownerDescriptor, Annotations.EMPTY, Name.special("<anonymous>"), referenceType,
expression.toSourceElement()
)
context.trace.record(BindingContext.VARIABLE, expression, localVariable)