Skip to content

Commit

Permalink
Fix ClassCastException for lowercase objects in Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb authored and MateuszKubuszok committed Sep 27, 2024
1 parent 361d4ce commit 7ce431b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ private[compiletime] trait ExprPromisesPlatform extends ExprPromises { this: Def
// Scala 3's enums' parameterless cases are vals with type erased, so w have to match them by value
// case arg @ Enum.Value => ...
CaseDef(Bind(bindName, Ident(sym.termRef)), None, body)
else if sym.flags.is(Flags.Module) then
// case objects are also matched by value but the tree for them is generated in a slightly different way
// case arg @ Enum.Value => ...
CaseDef(Bind(bindName, Ident(sym.companionModule.termRef)), None, body)
else
// case arg : Enum.Value => ...
CaseDef(Bind(bindName, Typed(Wildcard(), TypeTree.of[SomeFrom])), None, body)
Expand Down
19 changes: 19 additions & 0 deletions chimney/src/test/scala/io/scalaland/chimney/IssuesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,23 @@ class IssuesSpec extends ChimneySpec {
.withFieldConst(_.bar.matchingSome.baz.everyItem.b, "new")
.transform ==> Foo(Some(Bar(List(Baz(a = 10, b = "new", c = 10.0), Baz(a = 10, b = "new", c = 20.0)))))
}

test("fix-issue #479") {
import Issue479.*
val orangeTarget = Target.Impl("orange")
val pinkTarget = Target.Impl("pink")
val yellowTarget = Target.Impl("yellow")

val writer: Transformer[color, Target] = Transformer
.define[color, Target]
.withCoproductInstance[color.orange.type](_ => orangeTarget)
.withCoproductInstance[color.pink.type](_ => pinkTarget)
.withCoproductInstance[color.yellow.type](_ => yellowTarget)
.enableMacrosLogging
.buildTransformer

assert(writer.transform(color.pink) == pinkTarget)
assert(writer.transform(color.yellow) == yellowTarget)
assert(writer.transform(color.orange) == orangeTarget)
}
}
14 changes: 14 additions & 0 deletions chimney/src/test/scala/io/scalaland/chimney/fixtures/Issues.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,17 @@ object Issue579 {
case class Bar(baz: List[Baz])
case class Baz(a: Int, b: String, c: Double)
}

object Issue479 {
sealed trait color
object color {
case object orange extends color
case object pink extends color
case object yellow extends color
}

sealed trait Target
object Target {
case class Impl(value: String) extends Target
}
}

0 comments on commit 7ce431b

Please sign in to comment.