Skip to content

Commit bff91a8

Browse files
committed
Pickle primitive classes using Reflection.TypeRepr.typeConstructorOf directly
This removes the need of some extra helper methods in the internal package.
1 parent f03c23d commit bff91a8

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
16031603
def showAnsiColored: String =
16041604
SourceCode.showType(using QuoteContextImpl.this)(self)(SyntaxHighlight.ANSI)
16051605

1606-
def seal: scala.quoted.Type[_] =
1606+
def seal: scala.quoted.Type[_] = self.asType
1607+
1608+
def asType: scala.quoted.Type[?] =
16071609
new scala.internal.quoted.Type(Inferred(self), QuoteContextImpl.this.hashCode)
16081610

16091611
def =:=(that: TypeRepr): Boolean = self =:= that

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,30 @@ class ReifyQuotes extends MacroTransform {
199199
ref(unpickleMeth).appliedToType(originalTp).appliedTo(pickledQuote)
200200
}
201201

202-
def taggedType(sym: Symbol) = ref(defn.InternalQuotedTypeModule).select(sym.name.toTermName)
202+
/** Encode quote using Reflection.TypeRepr.typeConstructorOf
203+
*
204+
* Generate the code
205+
* ```scala
206+
* qctx => qctx.reflect.TypeReprMethods.asType(
207+
* qctx.reflect.TypeRepr.typeConstructorOf(classOf[<type>]])
208+
* ).asInstanceOf[scala.quoted.Type[<type>]]
209+
* ```
210+
* this closure is always applied directly to the actual context and the BetaReduce phase removes it.
211+
*/
212+
def taggedType() =
213+
val typeType = defn.QuotedTypeClass.typeRef.appliedTo(body.tpe)
214+
val classTree = TypeApply(ref(defn.Predef_classOf.termRef), body :: Nil)
215+
val tpe = MethodType(defn.QuoteContextClass.typeRef :: Nil, typeType)
216+
val meth = newSymbol(ctx.owner, UniqueName.fresh(nme.ANON_FUN), Synthetic | Method, tpe)
217+
def mkConst(tss: List[List[Tree]]) = {
218+
val reflect = tss.head.head.select("reflect".toTermName)
219+
val typeRepr = reflect.select("TypeRepr".toTermName).select("typeConstructorOf".toTermName).appliedTo(classTree)
220+
reflect.select("TypeReprMethods".toTermName).select("asType".toTermName).appliedTo(typeRepr).asInstance(typeType)
221+
}
222+
Closure(meth, mkConst).withSpan(body.span)
203223

204224
if (isType) {
205-
if (splices.isEmpty && body.symbol.isPrimitiveValueClass) taggedType(body.symbol)
225+
if (splices.isEmpty && body.symbol.isPrimitiveValueClass) taggedType()
206226
else pickleAsTasty()
207227
}
208228
else getLiteral(body) match {

library/src-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,4 @@ object Type {
4141
qctx.asInstanceOf[QuoteContextInternal].typeMatch(scrutineeType, patternType).asInstanceOf[Option[Tup]]
4242
}
4343

44-
45-
// TODO generalize following optimizations for all classes without parameters
46-
47-
def Unit: QuoteContext ?=> quoted.Type[Unit] =
48-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Unit]).seal.asInstanceOf[quoted.Type[Unit]]
49-
50-
def Boolean: QuoteContext ?=> quoted.Type[Boolean] =
51-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Boolean]).seal.asInstanceOf[quoted.Type[Boolean]]
52-
53-
def Byte: QuoteContext ?=> quoted.Type[Byte] =
54-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Byte]).seal.asInstanceOf[quoted.Type[Byte]]
55-
56-
def Char: QuoteContext ?=> quoted.Type[Char] =
57-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Char]).seal.asInstanceOf[quoted.Type[Char]]
58-
59-
def Short: QuoteContext ?=> quoted.Type[Short] =
60-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Short]).seal.asInstanceOf[quoted.Type[Short]]
61-
62-
def Int: QuoteContext ?=> quoted.Type[Int] =
63-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Int]).seal.asInstanceOf[quoted.Type[Int]]
64-
65-
def Long: QuoteContext ?=> quoted.Type[Long] =
66-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Long]).seal.asInstanceOf[quoted.Type[Long]]
67-
68-
def Float: QuoteContext ?=> quoted.Type[Float] =
69-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Float]).seal.asInstanceOf[quoted.Type[Float]]
70-
71-
def Double: QuoteContext ?=> quoted.Type[Double] =
72-
qctx.reflect.TypeRepr.typeConstructorOf(classOf[Double]).seal.asInstanceOf[quoted.Type[Double]]
73-
7444
}

library/src/scala/tasty/Reflection.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,17 @@ trait Reflection { reflection =>
17741774
/** Convert `TypeRepr` to an `quoted.Type[_]` */
17751775
def seal: scala.quoted.Type[_]
17761776

1777+
/** Convert this `TypeRepr` to an `Type[?]`
1778+
*
1779+
* Usage:
1780+
* ```
1781+
* typeRepr.asType match
1782+
* case '[$t] =>
1783+
* '{ val x: t = ... }
1784+
* ```
1785+
*/
1786+
def asType: scala.quoted.Type[?]
1787+
17771788
/** Is `self` type the same as `that` type?
17781789
* This is the case iff `self <:< that` and `that <:< self`.
17791790
*/

0 commit comments

Comments
 (0)