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 quoted types when staging #17059

Merged
merged 2 commits into from
Mar 22, 2023

Conversation

nicolasstucki
Copy link
Contributor

This change improves the code generation of the contents of splices. In the splicing phase, we have to find all types that are defined in the quote but used in the splice. When there are type aliases, we can end up with several Type[T] for the different aliases of T. By dealiasing during the staging phase (just before splicing phase) we make sure that the splicer phase will only generate one Type[T].

By dealiasing we also optimize some situations where a type from outside a quote is inserted in the quoted code and then used in one of its splice through an alias. In this situation we can use the outer Type[T] directly.

Example

After staging the following code should get what is in the comments

import scala.quoted.*

def f[T](using t: Type[T])(using Quotes) =
  '{
    // @SplicedType type t$1 = t.Underlying 
    type T2 = T // type T2 = t$1
    ${

      val t0: T = ???
      val t1: T2 = ??? //  val t1: T = ???
      val tp1 = Type.of[T] // val tp1 = t
      val tp2 = Type.of[T2] //  val tp2 = t
      '{
        // @SplicedType type t$2 = t.Underlying 
        val t3: T = ??? // val t3: t$2 = ???
        val t4: T2 = ???  // val t4: t$2 = ???
      }
    }
  }

def g(using Quotes) =
  '{
    type U
    type U2 = U
    ${

      val u1: U = ???
      val u2: U2 = ??? //  val u2: U = ???

      val tp1 = Type.of[U] // val tp1 = Type.of[U]
      val tp2 = Type.of[U2]  // val tp2 = Type.of[U]
      '{
        val u3: U = ???
        val u4: U2 = ??? // val u4: U = ???
      }
    }
  }

@nicolasstucki nicolasstucki force-pushed the dealias-quoted-type-refrences branch 4 times, most recently from 027a39c to fbfddef Compare March 8, 2023 10:26
@nicolasstucki nicolasstucki requested a review from smarter March 8, 2023 10:27
@nicolasstucki nicolasstucki marked this pull request as ready for review March 8, 2023 10:27
@nicolasstucki nicolasstucki requested review from natsukagami and removed request for smarter March 9, 2023 13:11
@nicolasstucki nicolasstucki assigned natsukagami and unassigned smarter Mar 9, 2023
@nicolasstucki nicolasstucki force-pushed the dealias-quoted-type-refrences branch from fbfddef to 62d1787 Compare March 10, 2023 16:59
@nicolasstucki nicolasstucki force-pushed the dealias-quoted-type-refrences branch 4 times, most recently from a7a59c7 to 91d19ce Compare March 14, 2023 15:40
@nicolasstucki nicolasstucki force-pushed the dealias-quoted-type-refrences branch from 91d19ce to e7b8f61 Compare March 17, 2023 14:18
Copy link
Contributor

@natsukagami natsukagami left a comment

Choose a reason for hiding this comment

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

lgtm!

This change improves the code generation of the contents of splices. In
the splicing phase, we have to find all types that are defined in the
quote but used in the splice. When there are type aliases, we can end up
with several `Type[T]` for the different aliases of `T`. By dealiasing
during the staging phase (just before splicing phase) we make sure that
the splicer phase will only generate one `Type[T]`.

By dealiasing we also optimize some situations where a type from outside
a quote is inserted in the quoted code and then used in one of its
splice through an alias. In this situation we can use the outer
`Type[T]` directly.
@nicolasstucki nicolasstucki force-pushed the dealias-quoted-type-refrences branch from e7b8f61 to d479de6 Compare March 22, 2023 07:04
@nicolasstucki
Copy link
Contributor Author

Rebased, conflicts were automatically resolved.

@nicolasstucki nicolasstucki enabled auto-merge March 22, 2023 07:05
@nicolasstucki nicolasstucki merged commit 081f1eb into scala:main Mar 22, 2023
@nicolasstucki nicolasstucki deleted the dealias-quoted-type-refrences branch March 22, 2023 10:07
@xuwei-k
Copy link
Contributor

xuwei-k commented May 27, 2023

@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants