Skip to content

Commit

Permalink
reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Dec 17, 2023
1 parent 8bcce7c commit ff48bfb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
38 changes: 12 additions & 26 deletions core/src/main/scala/org/bykn/bosatsu/PackageError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -537,75 +537,61 @@ object PackageError {
val region = regions(kindError.dt.toTypeConst)
val ctx = lm.showRegion(region, 2, errColor)
.getOrElse(Doc.str(region)) // we should highlight the whole region
kindError match {
val prefix = sourceMap.headLine(pack, Some(region))
val message = kindError match {
case KindFormula.Error.Unsatisfiable(_, _, _, _) =>
val prefix = sourceMap.headLine(pack, Some(region))
// TODO: would be good to give a more precise problem, e.g. which
// type parameters are the problem.
val message = Doc.text("could not solve for valid variances")
(prefix + Doc.hardLine + message + Doc.hardLine + ctx).render(80)
Doc.text("could not solve for valid variances")
case KindFormula.Error.FromShapeError(se) =>
se match {
case Shape.UnificationError(_, cons, left, right) =>
val prefix = sourceMap.headLine(pack, Some(region))
(prefix + Doc.hardLine + Doc.text("shape error: expected ") + Shape.shapeDoc(left) + Doc.text(" and ") + Shape.shapeDoc(right) +
Doc.text(s" to match in the constructor ${cons.name.sourceCodeRepr}") + Doc.hardLine + Doc.hardLine +
ctx).render(80)
Doc.text("shape error: expected ") + Shape.shapeDoc(left) + Doc.text(" and ") + Shape.shapeDoc(right) +
Doc.text(s" to match in the constructor ${cons.name.sourceCodeRepr}") + Doc.hardLine
case Shape.ShapeMismatch(_, cons, outer, tyApp, right) =>
val tmap = showTypes(pack, outer :: tyApp :: Nil)
val prefix = sourceMap.headLine(pack, Some(region))
val typeDoc =
if (outer != tyApp) (tmap(outer) + Doc.text(" at application ") + tmap(tyApp))
else tmap(outer)

(prefix + Doc.text(" shape error: expected ") + Shape.shapeDoc(right) + Doc.text(" -> ?") + Doc.text(" but found * ") +
Doc.text("shape error: expected ") + Shape.shapeDoc(right) + Doc.text(" -> ?") + Doc.text(" but found * ") +
Doc.text(s"in the constructor ${cons.name.sourceCodeRepr} inside type ") +
typeDoc +
Doc.hardLine + Doc.hardLine +
ctx).render(80)
Doc.hardLine
case Shape.FinishFailure(dt, left, right) =>
val tdoc = showTypes(pack, dt.toTypeTyConst :: Nil)(dt.toTypeTyConst)
val prefix = sourceMap.headLine(pack, Some(region))
val message = Doc.text("in type ") + tdoc + Doc.text(" could not unify shapes: ") + Shape.shapeDoc(left) + Doc.text(" and ") +
Doc.text("in type ") + tdoc + Doc.text(" could not unify shapes: ") + Shape.shapeDoc(left) + Doc.text(" and ") +
Shape.shapeDoc(right)
(prefix + Doc.hardLine + message + Doc.hardLine + ctx).render(80)
case Shape.ShapeLoop(dt, tpe, _) =>
val tpe2 = tpe match {
case Left(ap) => ap
case Right(v) => Type.TyVar(v)
}
val tdocs = showTypes(pack, dt.toTypeTyConst :: tpe2 :: Nil)

val prefix = sourceMap.headLine(pack, Some(region))
val message = Doc.text("in type ") + tdocs(dt.toTypeTyConst) + Doc.text(" cyclic dependency encountered in ") +
Doc.text("in type ") + tdocs(dt.toTypeTyConst) + Doc.text(" cyclic dependency encountered in ") +
tdocs(tpe2)
(prefix + Doc.hardLine + message + Doc.hardLine + ctx).render(80)
case Shape.UnboundVar(dt, cfn, v) =>
val tpe2 = Type.TyVar(v)
val tdocs = showTypes(pack, dt.toTypeTyConst :: tpe2 :: Nil)

val prefix = sourceMap.headLine(pack, Some(region))
val cfnMsg = if (dt.isStruct) Doc.empty else {
Doc.text(s" in constructor ${cfn.name.sourceCodeRepr} ")
}
val message = Doc.text("in type ") + tdocs(dt.toTypeTyConst) +
Doc.text("in type ") + tdocs(dt.toTypeTyConst) +
Doc.text(" unbound type variable ") + tdocs(tpe2) + cfnMsg

(prefix + Doc.hardLine + message + Doc.hardLine + ctx).render(80)
case Shape.UnknownConst(dt, cfn, c) =>
val tpe2 = Type.TyConst(c)
val tdocs = showTypes(pack, dt.toTypeTyConst :: tpe2 :: Nil)

val prefix = sourceMap.headLine(pack, Some(region))
val cfnMsg = if (dt.isStruct) Doc.empty else {
Doc.text(s" in constructor ${cfn.name.sourceCodeRepr} ")
}
val message = Doc.text("in type ") + tdocs(dt.toTypeTyConst) +
Doc.text("in type ") + tdocs(dt.toTypeTyConst) +
Doc.text(" unknown type ") + tdocs(tpe2) + cfnMsg

(prefix + Doc.hardLine + message + Doc.hardLine + ctx).render(80)
}
}
(prefix + Doc.hardLine + message + Doc.hardLine + ctx).render(80)
}
}
}
3 changes: 2 additions & 1 deletion core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2806,7 +2806,8 @@ package Foo
struct Foo[a: *](a: a[Int])
""")) { case kie@PackageError.KindInferenceError(_, _, _) =>
assert(kie.message(Map.empty, Colorize.None) ==
"""in file: <unknown source>, package Foo shape error: expected * -> ? but found * in the constructor Foo inside type a[Bosatsu/Predef::Int]
"""in file: <unknown source>, package Foo
shape error: expected * -> ? but found * in the constructor Foo inside type a[Bosatsu/Predef::Int]
Region(14,41)""")
()
Expand Down

0 comments on commit ff48bfb

Please sign in to comment.