From 181dfe005f288a23b83de2105bbe97a4b0f2ddad Mon Sep 17 00:00:00 2001 From: "P. Oscar Boykin" Date: Wed, 11 Dec 2024 09:39:50 -1000 Subject: [PATCH] suggest `recur` when functions use `match` and recur. (#1312) * Suggest recur when recurring without * fix tests --- .../scala/org/bykn/bosatsu/DefRecursionCheck.scala | 10 ++++++++-- .../test/scala/org/bykn/bosatsu/EvaluationTest.scala | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/org/bykn/bosatsu/DefRecursionCheck.scala b/core/src/main/scala/org/bykn/bosatsu/DefRecursionCheck.scala index 6e9fb2d2d..c92292867 100644 --- a/core/src/main/scala/org/bykn/bosatsu/DefRecursionCheck.scala +++ b/core/src/main/scala/org/bykn/bosatsu/DefRecursionCheck.scala @@ -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 { @@ -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 diff --git a/core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala b/core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala index 7caa41b1a..086cf5f13 100644 --- a/core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala +++ b/core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala @@ -1895,7 +1895,7 @@ main = fn te.message( Map.empty, Colorize.None - ) == "in file: , package A\ninvalid recursion on fn\nRegion(63,79)\n" + ) == "in file: , package A\ninvalid recursion on fn. Consider replacing `match` with `recur`.\nRegion(63,79)\n" ) () }