Fix typechecker when initializer of destructuring declaration is unresolved or missing
This commit is contained in:
+9
-7
@@ -42,14 +42,14 @@ class DestructuringDeclarationResolver(
|
||||
fun defineLocalVariablesFromMultiDeclaration(
|
||||
writableScope: LexicalWritableScope,
|
||||
destructuringDeclaration: KtDestructuringDeclaration,
|
||||
receiver: ReceiverValue,
|
||||
reportErrorsOn: KtExpression,
|
||||
receiver: ReceiverValue?,
|
||||
initializer: KtExpression?,
|
||||
context: ExpressionTypingContext
|
||||
) {
|
||||
for ((componentIndex, entry) in destructuringDeclaration.entries.withIndex()) {
|
||||
val componentName = createComponentName(componentIndex + 1)
|
||||
|
||||
val componentType = resolveComponentFunctionAndGetType(componentName, context, entry, receiver, reportErrorsOn)
|
||||
val componentType = resolveComponentFunctionAndGetType(componentName, context, entry, receiver, initializer)
|
||||
val variableDescriptor = localVariableResolver.resolveLocalVariableDescriptorWithType(writableScope, entry, componentType, context.trace)
|
||||
|
||||
ExpressionTypingUtils.checkVariableShadowing(writableScope, context.trace, variableDescriptor)
|
||||
@@ -62,15 +62,17 @@ class DestructuringDeclarationResolver(
|
||||
componentName: Name,
|
||||
context: ExpressionTypingContext,
|
||||
entry: KtDestructuringDeclarationEntry,
|
||||
receiver: ReceiverValue,
|
||||
reportErrorsOn: KtExpression
|
||||
receiver: ReceiverValue?,
|
||||
initializer: KtExpression?
|
||||
): KotlinType {
|
||||
fun errorType() = ErrorUtils.createErrorType("$componentName() return type")
|
||||
|
||||
if (receiver == null || initializer == null) return errorType()
|
||||
|
||||
val expectedType = getExpectedTypeForComponent(context, entry)
|
||||
val results = fakeCallResolver.resolveFakeCall(
|
||||
context.replaceExpectedType(expectedType), receiver, componentName,
|
||||
entry, reportErrorsOn, FakeCallKind.COMPONENT
|
||||
entry, initializer, FakeCallKind.COMPONENT
|
||||
)
|
||||
|
||||
if (!results.isSuccess) {
|
||||
@@ -85,7 +87,7 @@ class DestructuringDeclarationResolver(
|
||||
val functionReturnType = functionDescriptor.returnType
|
||||
if (functionReturnType != null && !TypeUtils.noExpectedType(expectedType)
|
||||
&& !KotlinTypeChecker.DEFAULT.isSubtypeOf(functionReturnType, expectedType) ) {
|
||||
context.trace.report(Errors.COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH.on(reportErrorsOn, componentName, functionReturnType, expectedType))
|
||||
context.trace.report(Errors.COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH.on(initializer, componentName, functionReturnType, expectedType))
|
||||
}
|
||||
return functionReturnType ?: errorType()
|
||||
}
|
||||
|
||||
+12
-8
@@ -117,19 +117,23 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
|
||||
KtExpression initializer = multiDeclaration.getInitializer();
|
||||
if (initializer == null) {
|
||||
context.trace.report(INITIALIZER_REQUIRED_FOR_DESTRUCTURING_DECLARATION.on(multiDeclaration));
|
||||
return TypeInfoFactoryKt.noTypeInfo(context);
|
||||
}
|
||||
ExpressionReceiver expressionReceiver = ExpressionTypingUtils.getExpressionReceiver(
|
||||
facade, initializer, context.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT));
|
||||
KotlinTypeInfo typeInfo = facade.getTypeInfo(initializer, context);
|
||||
if (expressionReceiver == null) {
|
||||
return TypeInfoFactoryKt.noTypeInfo(context);
|
||||
}
|
||||
|
||||
ExpressionReceiver expressionReceiver = initializer != null ? ExpressionTypingUtils.getExpressionReceiver(
|
||||
facade, initializer, context.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT)) : null;
|
||||
|
||||
components.destructuringDeclarationResolver
|
||||
.defineLocalVariablesFromMultiDeclaration(scope, multiDeclaration, expressionReceiver, initializer, context);
|
||||
components.modifiersChecker.withTrace(context.trace).checkModifiersForDestructuringDeclaration(multiDeclaration);
|
||||
components.identifierChecker.checkDeclaration(multiDeclaration, context.trace);
|
||||
return typeInfo.replaceType(components.dataFlowAnalyzer.checkStatementType(multiDeclaration, context));
|
||||
|
||||
if (expressionReceiver == null) {
|
||||
return TypeInfoFactoryKt.noTypeInfo(context);
|
||||
}
|
||||
else {
|
||||
return facade.getTypeInfo(initializer, context)
|
||||
.replaceType(components.dataFlowAnalyzer.checkStatementType(multiDeclaration, context));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user