Skip to content

Commit

Permalink
Add better error message on StackOverflow in macros, prevent SO in so…
Browse files Browse the repository at this point in the history
…me cases with Scala 3 enums mapping
  • Loading branch information
MateuszKubuszok committed May 7, 2024
1 parent 4a37a24 commit a34c153
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
}
def isCaseVal[A](implicit A: Type[A]): Boolean = {
val sym = TypeRepr.of(using A).typeSymbol
sym.isPublic && sym.flags.is(Flags.Case | Flags.Enum | Flags.JavaStatic)
sym.isPublic && sym.flags
.is(Flags.Case | Flags.Enum) && (sym.flags.is(Flags.JavaStatic) || sym.flags.is(Flags.StableRealizable))
}
def isJavaEnumValue[A: Type]: Boolean =
Type[A] <:< scala.quoted.Type.of[java.lang.Enum[?]] && !TypeRepr.of[A].typeSymbol.isAbstract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait SealedHierarchiesPlatform extends SealedHierarchies { this: DefinitionsPla

def isSealed[A: Type]: Boolean = {
val flags = TypeRepr.of[A].typeSymbol.flags
flags.is(Flags.Enum) || flags.is(Flags.Sealed)
flags.is(Flags.Sealed) // do NOT use flags.is(Flags.Enum) since it will also match enums cases!
}

def parse[A: Type]: Option[Enum[A]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ object DerivationError {
.collectFirst {
case MacroException(exception) =>
val stackTrace =
exception.getStackTrace.view.take(10).map(ste => s" \t${Console.RED}$ste${Console.RESET}").mkString("\n")
s" macro expansion thrown exception!: $exception:\n$stackTrace\n \t${Console.RED}...${Console.RESET}"
exception.getStackTrace.view
.take(10)
.map(ste => s" \t${Console.RED}$ste${Console.RESET}")
.mkString("\n") + s"\n \t${Console.RED}...${Console.RESET}"
exception match {
case _: StackOverflowError =>
s" macro expansion thrown StackOverflow - usually it's a sign that JVM need larger Stack. Increase it with e.g. -Xss64m passed to JVM!: $exception:\n$stackTrace"

Check warning on line 23 in chimney/src/main/scala/io/scalaland/chimney/internal/compiletime/DerivationError.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala/io/scalaland/chimney/internal/compiletime/DerivationError.scala#L22-L23

Added lines #L22 - L23 were not covered by tests
case _ =>
s" macro expansion thrown exception!: $exception:\n$stackTrace"
}
case NotYetImplemented(what) =>
s" derivation failed because functionality $what is not yet implemented!"
}
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -1677,8 +1677,8 @@ On Scala 2 `java.lang.UnsupportedOperationException: Position.point on NoPositio
recursive derivation (not only in Chimney) can overflow this stack trace, but on Scala 2 it can become notorious in
the form of an empty value used by the macro to report where error happened.

These issues can be addressed by increasing the compiler's JVM stack size, passing it e.g. -Xss64m (to increase the size
to 64MB).
These issues can be addressed by increasing the compiler's JVM stack size, passing it e.g. `-Xss64m` (to increase
the size to 64MB).

However, if you are using the compiler's flags to report unused definitions when macros are involved, there can also be
an error caused by [scala/bug#12895](https://github.com/scala/bug/issues/12895). This bug was fixed in Scala 2.13.14,
Expand Down

0 comments on commit a34c153

Please sign in to comment.