Skip to content

Commit

Permalink
fix: direct recursion
Browse files Browse the repository at this point in the history
fixes that recursive structures could lead to an infinite loop at
runtime if the very first field of a product would recur to the
"parent" sum (because of a lack of lazyness).
  • Loading branch information
MartinHH committed Sep 15, 2023
1 parent 5d44d58 commit bd8ce19
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/derived/ArbitraryDeriving.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ private object Gens:

def apply[T](gen: Gen[T]): Gens[T] = Gens(List(gen))

inline private def tupleInstance[T <: Tuple]: Gens[T] =
inline private def tupleInstance[T <: Tuple](isHead: Boolean): Gens[T] =
inline erasedValue[T] match
case _: EmptyTuple =>
Gens(Gen.const(EmptyTuple.asInstanceOf[T]))
case _: (t *: ts) =>
val gen: Gen[T] =
for {
tVal <- scalacheck.anyGivenArbitrary[t].arbitrary
tsVal <- tupleInstance[ts].gen
tVal <-
// only for the very first member of a product, some extra lazyness is needed to
// ensure we don't end up in an endless loop in case of recursive structures
if (isHead) Gen.lzy(scalacheck.anyGivenArbitrary[t].arbitrary)
else scalacheck.anyGivenArbitrary[t].arbitrary
tsVal <- tupleInstance[ts](false).gen
} yield (tVal *: tsVal).asInstanceOf[T]
Gens(gen)

inline def productInstance[T](p: Mirror.ProductOf[T]): Gens[T] =
Gens(tupleInstance[p.MirroredElemTypes].gen.map(p.fromProduct(_)))
Gens(tupleInstance[p.MirroredElemTypes](true).gen.map(p.fromProduct(_)))

private inline def summonSumInstances[T, Elems <: Tuple]: List[Gens[T]] =
inline erasedValue[Elems] match
Expand Down
4 changes: 4 additions & 0 deletions src/test/ArbitraryDerivingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class ArbitraryDerivingSuite extends munit.FunSuite:
equalValues(MaybeMaybeList.expectedGen[Int])
}

test("supports direct recursion)") {
equalValues(DirectRecursion.expectedGen)
}

// not a hard requirement (just guarding against accidental worsening by refactoring)
test("supports case classes with up to 26 fields (if -Xmax-inlines=32)") {
summon[Arbitrary[MaxCaseClass]]
Expand Down
4 changes: 4 additions & 0 deletions src/test/CogenDerivingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class CogenDerivingSuite extends munit.ScalaCheckSuite:
equalValues(MaybeMaybeList.expectedCogen[Int])
}

test("given derivation supports direct recursion") {
equalValues(DirectRecursion.expectedCogen)
}

test("enables derivation of Arbitrary instances for functions") {
val arbFunction1: Arbitrary[ComplexADTWithNestedMembers => ABC] =
summon
Expand Down
4 changes: 4 additions & 0 deletions src/test/ShrinkDerivingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class ShrinkDerivingSuite extends munit.ScalaCheckSuite:
equalValues(MaybeMaybeList.expectedShrink[Int])
}

property("supports direct recursion") {
equalValues(DirectRecursion.expectedShrink)
}

// seems there is no feasible way to get up to par with ArbitraryDeriving, so this is just a
// guard against making things even worse
test("supports case classes with up to 25 fields (if -Xmax-inlines=32)") {
Expand Down
40 changes: 40 additions & 0 deletions src/test/test_classes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,46 @@ object MaybeMaybeList:
mml => (mml.head, mml.tail)
)(shrinkTuple)

enum DirectRecursion:
case Continue(next: DirectRecursion)
case Stop

object DirectRecursion:

def expectedGen: Gen[DirectRecursion] =
Gen.oneOf(Gen.lzy(expectedGen.map(Continue(_))), Gen.const(Stop))

def expectedCogen: Cogen[DirectRecursion] =
Cogen { (seed, value) =>
value match
case Continue(dr) =>
perturb(
perturb[Unit](
perturb[DirectRecursion](
seed,
dr
)(expectedCogen),
()
),
0
)
case Stop =>
perturbSingletonInSum(1, seed, Stop)
}

@annotation.nowarn("msg=Stream .* is deprecated")
def expectedShrink: Shrink[DirectRecursion] =
Shrink {
case c: Continue =>
Shrink
.xmap[DirectRecursion, Continue](
Continue.apply,
_.next
)(expectedShrink)
.shrink(c)
case Stop => Stream.empty
}

// format: off
case class MaxCaseClass(
a1: Int, b1: Int, c1: Int, d1: Int, e1: Int, f1: Int, g1: Int, h1: Int, i1: Int, j1: Int,
Expand Down

0 comments on commit bd8ce19

Please sign in to comment.