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 2 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
2 changes: 1 addition & 1 deletion src/core/macro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ object Macro:
)
}
case _ =>
'{ TypeInfo(${ owner(tpe) }, ${ name(tpe) }, Nil) }
'{ TypeInfo(${ owner(tpe) }, ${ name(tpe.dealias) }, Nil) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should also dealias when computing the owner. Otherwise, the full name will be very confusing. For one of the examples it would be magnolia1.tests.ProductsTests.Domain1.Int.

Also the same dealiasing should be done in the AppliedType case, so maybe it would be easier to just move it into name and owner methods. It should be safe enough. i.e.

  def name(tpe: TypeRepr): Expr[String] = tpe.dealias match
    case tpe@TermRef(typeRepr, name) if tpe.typeSymbol.flags.is(Flags.Module) =>
      Expr(name.stripSuffix("$"))
    case TermRef(typeRepr, name) => Expr(name)
    case tpe                       => Expr(normalizedName(tpe.typeSymbol))
...
  def owner(tpe: TypeRepr): Expr[String] = Expr(
    ownerNameChain(tpe.dealias.typeSymbol.maybeOwner).mkString(".")
  )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @KacperFKorban, that would be indeed much better, I updated the PR.


typeInfo(TypeRepr.of[T])

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