All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aya.tyck.error.CircularSignatureError Maven / Gradle / Ivy

There is a newer version: 0.33.0
Show newest version
// Copyright (c) 2020-2021 Yinsen (Tesla) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.tyck.error;

import kala.collection.immutable.ImmutableSeq;
import org.aya.api.distill.DistillerOptions;
import org.aya.api.error.Problem;
import org.aya.concrete.remark.Remark;
import org.aya.concrete.stmt.Decl;
import org.aya.concrete.stmt.Sample;
import org.aya.concrete.stmt.Signatured;
import org.aya.pretty.doc.Doc;
import org.aya.tyck.order.TyckUnit;
import org.aya.util.error.SourcePos;
import org.jetbrains.annotations.NotNull;

import java.util.Comparator;

public record CircularSignatureError(
  @NotNull ImmutableSeq cycles
) implements Problem {
  @Override public @NotNull SourcePos sourcePos() {
    return cycles.view().map(TyckUnit::sourcePos)
      .max(Comparator.comparingInt(SourcePos::endLine));
  }

  @Override public @NotNull Doc describe(@NotNull DistillerOptions options) {
    return Doc.sep(
      Doc.english("Circular signature dependency found between"),
      Doc.commaList(cycles.view().map(this::nameOf).toImmutableSeq()
        .sorted().view().map(Doc::plain))
    );
  }

  private @NotNull String nameOf(@NotNull TyckUnit stmt) {
    return switch (stmt) {
      case Decl decl -> decl.ref().name();
      case Sample sample -> nameOf(sample.delegate());
      case Remark remark -> "a remark";
      case Signatured signatured -> signatured.ref().name();
      default -> throw new IllegalStateException("Unexpected stmt seen in SCCTycker: " + stmt);
    };
  }

  @Override public @NotNull Severity level() {
    return Severity.ERROR;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy