Skip to content

Commit

Permalink
Dealias types when deriving names (Scala 3) (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski authored Jun 1, 2023
1 parent 439f97d commit 58a17c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/core/macro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ object Macro:

def normalizedName(s: Symbol): String =
if s.flags.is(Flags.Module) then s.name.stripSuffix("$") else s.name
def name(tpe: TypeRepr): Expr[String] = tpe match
case TermRef(typeRepr, name) if tpe.typeSymbol.flags.is(Flags.Module) =>
def name(tpe: TypeRepr): Expr[String] = tpe.dealias match
case matchedTpe @ TermRef(typeRepr, name) if matchedTpe.typeSymbol.flags.is(Flags.Module) =>
Expr(name.stripSuffix("$"))
case TermRef(typeRepr, name) => Expr(name)
case _ => Expr(normalizedName(tpe.typeSymbol))
case matchedTpe => Expr(normalizedName(matchedTpe.typeSymbol))

def ownerNameChain(sym: Symbol): List[String] =
if sym.isNoSymbol then List.empty
Expand All @@ -151,7 +151,7 @@ object Macro:
else ownerNameChain(sym.owner) :+ normalizedName(sym)

def owner(tpe: TypeRepr): Expr[String] = Expr(
ownerNameChain(tpe.typeSymbol.maybeOwner).mkString(".")
ownerNameChain(tpe.dealias.typeSymbol.maybeOwner).mkString(".")
)

def typeInfo(tpe: TypeRepr): Expr[TypeInfo] = tpe match
Expand Down
20 changes: 20 additions & 0 deletions src/test/ProductsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ class ProductsTests extends munit.FunSuite:
assertEquals(res.full, "magnolia1.tests.ProductsTests.Fruit")
}

test("case class parameter typeName should be dealiased") {
given stringTypeName: TypeNameInfo[String] with {
def name = ???

def subtypeNames = ???
}
val res1 = TypeNameInfo.derived[Parameterized[Domain1.Type]].name
val res2 = TypeNameInfo.derived[Parameterized[Domain2.Type]].name
assertEquals(res1.typeParams.head.short, "Int")
assertEquals(res2.typeParams.head.short, "String")
}

test("show chained error stack when leaf instance is missing") {
val error = compileErrors("Show.derived[Schedule]")
assert(
Expand Down Expand Up @@ -266,5 +278,13 @@ object ProductsTests:

case class Fruit(name: String)

case class Parameterized[T](t: T)

object Domain1:
type Type = Int

object Domain2:
type Type = String

object Fruit:
given showFruit: Show[String, Fruit] = (f: Fruit) => f.name

0 comments on commit 58a17c0

Please sign in to comment.