Пример #1
0
  /**
   * Check if cast from supertype to subtype is erased. It is an error in "is" statement and warning
   * in "as".
   */
  public static boolean isCastErased(
      @NotNull KotlinType supertype,
      @NotNull KotlinType subtype,
      @NotNull KotlinTypeChecker typeChecker) {
    // cast between T and T? is always OK
    if (supertype.isMarkedNullable() || subtype.isMarkedNullable()) {
      return isCastErased(
          TypeUtils.makeNotNullable(supertype), TypeUtils.makeNotNullable(subtype), typeChecker);
    }

    // if it is a upcast, it's never erased
    if (typeChecker.isSubtypeOf(supertype, subtype)) return false;

    // downcasting to a non-reified type parameter is always erased
    if (TypeUtils.isNonReifiedTypeParemeter(subtype)) return true;

    // Check that we are actually casting to a generic type
    // NOTE: this does not account for 'as Array<List<T>>'
    if (allParametersReified(subtype)) return false;

    KotlinType staticallyKnownSubtype =
        findStaticallyKnownSubtype(supertype, subtype.getConstructor()).getResultingType();

    // If the substitution failed, it means that the result is an impossible type, e.g. something
    // like Out<in Foo>
    // In this case, we can't guarantee anything, so the cast is considered to be erased
    if (staticallyKnownSubtype == null) return true;

    // If the type we calculated is a subtype of the cast target, it's OK to use the cast target
    // instead.
    // If not, it's wrong to use it
    return !typeChecker.isSubtypeOf(staticallyKnownSubtype, subtype);
  }
Пример #2
0
  /**
   * Remember that we are trying to cast something of type {@code supertype} to {@code subtype}.
   *
   * <p>Since at runtime we can only check the class (type constructor), the rest of the subtype
   * should be known statically, from supertype. This method reconstructs all static information
   * that can be obtained from supertype.
   *
   * <p>Example 1: supertype = Collection<String> subtype = List<...> result = List<String>, all
   * arguments are inferred
   *
   * <p>Example 2: supertype = Any subtype = List<...> result = List<*>, some arguments were not
   * inferred, replaced with '*'
   */
  public static TypeReconstructionResult findStaticallyKnownSubtype(
      @NotNull KotlinType supertype, @NotNull TypeConstructor subtypeConstructor) {
    assert !supertype.isMarkedNullable() : "This method only makes sense for non-nullable types";

    // Assume we are casting an expression of type Collection<Foo> to List<Bar>
    // First, let's make List<T>, where T is a type variable
    ClassifierDescriptor descriptor = subtypeConstructor.getDeclarationDescriptor();
    assert descriptor != null : "Can't create default type for " + subtypeConstructor;
    KotlinType subtypeWithVariables = descriptor.getDefaultType();

    // Now, let's find a supertype of List<T> that is a Collection of something,
    // in this case it will be Collection<T>
    KotlinType supertypeWithVariables =
        TypeCheckingProcedure.findCorrespondingSupertype(subtypeWithVariables, supertype);

    final List<TypeParameterDescriptor> variables =
        subtypeWithVariables.getConstructor().getParameters();

    Map<TypeConstructor, TypeProjection> substitution;
    if (supertypeWithVariables != null) {
      // Now, let's try to unify Collection<T> and Collection<Foo> solution is a map from T to Foo
      TypeUnifier.UnificationResult solution =
          TypeUnifier.unify(
              new TypeProjectionImpl(supertype),
              new TypeProjectionImpl(supertypeWithVariables),
              new Predicate<TypeConstructor>() {
                @Override
                public boolean apply(TypeConstructor typeConstructor) {
                  ClassifierDescriptor descriptor = typeConstructor.getDeclarationDescriptor();
                  return descriptor instanceof TypeParameterDescriptor
                      && variables.contains(descriptor);
                }
              });
      substitution = Maps.newHashMap(solution.getSubstitution());
    } else {
      // If there's no corresponding supertype, no variables are determined
      // This may be OK, e.g. in case 'Any as List<*>'
      substitution = Maps.newHashMapWithExpectedSize(variables.size());
    }

    // If some of the parameters are not determined by unification, it means that these parameters
    // are lost,
    // let's put stars instead, so that we can only cast to something like List<*>, e.g. (a: Any) as
    // List<*>
    boolean allArgumentsInferred = true;
    for (TypeParameterDescriptor variable : variables) {
      TypeProjection value = substitution.get(variable.getTypeConstructor());
      if (value == null) {
        substitution.put(variable.getTypeConstructor(), TypeUtils.makeStarProjection(variable));
        allArgumentsInferred = false;
      }
    }

    // At this point we have values for all type parameters of List
    // Let's make a type by substituting them: List<T> -> List<Foo>
    KotlinType substituted =
        TypeSubstitutor.create(substitution).substitute(subtypeWithVariables, Variance.INVARIANT);

    return new TypeReconstructionResult(substituted, allArgumentsInferred);
  }
Пример #3
0
 @Nullable
 public static TypeParameterDescriptor getTypeParameterDescriptorOrNull(@NotNull KotlinType type) {
   if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) {
     return (TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor();
   }
   return null;
 }
Пример #4
0
 public static boolean dependsOnTypeConstructors(
     @NotNull KotlinType type, @NotNull Collection<TypeConstructor> typeParameterConstructors) {
   if (typeParameterConstructors.contains(type.getConstructor())) return true;
   for (TypeProjection typeProjection : type.getArguments()) {
     if (!typeProjection.isStarProjection()
         && dependsOnTypeConstructors(typeProjection.getType(), typeParameterConstructors)) {
       return true;
     }
   }
   return false;
 }
Пример #5
0
  public static boolean hasNullableSuperType(@NotNull KotlinType type) {
    if (type.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) {
      // A class/trait cannot have a nullable supertype
      return false;
    }

    for (KotlinType supertype : getImmediateSupertypes(type)) {
      if (supertype.isMarkedNullable()) return true;
      if (hasNullableSuperType(supertype)) return true;
    }

    return false;
  }
Пример #6
0
 private static boolean lowerThanBound(
     KotlinTypeChecker typeChecker,
     KotlinType argument,
     TypeParameterDescriptor parameterDescriptor) {
   for (KotlinType bound : parameterDescriptor.getUpperBounds()) {
     if (typeChecker.isSubtypeOf(argument, bound)) {
       if (!argument.getConstructor().equals(bound.getConstructor())) {
         return true;
       }
     }
   }
   return false;
 }
Пример #7
0
 @NotNull
 public KotlinType getArrayElementType(@NotNull KotlinType arrayType) {
   if (isArray(arrayType)) {
     if (arrayType.getArguments().size() != 1) {
       throw new IllegalStateException();
     }
     return arrayType.getArguments().get(0).getType();
   }
   KotlinType primitiveType =
       kotlinArrayTypeToPrimitiveKotlinType.get(TypeUtils.makeNotNullable(arrayType));
   if (primitiveType == null) {
     throw new IllegalStateException("not array: " + arrayType);
   }
   return primitiveType;
 }
Пример #8
0
 public static boolean contains(
     @Nullable KotlinType type, @NotNull Function1<KotlinType, Boolean> isSpecialType) {
   if (type == null) return false;
   if (isSpecialType.invoke(type)) return true;
   Flexibility flexibility = type.getCapability(Flexibility.class);
   if (flexibility != null
       && (contains(flexibility.getLowerBound(), isSpecialType)
           || contains(flexibility.getUpperBound(), isSpecialType))) {
     return true;
   }
   for (TypeProjection projection : type.getArguments()) {
     if (!projection.isStarProjection() && contains(projection.getType(), isSpecialType))
       return true;
   }
   return false;
 }
Пример #9
0
 @Nullable
 public static ClassDescriptor getClassDescriptor(@NotNull KotlinType type) {
   DeclarationDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor();
   if (declarationDescriptor instanceof ClassDescriptor) {
     return (ClassDescriptor) declarationDescriptor;
   }
   return null;
 }
Пример #10
0
  public static boolean isExtensionFunctionType(@NotNull KotlinType type) {
    if (isExactExtensionFunctionType(type)) return true;

    for (KotlinType superType : type.getConstructor().getSupertypes()) {
      if (isExtensionFunctionType(superType)) return true;
    }

    return false;
  }
Пример #11
0
 /**
  * Differs from `isNullableType` only by treating type parameters: acceptsNullable(T) <=> T has
  * nullable lower bound Semantics should be the same as `isSubtype(Nothing?, T)`
  *
  * @return true if `null` can be assigned to storage of this type
  */
 public static boolean acceptsNullable(@NotNull KotlinType type) {
   if (type.isMarkedNullable()) {
     return true;
   }
   if (FlexibleTypesKt.isFlexible(type)
       && acceptsNullable(FlexibleTypesKt.flexibility(type).getUpperBound())) {
     return true;
   }
   return false;
 }
Пример #12
0
 private static boolean isConstructedFromGivenClass(
     @NotNull KotlinType type, @NotNull FqNameUnsafe fqName) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return descriptor != null
       &&
       /* quick check to avoid creation of full FqName instance */ descriptor
           .getName()
           .equals(fqName.shortName())
       && fqName.equals(getFqName(descriptor));
 }
Пример #13
0
 public static boolean isSubtypeOfClass(
     @NotNull KotlinType type, @NotNull DeclarationDescriptor superClass) {
   if (isSameClass(type, superClass)) return true;
   for (KotlinType superType : type.getConstructor().getSupertypes()) {
     if (isSubtypeOfClass(superType, superClass)) {
       return true;
     }
   }
   return false;
 }
Пример #14
0
 @Nullable
 public static KotlinType getReceiverType(@NotNull KotlinType type) {
   assert isFunctionOrExtensionFunctionType(type) : type;
   if (isExtensionFunctionType(type)) {
     // TODO: this is incorrect when a class extends from an extension function and swaps type
     // arguments
     return type.getArguments().get(0).getType();
   }
   return null;
 }
Пример #15
0
  private static List<KotlinType> mapToPlatformIndependentTypes(
      @NotNull KotlinType type, @NotNull PlatformToKotlinClassMap platformToKotlinClassMap) {
    ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
    if (!(descriptor instanceof ClassDescriptor)) return Collections.singletonList(type);

    ClassDescriptor originalClass = (ClassDescriptor) descriptor;
    Collection<ClassDescriptor> kotlinClasses =
        platformToKotlinClassMap.mapPlatformClass(originalClass);
    if (kotlinClasses.isEmpty()) return Collections.singletonList(type);

    List<KotlinType> result = Lists.newArrayListWithCapacity(2);
    result.add(type);
    for (ClassDescriptor classDescriptor : kotlinClasses) {
      KotlinType kotlinType =
          TypeUtils.substituteProjectionsForParameters(classDescriptor, type.getArguments());
      result.add(kotlinType);
    }

    return result;
  }
Пример #16
0
 @Nullable
 public static KotlinType createSubstitutedSupertype(
     @NotNull KotlinType subType,
     @NotNull KotlinType superType,
     @NotNull TypeSubstitutor substitutor) {
   KotlinType substitutedType = substitutor.substitute(superType, Variance.INVARIANT);
   if (substitutedType != null) {
     return makeNullableIfNeeded(substitutedType, subType.isMarkedNullable());
   }
   return null;
 }
Пример #17
0
  public static boolean shouldRecordInitializerForProperty(
      @NotNull VariableDescriptor variable, @NotNull KotlinType type) {
    if (variable.isVar() || type.isError()) return false;

    if (TypeUtils.acceptsNullable(type)) return true;

    KotlinBuiltIns builtIns = getBuiltIns(variable);
    return KotlinBuiltIns.isPrimitiveType(type)
        || KotlinTypeChecker.DEFAULT.equalTypes(builtIns.getStringType(), type)
        || KotlinTypeChecker.DEFAULT.equalTypes(builtIns.getNumber().getDefaultType(), type)
        || KotlinTypeChecker.DEFAULT.equalTypes(builtIns.getAnyType(), type);
  }
Пример #18
0
 @NotNull
 public static List<KotlinType> getImmediateSupertypes(@NotNull KotlinType type) {
   TypeSubstitutor substitutor = TypeSubstitutor.create(type);
   Collection<KotlinType> originalSupertypes = type.getConstructor().getSupertypes();
   List<KotlinType> result = new ArrayList<KotlinType>(originalSupertypes.size());
   for (KotlinType supertype : originalSupertypes) {
     KotlinType substitutedType = createSubstitutedSupertype(type, supertype, substitutor);
     if (substitutedType != null) {
       result.add(substitutedType);
     }
   }
   return result;
 }
Пример #19
0
 /**
  * A work-around of the generic nullability problem in the type checker Semantics should be the
  * same as `!isSubtype(T, Any)`
  *
  * @return true if a value of this type can be null
  */
 public static boolean isNullableType(@NotNull KotlinType type) {
   if (type.isMarkedNullable()) {
     return true;
   }
   if (FlexibleTypesKt.isFlexible(type)
       && isNullableType(FlexibleTypesKt.flexibility(type).getUpperBound())) {
     return true;
   }
   if (isTypeParameter(type)) {
     return hasNullableSuperType(type);
   }
   return false;
 }
Пример #20
0
 @NotNull
 public static KotlinType getPrimitiveNumberType(
     @NotNull IntegerValueTypeConstructor numberValueTypeConstructor,
     @NotNull KotlinType expectedType) {
   if (noExpectedType(expectedType) || expectedType.isError()) {
     return getDefaultPrimitiveNumberType(numberValueTypeConstructor);
   }
   for (KotlinType primitiveNumberType : numberValueTypeConstructor.getSupertypes()) {
     if (KotlinTypeChecker.DEFAULT.isSubtypeOf(primitiveNumberType, expectedType)) {
       return primitiveNumberType;
     }
   }
   return getDefaultPrimitiveNumberType(numberValueTypeConstructor);
 }
Пример #21
0
 private static boolean isSameClass(
     @NotNull KotlinType type, @NotNull DeclarationDescriptor other) {
   DeclarationDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   if (descriptor != null) {
     DeclarationDescriptor originalDescriptor = descriptor.getOriginal();
     if (originalDescriptor instanceof ClassifierDescriptor
         && other instanceof ClassifierDescriptor
         && ((ClassifierDescriptor) other)
             .getTypeConstructor()
             .equals(((ClassifierDescriptor) originalDescriptor).getTypeConstructor())) {
       return true;
     }
   }
   return false;
 }
Пример #22
0
  @NotNull
  public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) {
    Flexibility flexibility = type.getCapability(Flexibility.class);
    if (flexibility != null) {
      return flexibility.makeNullableAsSpecified(nullable);
    }

    // Wrapping serves two purposes here
    // 1. It's requires less memory than copying with a changed nullability flag: a copy has many
    // fields, while a wrapper has only one
    // 2. It preserves laziness of types

    // Unwrap to avoid long delegation call chains
    if (type instanceof AbstractTypeWithKnownNullability) {
      return makeNullableAsSpecified(((AbstractTypeWithKnownNullability) type).delegate, nullable);
    }

    // checking to preserve laziness
    if (!(type instanceof LazyType) && type.isMarkedNullable() == nullable) {
      return type;
    }

    return nullable ? new NullableType(type) : new NotNullType(type);
  }
Пример #23
0
 @NotNull
 public static List<TypeProjection> getParameterTypeProjectionsFromFunctionType(
     @NotNull KotlinType type) {
   assert isFunctionOrExtensionFunctionType(type);
   List<TypeProjection> arguments = type.getArguments();
   int first = isExtensionFunctionType(type) ? 1 : 0;
   int last = arguments.size() - 2;
   // TODO: fix bugs associated with this here and in neighboring methods, see KT-9820
   assert first <= last + 1 : "Not an exact function type: " + type;
   List<TypeProjection> parameterTypes = new ArrayList<TypeProjection>(last - first + 1);
   for (int i = first; i <= last; i++) {
     parameterTypes.add(arguments.get(i));
   }
   return parameterTypes;
 }
Пример #24
0
 private static boolean isNotNullConstructedFromGivenClass(
     @NotNull KotlinType type, @NotNull FqNameUnsafe fqName) {
   return !type.isMarkedNullable() && isConstructedFromGivenClass(type, fqName);
 }
Пример #25
0
 public static boolean isNullableAny(@NotNull KotlinType type) {
   return isAnyOrNullableAny(type) && type.isMarkedNullable();
 }
Пример #26
0
 @NotNull
 public static KotlinType getReturnTypeFromFunctionType(@NotNull KotlinType type) {
   assert isFunctionOrExtensionFunctionType(type);
   List<TypeProjection> arguments = type.getArguments();
   return arguments.get(arguments.size() - 1).getType();
 }
Пример #27
0
 private static boolean isTypeAnnotatedWithExtension(@NotNull KotlinType type) {
   return type.getAnnotations().findAnnotation(FQ_NAMES.extensionFunctionType) != null
       || type.getAnnotations().findAnnotation(FQ_NAMES.deprecatedExtensionAnnotation) != null;
 }
Пример #28
0
 public static boolean isExactFunctionOrExtensionFunctionType(@NotNull KotlinType type) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return descriptor != null && isNumberedFunctionClassFqName(getFqName(descriptor));
 }
Пример #29
0
 public static boolean isPrimitiveArray(@NotNull KotlinType type) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return descriptor != null && getPrimitiveTypeByArrayClassFqName(getFqName(descriptor)) != null;
 }
Пример #30
0
 public static boolean isPrimitiveType(@NotNull KotlinType type) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return !type.isMarkedNullable()
       && descriptor instanceof ClassDescriptor
       && isPrimitiveClass((ClassDescriptor) descriptor);
 }