Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealias types when deriving names (Scala 3) #468

Merged
merged 3 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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