예제 #1
0
    private void reportCyclicInheritanceHierarchyError(
        @NotNull BindingTrace trace,
        @NotNull ClassDescriptor classDescriptor,
        @NotNull ClassDescriptor superclass) {
      PsiElement psiElement = DescriptorToSourceUtils.getSourceFromDescriptor(classDescriptor);

      PsiElement elementToMark = null;
      if (psiElement instanceof KtClassOrObject) {
        KtClassOrObject classOrObject = (KtClassOrObject) psiElement;
        for (KtSuperTypeListEntry delegationSpecifier : classOrObject.getSuperTypeListEntries()) {
          KtTypeReference typeReference = delegationSpecifier.getTypeReference();
          if (typeReference == null) continue;
          KotlinType supertype = trace.get(TYPE, typeReference);
          if (supertype != null && supertype.getConstructor() == superclass.getTypeConstructor()) {
            elementToMark = typeReference;
          }
        }
      }
      if (elementToMark == null && psiElement instanceof PsiNameIdentifierOwner) {
        PsiNameIdentifierOwner namedElement = (PsiNameIdentifierOwner) psiElement;
        PsiElement nameIdentifier = namedElement.getNameIdentifier();
        if (nameIdentifier != null) {
          elementToMark = nameIdentifier;
        }
      }
      if (elementToMark != null) {
        trace.report(CYCLIC_INHERITANCE_HIERARCHY.on(elementToMark));
      }
    }
예제 #2
0
 @NotNull
 private static PsiClass findClass(
     String qualifiedName, Project project, KotlinCoreEnvironment environment) {
   ModuleDescriptor module = LazyResolveTestUtil.resolveProject(project, environment);
   ClassDescriptor classDescriptor =
       DescriptorUtilsKt.resolveTopLevelClass(
           module, new FqName(qualifiedName), NoLookupLocation.FROM_TEST);
   assertNotNull("Class descriptor wasn't resolved: " + qualifiedName, classDescriptor);
   PsiClass psiClass = (PsiClass) DescriptorToSourceUtils.getSourceFromDescriptor(classDescriptor);
   assertNotNull("Class declaration wasn't found: " + classDescriptor, psiClass);
   return psiClass;
 }
예제 #3
0
  @NotNull
  public static Pair<FunctionDescriptor, PsiElement> getContainingFunctionSkipFunctionLiterals(
      @Nullable DeclarationDescriptor startDescriptor, boolean strict) {
    FunctionDescriptor containingFunctionDescriptor =
        DescriptorUtils.getParentOfType(startDescriptor, FunctionDescriptor.class, strict);
    PsiElement containingFunction =
        containingFunctionDescriptor != null
            ? DescriptorToSourceUtils.getSourceFromDescriptor(containingFunctionDescriptor)
            : null;
    while (containingFunction instanceof JetFunctionLiteral) {
      containingFunctionDescriptor =
          DescriptorUtils.getParentOfType(containingFunctionDescriptor, FunctionDescriptor.class);
      containingFunction =
          containingFunctionDescriptor != null
              ? DescriptorToSourceUtils.getSourceFromDescriptor(containingFunctionDescriptor)
              : null;
    }

    return new Pair<FunctionDescriptor, PsiElement>(
        containingFunctionDescriptor, containingFunction);
  }
예제 #4
0
  private void checkSupertypesForConsistency(@NotNull ClassDescriptor classDescriptor) {
    Multimap<TypeConstructor, TypeProjection> multimap =
        SubstitutionUtils.buildDeepSubstitutionMultimap(classDescriptor.getDefaultType());
    for (Map.Entry<TypeConstructor, Collection<TypeProjection>> entry :
        multimap.asMap().entrySet()) {
      Collection<TypeProjection> projections = entry.getValue();
      if (projections.size() > 1) {
        TypeConstructor typeConstructor = entry.getKey();
        DeclarationDescriptor declarationDescriptor = typeConstructor.getDeclarationDescriptor();
        assert declarationDescriptor instanceof TypeParameterDescriptor : declarationDescriptor;
        TypeParameterDescriptor typeParameterDescriptor =
            (TypeParameterDescriptor) declarationDescriptor;

        // Immediate arguments of supertypes cannot be projected
        Set<JetType> conflictingTypes = Sets.newLinkedHashSet();
        for (TypeProjection projection : projections) {
          conflictingTypes.add(projection.getType());
        }
        removeDuplicateTypes(conflictingTypes);
        if (conflictingTypes.size() > 1) {
          DeclarationDescriptor containingDeclaration =
              typeParameterDescriptor.getContainingDeclaration();
          assert containingDeclaration instanceof ClassDescriptor : containingDeclaration;
          JetClassOrObject psiElement =
              (JetClassOrObject) DescriptorToSourceUtils.getSourceFromDescriptor(classDescriptor);
          assert psiElement != null;
          JetDelegationSpecifierList delegationSpecifierList =
              psiElement.getDelegationSpecifierList();
          assert delegationSpecifierList != null;
          //
          // trace.getErrorHandler().genericError(delegationSpecifierList.getNode(), "Type parameter
          // " + typeParameterDescriptor.getName() + " of " + containingDeclaration.getName() + "
          // has inconsistent values: " + conflictingTypes);
          trace.report(
              INCONSISTENT_TYPE_PARAMETER_VALUES.on(
                  delegationSpecifierList,
                  typeParameterDescriptor,
                  (ClassDescriptor) containingDeclaration,
                  conflictingTypes));
        }
      }
    }
  }