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

Conversation

kciesielski
Copy link
Member

@kciesielski kciesielski commented May 24, 2023

Fixes #467 for Scala 3

This PR ensures that type names are derived from dealiased underlying types.
Inspired by an issue reported in tapir:

//> using scala 3.3.0-RC6
//> using dep com.softwaremill.sttp.tapir::tapir-core:1.4.0
import sttp.tapir.*
import sttp.tapir.generic.auto.*

case class Foo[T](t: T)

object Domain1:
  type Type = Int

object Domain2:
  type Type = String

@main
def main =
  val d1Schema = summon[Schema[Foo[Domain1.Type]]]
  val d2Schema = summon[Schema[Foo[Domain2.Type]]]
  println(d1Schema.name.map(_.show)) // Some(.Foo[Type]), should be Some(.Foo[Int])
  println(d2Schema.name.map(_.show)) // Some(.Foo[Type]), should be Some(.Foo[String])

This issue doesn't happen for Scala 2, where dealiasing seems to be correctly applied.

@@ -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.

@kciesielski kciesielski merged commit 58a17c0 into scala3 Jun 1, 2023
@kciesielski kciesielski deleted the fix/467-dealias-type-param-name branch June 1, 2023 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type parameter names should should be dealiased for Scala 3
2 participants