com-lihaoyi / upickle

uPickle: a simple, fast, dependency-free JSON & Binary (MessagePack) serialization library for Scala
https://com-lihaoyi.github.io/upickle
MIT License
721 stars 165 forks source link

Sealed case class causes compiler crash #628

Closed lihaoyi closed 1 month ago

lihaoyi commented 2 months ago
sealed case class ImportType(name: String)
implicit val importTypeRW: upickle.default.ReadWriter[ImportData.ImportType] =
    upickle.default.macroRW
[error] /Users/lihaoyi/Github/Ammonite/amm/runtime/src/main/scala/ammonite/runtime/Storage.scala:114:21: exception during macro expansion:
[error] java.lang.UnsupportedOperationException: empty.max
[error]     at scala.collection.IterableOnceOps.max(IterableOnce.scala:1127)
[error]     at scala.collection.IterableOnceOps.max$(IterableOnce.scala:1124)
[error]     at scala.collection.AbstractIterable.max(Iterable.scala:935)
[error]     at upickle.implicits.internal.Macros$DeriveDefaults.annotate(Macros.scala:231)
[error]     at upickle.implicits.internal.Macros$DeriveDefaults.annotate$(Macros.scala:209)
[error]     at upickle.implicits.internal.Macros$Reading.annotate(Macros.scala:278)
[error]     at upickle.implicits.internal.Macros$DeriveDefaults.deriveClass(Macros.scala:203)
[error]     at upickle.implicits.internal.Macros$DeriveDefaults.deriveClass$(Macros.scala:160)
[error]     at upickle.implicits.internal.Macros$Reading.deriveClass(Macros.scala:278)
[error]     at upickle.implicits.internal.Macros$DeriveDefaults.derive(Macros.scala:115)
[error]     at upickle.implicits.internal.Macros$DeriveDefaults.derive$(Macros.scala:109)
[error]     at upickle.implicits.internal.Macros$Reading.derive(Macros.scala:278)
[error]     at upickle.implicits.internal.Macros$.macroRImpl(Macros.scala:473)
[error]     upickle.default.macroRW
[error]                     ^

Fails on most Scala versions. It seem sealed is confusing the macro into thinking it's a sealed trait, and then when it finds no case class or case object children it crashes

Workaround is to remove the sealed, but really we should just not crash