Skip to content

Commit

Permalink
suggest recur when functions use match and recur. (#1312)
Browse files Browse the repository at this point in the history
* Suggest recur when recurring without

* fix tests
  • Loading branch information
johnynek authored Dec 11, 2024
1 parent 4dc0a00 commit 181dfe0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/DefRecursionCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ object DefRecursionCheck {
case class InvalidRecursion(name: Bindable, illegalPosition: Region)
extends RecursionError {
def region = illegalPosition
def message = s"invalid recursion on ${name.sourceCodeRepr}"
def message = s"invalid recursion on ${name.sourceCodeRepr}. Consider replacing `match` with `recur`."
}

case class NotEnoughRecurArgs(name: Bindable, illegalPosition: Region)
extends RecursionError {
def region = illegalPosition
def message = s"not enough args to ${name.sourceCodeRepr} to check recursion safety."
}
case class IllegalShadow(fnname: Bindable, decl: Declaration)
extends RecursionError {
Expand Down Expand Up @@ -430,7 +436,7 @@ object DefRecursionCheck {
groups.get(group.toLong).flatMap(_.get(idx.toLong)) match {
case None =>
// not enough args to check recursion
failSt(InvalidRecursion(nm, region))
failSt(NotEnoughRecurArgs(nm, region))
case Some(arg) =>
toSt(allowedRecursion(irb.defname, branch, names, arg)) *>
setSt(irb.incRecCount) // we have recurred again
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ main = fn
te.message(
Map.empty,
Colorize.None
) == "in file: <unknown source>, package A\ninvalid recursion on fn\nRegion(63,79)\n"
) == "in file: <unknown source>, package A\ninvalid recursion on fn. Consider replacing `match` with `recur`.\nRegion(63,79)\n"
)
()
}
Expand Down

0 comments on commit 181dfe0

Please sign in to comment.