@SuppressWarnings("unchecked")
  private static @NotNull <T extends SemElement> List<T> collectSemElements(@NotNull State currentProducers,
                                                                            @NotNull SemKey<T> key,
                                                                            @NotNull PsiElement psi,
                                                                            @Nullable IntObjectMap<List<SemElement>> chunk) {
    RecursionGuard.StackStamp stamp = RecursionManager.markStack();

    List<T> result;
    Int2ObjectMap<List<SemElement>> map;

    ProcessingContext processingContext = new ProcessingContext();
    Collection<SemKey<?>> inheritors = currentProducers.inheritors.getOrDefault(key, emptyList());

    if (inheritors.size() == 1) {
      SemKey<?> singleKey = inheritors.iterator().next();
      List<SemElement> list = createSemElements(currentProducers, singleKey, psi, processingContext);
      map = Int2ObjectMaps.singleton(singleKey.getUniqueId(), list);
      result = (List<T>)list;
    }
    else {
      map = new Int2ObjectOpenHashMap<>(inheritors.size());
      result = new ArrayList<>();

      for (SemKey<?> each : inheritors) {
        List<SemElement> list = createSemElements(currentProducers, each, psi, processingContext);
        map.put(each.getUniqueId(), list);
        if (!list.isEmpty()) {
          result.addAll((List<T>)list);
        }
      }
    }

    if (chunk != null && stamp.mayCacheNow()) {
      Int2ObjectMaps.fastForEach(map, entry -> {
        putSemElements(chunk, entry.getIntKey(), entry.getValue());
      });
    }

    if (result.isEmpty()) return emptyList();
    if (result.size() == 1) return singletonList(result.get(0));

    return result.stream()
      .distinct()
      .toList();
  }
@SuppressWarnings("unchecked")
  private static @NotNull <T extends SemElement> List<T> collectSemElements(@NotNull State currentProducers,
                                                                            @NotNull SemKey<T> key,
                                                                            @NotNull PsiElement psi,
                                                                            @Nullable IntObjectMap<List<SemElement>> chunk)