@NotNull public static JetType createCorrespondingExtensionFunctionType( @NotNull JetType functionType, @NotNull JetType receiverType) { assert KotlinBuiltIns.getInstance().isFunctionType(functionType); List<TypeProjection> typeArguments = functionType.getArguments(); assert !typeArguments.isEmpty(); List<JetType> arguments = Lists.newArrayList(); // excluding the last type argument of the function type, which is the return type int index = 0; int lastIndex = typeArguments.size() - 1; for (TypeProjection typeArgument : typeArguments) { if (index < lastIndex) { arguments.add(typeArgument.getType()); } index++; } JetType returnType = typeArguments.get(lastIndex).getType(); return KotlinBuiltIns.getInstance() .getFunctionType(functionType.getAnnotations(), receiverType, arguments, returnType); }
/** * Determines what constraint (supertype, subtype or equal) should be generated for type parameter * {@code T} in a constraint like (in this example subtype one): <br> * {@code MyClass<in/out/- A> <: MyClass<in/out/- B>}, where {@code MyClass<in/out/- T>} is * declared. <br> * The parameters description is given according to the example above. * * @param typeParameterVariance declared variance of T * @param subjectTypeProjection {@code in/out/- A} * @param constrainingTypeProjection {@code in/out/- B} * @param upperConstraintKind kind of the constraint {@code MyClass<...A> <: MyClass<...B>} * (subtype in this example). * @return kind of constraint to be generated: {@code A <: B} (subtype), {@code A >: B} * (supertype) or {@code A = B} (equal). */ @NotNull private static ConstraintKind getTypeParameterConstraintKind( @NotNull Variance typeParameterVariance, @NotNull TypeProjection subjectTypeProjection, @NotNull TypeProjection constrainingTypeProjection, @NotNull ConstraintKind upperConstraintKind) { // If variance of type parameter is non-trivial, it should be taken into consideration to infer // result constraint type. // Otherwise when type parameter declared as INVARIANT, there might be non-trivial use-site // variance of a supertype. // // Example: Let class MyClass<T> is declared. // // If super type has 'out' projection: // MyClass<A> <: MyClass<out B>, // then constraint A <: B can be generated. // // If super type has 'in' projection: // MyClass<A> <: MyClass<in B>, // then constraint A >: B can be generated. // // Otherwise constraint A = B should be generated. Variance varianceForTypeParameter; if (typeParameterVariance != INVARIANT) { varianceForTypeParameter = typeParameterVariance; } else if (upperConstraintKind == SUPER_TYPE) { varianceForTypeParameter = constrainingTypeProjection.getProjectionKind(); } else if (upperConstraintKind == SUB_TYPE) { varianceForTypeParameter = subjectTypeProjection.getProjectionKind(); } else { varianceForTypeParameter = INVARIANT; } return getTypeParameterConstraintKind(varianceForTypeParameter, upperConstraintKind); }
private void addConstraint( @NotNull ConstraintKind constraintKind, @NotNull JetType subjectType, @Nullable JetType constrainingType, @NotNull ConstraintPosition constraintPosition) { if (constrainingType == TypeUtils.NO_EXPECTED_TYPE || constrainingType == DONT_CARE || constrainingType == CANT_INFER) { return; } if (constrainingType == null || (ErrorUtils.isErrorType(constrainingType) && constrainingType != PLACEHOLDER_FUNCTION_TYPE)) { hasErrorInConstrainingTypes = true; return; } assert subjectType != TypeUtils.NO_EXPECTED_TYPE : "Subject type shouldn't be NO_EXPECTED_TYPE (in position " + constraintPosition + " )"; if (ErrorUtils.isErrorType(subjectType)) return; DeclarationDescriptor subjectTypeDescriptor = subjectType.getConstructor().getDeclarationDescriptor(); KotlinBuiltIns kotlinBuiltIns = KotlinBuiltIns.getInstance(); if (constrainingType == PLACEHOLDER_FUNCTION_TYPE) { if (!kotlinBuiltIns.isFunctionOrExtensionFunctionType(subjectType)) { if (subjectTypeDescriptor instanceof TypeParameterDescriptor && typeParameterConstraints.get(subjectTypeDescriptor) != null) { // a constraint binds type parameter and any function type, so there is no new info and no // error return; } errorConstraintPositions.add(constraintPosition); } return; } // todo temporary hack // function literal without declaring receiver type { x -> ... } // can be considered as extension function if one is expected // (special type constructor for function/ extension function should be introduced like // PLACEHOLDER_FUNCTION_TYPE) if (constraintKind == SUB_TYPE && kotlinBuiltIns.isFunctionType(constrainingType) && kotlinBuiltIns.isExtensionFunctionType(subjectType)) { constrainingType = createCorrespondingExtensionFunctionType(constrainingType, DONT_CARE); } DeclarationDescriptor constrainingTypeDescriptor = constrainingType.getConstructor().getDeclarationDescriptor(); if (subjectTypeDescriptor instanceof TypeParameterDescriptor) { TypeParameterDescriptor typeParameter = (TypeParameterDescriptor) subjectTypeDescriptor; TypeConstraintsImpl typeConstraints = typeParameterConstraints.get(typeParameter); if (typeConstraints != null) { if (TypeUtils.dependsOnTypeParameterConstructors( constrainingType, Collections.singleton(DONT_CARE.getConstructor()))) { return; } if (subjectType.isNullable() && constrainingType.isNullable()) { constrainingType = TypeUtils.makeNotNullable(constrainingType); } typeConstraints.addBound(constraintKind, constrainingType); return; } } if (constrainingTypeDescriptor instanceof TypeParameterDescriptor) { assert typeParameterConstraints.get(constrainingTypeDescriptor) == null : "Constraining type contains type variable " + constrainingTypeDescriptor.getName(); } if (constraintKind == SUB_TYPE && kotlinBuiltIns.isNothingOrNullableNothing(constrainingType)) { // following constraints are always true: // 'Nothing' is a subtype of any type if (!constrainingType.isNullable()) return; // 'Nothing?' is a subtype of nullable type if (subjectType.isNullable()) return; } if (!(constrainingTypeDescriptor instanceof ClassDescriptor) || !(subjectTypeDescriptor instanceof ClassDescriptor)) { errorConstraintPositions.add(constraintPosition); return; } switch (constraintKind) { case SUB_TYPE: { if (kotlinBuiltIns.isNothingOrNullableNothing(constrainingType)) break; JetType correspondingSupertype = TypeCheckingProcedure.findCorrespondingSupertype(constrainingType, subjectType); if (correspondingSupertype != null) { constrainingType = correspondingSupertype; } break; } case SUPER_TYPE: { if (kotlinBuiltIns.isNothingOrNullableNothing(subjectType)) break; JetType correspondingSupertype = TypeCheckingProcedure.findCorrespondingSupertype(subjectType, constrainingType); if (correspondingSupertype != null) { subjectType = correspondingSupertype; } } case EQUAL: // nothing } if (constrainingType.getConstructor() != subjectType.getConstructor()) { errorConstraintPositions.add(constraintPosition); return; } TypeConstructor typeConstructor = subjectType.getConstructor(); List<TypeProjection> subjectArguments = subjectType.getArguments(); List<TypeProjection> constrainingArguments = constrainingType.getArguments(); List<TypeParameterDescriptor> parameters = typeConstructor.getParameters(); for (int i = 0; i < subjectArguments.size(); i++) { Variance typeParameterVariance = parameters.get(i).getVariance(); TypeProjection subjectArgument = subjectArguments.get(i); TypeProjection constrainingArgument = constrainingArguments.get(i); ConstraintKind typeParameterConstraintKind = getTypeParameterConstraintKind( typeParameterVariance, subjectArgument, constrainingArgument, constraintKind); addConstraint( typeParameterConstraintKind, subjectArgument.getType(), constrainingArgument.getType(), constraintPosition); } }