Search in sources :

Example 1 with KotlinTypeChecker

use of org.jetbrains.kotlin.types.checker.KotlinTypeChecker in project kotlin by JetBrains.

the class OverridingUtil method isOverridableByWithoutExternalConditions.

@NotNull
public OverrideCompatibilityInfo isOverridableByWithoutExternalConditions(@NotNull CallableDescriptor superDescriptor, @NotNull CallableDescriptor subDescriptor, boolean checkReturnType) {
    OverrideCompatibilityInfo basicOverridability = getBasicOverridabilityProblem(superDescriptor, subDescriptor);
    if (basicOverridability != null)
        return basicOverridability;
    List<KotlinType> superValueParameters = compiledValueParameters(superDescriptor);
    List<KotlinType> subValueParameters = compiledValueParameters(subDescriptor);
    List<TypeParameterDescriptor> superTypeParameters = superDescriptor.getTypeParameters();
    List<TypeParameterDescriptor> subTypeParameters = subDescriptor.getTypeParameters();
    if (superTypeParameters.size() != subTypeParameters.size()) {
        for (int i = 0; i < superValueParameters.size(); ++i) {
            // TODO: compare erasure
            if (!KotlinTypeChecker.DEFAULT.equalTypes(superValueParameters.get(i), subValueParameters.get(i))) {
                return OverrideCompatibilityInfo.incompatible("Type parameter number mismatch");
            }
        }
        return OverrideCompatibilityInfo.conflict("Type parameter number mismatch");
    }
    KotlinTypeChecker typeChecker = createTypeChecker(superTypeParameters, subTypeParameters);
    for (int i = 0; i < superTypeParameters.size(); i++) {
        if (!areTypeParametersEquivalent(superTypeParameters.get(i), subTypeParameters.get(i), typeChecker)) {
            return OverrideCompatibilityInfo.incompatible("Type parameter bounds mismatch");
        }
    }
    for (int i = 0; i < superValueParameters.size(); i++) {
        if (!areTypesEquivalent(superValueParameters.get(i), subValueParameters.get(i), typeChecker)) {
            return OverrideCompatibilityInfo.incompatible("Value parameter type mismatch");
        }
    }
    if (superDescriptor instanceof FunctionDescriptor && subDescriptor instanceof FunctionDescriptor && ((FunctionDescriptor) superDescriptor).isSuspend() != ((FunctionDescriptor) subDescriptor).isSuspend()) {
        return OverrideCompatibilityInfo.conflict("Incompatible suspendability");
    }
    if (checkReturnType) {
        KotlinType superReturnType = superDescriptor.getReturnType();
        KotlinType subReturnType = subDescriptor.getReturnType();
        if (superReturnType != null && subReturnType != null) {
            boolean bothErrors = subReturnType.isError() && superReturnType.isError();
            if (!bothErrors && !typeChecker.isSubtypeOf(subReturnType, superReturnType)) {
                return OverrideCompatibilityInfo.conflict("Return type mismatch");
            }
        }
    }
    return OverrideCompatibilityInfo.success();
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KotlinTypeChecker(org.jetbrains.kotlin.types.checker.KotlinTypeChecker) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with KotlinTypeChecker

use of org.jetbrains.kotlin.types.checker.KotlinTypeChecker in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method checkForCastImpossibilityOrRedundancy.

private void checkForCastImpossibilityOrRedundancy(KtBinaryExpressionWithTypeRHS expression, KotlinType actualType, KotlinType targetType, ExpressionTypingContext context) {
    if (actualType == null || noExpectedType(targetType) || targetType.isError())
        return;
    if (DynamicTypesKt.isDynamic(targetType)) {
        KtTypeReference right = expression.getRight();
        assert right != null : "We know target is dynamic, but RHS is missing";
        context.trace.report(DYNAMIC_NOT_ALLOWED.on(right));
        return;
    }
    if (!CastDiagnosticsUtil.isCastPossible(actualType, targetType, components.platformToKotlinClassMap)) {
        context.trace.report(CAST_NEVER_SUCCEEDS.on(expression.getOperationReference()));
        return;
    }
    KotlinTypeChecker typeChecker = KotlinTypeChecker.DEFAULT;
    if (isExactTypeCast(actualType, targetType)) {
        // cast to itself: String as String
        context.trace.report(USELESS_CAST.on(expression));
        return;
    }
    Collection<KotlinType> possibleTypes = components.dataFlowAnalyzer.getAllPossibleTypes(expression.getLeft(), context.dataFlowInfo, actualType, context);
    boolean checkExactType = checkExactTypeForUselessCast(expression);
    for (KotlinType possibleType : possibleTypes) {
        boolean castIsUseless = checkExactType ? isExactTypeCast(possibleType, targetType) : isUpcast(possibleType, targetType, typeChecker);
        if (castIsUseless) {
            context.trace.report(USELESS_CAST.on(expression));
            return;
        }
    }
    if (CastDiagnosticsUtil.isCastErased(actualType, targetType, typeChecker)) {
        context.trace.report(UNCHECKED_CAST.on(expression, actualType, targetType));
    }
}
Also used : KotlinTypeChecker(org.jetbrains.kotlin.types.checker.KotlinTypeChecker)

Aggregations

KotlinTypeChecker (org.jetbrains.kotlin.types.checker.KotlinTypeChecker)2 NotNull (org.jetbrains.annotations.NotNull)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1