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;
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations