Search in sources :

Example 6 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class ControlStructureTypingVisitor method createLoopParameterDescriptor.

private VariableDescriptor createLoopParameterDescriptor(KtParameter loopParameter, KotlinType expectedParameterType, ExpressionTypingContext context) {
    components.modifiersChecker.withTrace(context.trace).checkParameterHasNoValOrVar(loopParameter, VAL_OR_VAR_ON_LOOP_PARAMETER);
    KtTypeReference typeReference = loopParameter.getTypeReference();
    VariableDescriptor variableDescriptor;
    if (typeReference != null) {
        variableDescriptor = components.descriptorResolver.resolveLocalVariableDescriptor(context.scope, loopParameter, context.trace);
        KotlinType actualParameterType = variableDescriptor.getType();
        if (expectedParameterType != null && !KotlinTypeChecker.DEFAULT.isSubtypeOf(expectedParameterType, actualParameterType)) {
            context.trace.report(TYPE_MISMATCH_IN_FOR_LOOP.on(typeReference, expectedParameterType, actualParameterType));
        }
    } else {
        if (expectedParameterType == null) {
            expectedParameterType = ErrorUtils.createErrorType("Error");
        }
        variableDescriptor = components.descriptorResolver.resolveLocalVariableDescriptor(loopParameter, expectedParameterType, context.trace, context.scope);
    }
    checkVariableShadowing(context.scope, context.trace, variableDescriptor);
    return variableDescriptor;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 7 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class ControlStructureTypingVisitor method visitReturnExpression.

@Override
public KotlinTypeInfo visitReturnExpression(@NotNull KtReturnExpression expression, ExpressionTypingContext context) {
    KtElement labelTargetElement = LabelResolver.INSTANCE.resolveControlLabel(expression, context);
    KtExpression returnedExpression = expression.getReturnedExpression();
    KotlinType expectedType = NO_EXPECTED_TYPE;
    KotlinType resultType = components.builtIns.getNothingType();
    KtDeclaration parentDeclaration = context.getContextParentOfType(expression, KtDeclaration.class);
    if (parentDeclaration instanceof KtParameter) {
        // In a default value for parameter
        context.trace.report(RETURN_NOT_ALLOWED.on(expression));
    }
    if (expression.getTargetLabel() == null) {
        while (parentDeclaration instanceof KtDestructuringDeclaration) {
            //TODO: It's hacking fix for KT-5100: Strange "Return is not allowed here" for multi-declaration initializer with elvis expression
            parentDeclaration = context.getContextParentOfType(parentDeclaration, KtDeclaration.class);
        }
        // Parent declaration can be null in code fragments or in some bad error expressions
        DeclarationDescriptor declarationDescriptor = context.trace.get(DECLARATION_TO_DESCRIPTOR, parentDeclaration);
        Pair<FunctionDescriptor, PsiElement> containingFunInfo = BindingContextUtils.getContainingFunctionSkipFunctionLiterals(declarationDescriptor, false);
        FunctionDescriptor containingFunctionDescriptor = containingFunInfo.getFirst();
        if (containingFunctionDescriptor != null) {
            if (!InlineUtil.checkNonLocalReturnUsage(containingFunctionDescriptor, expression, context) || isClassInitializer(containingFunInfo)) {
                // Unqualified, in a function literal
                context.trace.report(RETURN_NOT_ALLOWED.on(expression));
                resultType = ErrorUtils.createErrorType(RETURN_NOT_ALLOWED_MESSAGE);
            }
            expectedType = getFunctionExpectedReturnType(containingFunctionDescriptor, (KtElement) containingFunInfo.getSecond(), context);
        } else {
            // Outside a function
            context.trace.report(RETURN_NOT_ALLOWED.on(expression));
            resultType = ErrorUtils.createErrorType(RETURN_NOT_ALLOWED_MESSAGE);
        }
    } else if (labelTargetElement != null) {
        SimpleFunctionDescriptor functionDescriptor = context.trace.get(FUNCTION, labelTargetElement);
        if (functionDescriptor != null) {
            expectedType = getFunctionExpectedReturnType(functionDescriptor, labelTargetElement, context);
            if (!InlineUtil.checkNonLocalReturnUsage(functionDescriptor, expression, context)) {
                // Qualified, non-local
                context.trace.report(RETURN_NOT_ALLOWED.on(expression));
                resultType = ErrorUtils.createErrorType(RETURN_NOT_ALLOWED_MESSAGE);
            }
        } else {
            context.trace.report(NOT_A_RETURN_LABEL.on(expression, expression.getLabelName()));
        }
    }
    if (returnedExpression != null) {
        facade.getTypeInfo(returnedExpression, context.replaceExpectedType(expectedType).replaceScope(context.scope).replaceContextDependency(INDEPENDENT));
    } else {
        // for lambda with implicit return type Unit
        if (!noExpectedType(expectedType) && !KotlinBuiltIns.isUnit(expectedType) && !isDontCarePlaceholder(expectedType)) {
            context.trace.report(RETURN_TYPE_MISMATCH.on(expression, expectedType));
        }
    }
    return components.dataFlowAnalyzer.createCheckedTypeInfo(resultType, context, expression);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) PsiElement(com.intellij.psi.PsiElement)

Example 8 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class ControlStructureTypingVisitor method visitThrowExpression.

@Override
public KotlinTypeInfo visitThrowExpression(@NotNull KtThrowExpression expression, ExpressionTypingContext context) {
    KtExpression thrownExpression = expression.getThrownExpression();
    if (thrownExpression != null) {
        KotlinType throwableType = components.builtIns.getThrowable().getDefaultType();
        facade.getTypeInfo(thrownExpression, context.replaceExpectedType(throwableType).replaceScope(context.scope).replaceContextDependency(INDEPENDENT));
    }
    return components.dataFlowAnalyzer.createCheckedTypeInfo(components.builtIns.getNothingType(), context, expression);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 9 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class DataFlowAnalyzer method typeHasOverriddenEquals.

private static boolean typeHasOverriddenEquals(@NotNull KotlinType type, @NotNull KtElement lookupElement) {
    Collection<SimpleFunctionDescriptor> members = type.getMemberScope().getContributedFunctions(OperatorNameConventions.EQUALS, new KotlinLookupLocation(lookupElement));
    for (FunctionDescriptor member : members) {
        KotlinType returnType = member.getReturnType();
        if (returnType == null || !KotlinBuiltIns.isBoolean(returnType))
            continue;
        if (member.getValueParameters().size() != 1)
            continue;
        KotlinType parameterType = member.getValueParameters().iterator().next().getType();
        if (!KotlinBuiltIns.isNullableAny(parameterType))
            continue;
        FunctionDescriptor fromSuperClass = getOverriddenDescriptorFromClass(member);
        if (fromSuperClass == null)
            return false;
        ClassifierDescriptor superClassDescriptor = (ClassifierDescriptor) fromSuperClass.getContainingDeclaration();
        // We should have override fun in class other than Any (to prove unknown behaviour)
        return !KotlinBuiltIns.isAnyOrNullableAny(superClassDescriptor.getDefaultType());
    }
    return false;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KotlinLookupLocation(org.jetbrains.kotlin.incremental.KotlinLookupLocation)

Example 10 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class DataFlowAnalyzer method recordExpectedType.

public void recordExpectedType(@NotNull BindingTrace trace, @NotNull KtExpression expression, @NotNull KotlinType expectedType) {
    if (expectedType != NO_EXPECTED_TYPE) {
        KotlinType normalizeExpectedType = expectedType == UNIT_EXPECTED_TYPE ? builtIns.getUnitType() : expectedType;
        trace.record(BindingContext.EXPECTED_EXPRESSION_TYPE, expression, normalizeExpectedType);
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Aggregations

KotlinType (org.jetbrains.kotlin.types.KotlinType)110 NotNull (org.jetbrains.annotations.NotNull)34 IElementType (com.intellij.psi.tree.IElementType)16 Type (org.jetbrains.org.objectweb.asm.Type)16 Nullable (org.jetbrains.annotations.Nullable)10 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)7 PsiElement (com.intellij.psi.PsiElement)6 Name (org.jetbrains.kotlin.name.Name)6 ArrayList (java.util.ArrayList)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)4 Map (java.util.Map)3 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)3 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)3 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)3 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)3 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)3 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)3 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)2 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)2 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)2