private void assertCommonSupertype(String expected, String... types) {
   Collection<JetType> subtypes = new ArrayList<JetType>();
   for (String type : types) {
     subtypes.add(makeType(type));
   }
   JetType result = CommonSupertypes.commonSupertype(subtypes);
   assertTrue(result + " != " + expected, result.equals(makeType(expected)));
 }
 private void assertErrorType(String expression) {
   Project project = getProject();
   JetExpression jetExpression = JetPsiFactory(project).createExpression(expression);
   JetType type =
       expressionTypingServices.safeGetType(
           scopeWithImports,
           jetExpression,
           TypeUtils.NO_EXPECTED_TYPE,
           DataFlowInfo.EMPTY,
           JetTestUtils.DUMMY_TRACE);
   assertTrue("Error type expected but " + type + " returned", type.isError());
 }
 private void assertType(String expression, JetType expectedType) {
   Project project = getProject();
   JetExpression jetExpression = JetPsiFactory(project).createExpression(expression);
   JetType type =
       expressionTypingServices.getType(
           scopeWithImports,
           jetExpression,
           TypeUtils.NO_EXPECTED_TYPE,
           DataFlowInfo.EMPTY,
           JetTestUtils.DUMMY_TRACE);
   assertTrue(type + " != " + expectedType, type.equals(expectedType));
 }
Example #4
0
  private void checkSupertypeList(
      @NotNull ClassDescriptor supertypeOwner,
      @NotNull Map<JetTypeReference, JetType> supertypes,
      @NotNull JetClassOrObject jetClass) {
    Set<TypeConstructor> allowedFinalSupertypes =
        getAllowedFinalSupertypes(supertypeOwner, jetClass);
    Set<TypeConstructor> typeConstructors = Sets.newHashSet();
    boolean classAppeared = false;
    for (Map.Entry<JetTypeReference, JetType> entry : supertypes.entrySet()) {
      JetTypeReference typeReference = entry.getKey();
      JetType supertype = entry.getValue();

      boolean addSupertype = true;

      ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
      if (classDescriptor != null) {
        if (ErrorUtils.isError(classDescriptor)) continue;

        if (classDescriptor.getKind() != ClassKind.INTERFACE) {
          if (supertypeOwner.getKind() == ClassKind.ENUM_CLASS) {
            trace.report(CLASS_IN_SUPERTYPE_FOR_ENUM.on(typeReference));
            addSupertype = false;
          } else if (supertypeOwner.getKind() == ClassKind.INTERFACE
              && !classAppeared
              && !TypesPackage.isDynamic(supertype) /* avoid duplicate diagnostics */) {
            trace.report(TRAIT_WITH_SUPERCLASS.on(typeReference));
            addSupertype = false;
          }

          if (classAppeared) {
            trace.report(MANY_CLASSES_IN_SUPERTYPE_LIST.on(typeReference));
          } else {
            classAppeared = true;
          }
        }
      } else {
        trace.report(SUPERTYPE_NOT_A_CLASS_OR_TRAIT.on(typeReference));
      }

      TypeConstructor constructor = supertype.getConstructor();
      if (addSupertype && !typeConstructors.add(constructor)) {
        trace.report(SUPERTYPE_APPEARS_TWICE.on(typeReference));
      }

      if (DescriptorUtils.isSingleton(classDescriptor)) {
        trace.report(SINGLETON_IN_SUPERTYPE.on(typeReference));
      } else if (constructor.isFinal() && !allowedFinalSupertypes.contains(constructor)) {
        if (classDescriptor.getModality() == Modality.SEALED) {
          DeclarationDescriptor containingDescriptor = supertypeOwner.getContainingDeclaration();
          while (containingDescriptor != null && containingDescriptor != classDescriptor) {
            containingDescriptor = containingDescriptor.getContainingDeclaration();
          }
          if (containingDescriptor == null) {
            trace.report(SEALED_SUPERTYPE.on(typeReference));
          } else {
            trace.report(SEALED_SUPERTYPE_IN_LOCAL_CLASS.on(typeReference));
          }
        } else {
          trace.report(FINAL_SUPERTYPE.on(typeReference));
        }
      }
    }
  }