Skip to content

Fix quoted type tags and use them instead of pickling #4204

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

Merged
merged 2 commits into from
Apr 11, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ object PickledQuotes {
private def classTagToTypeTree(ct: ClassTag[_])(implicit ctx: Context): TypeTree = {
val tpe = ct match {
case ClassTag.Unit => defn.UnitType
case ClassTag.Boolean => defn.BooleanType
case ClassTag.Byte => defn.ByteType
case ClassTag.Char => defn.CharType
case ClassTag.Short => defn.ShortType
case ClassTag.Int => defn.IntType
case ClassTag.Long => defn.LongType
case ClassTag.Float => defn.FloatType
case ClassTag.Double => defn.FloatType
case ClassTag.Double => defn.DoubleType
}
TypeTree(tpe)
}
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,19 @@ class ReifyQuotes extends MacroTransformWithImplicits {
liftList(splices, defn.AnyType))
}
if (splices.nonEmpty) pickleAsTasty()
else if (isType) {
def tag(tagName: String) = ref(defn.QuotedTypeModule).select(tagName.toTermName)
if (body.symbol == defn.UnitClass) tag("UnitTag")
else if (body.symbol == defn.BooleanClass) tag("BooleanTag")
else if (body.symbol == defn.ByteClass) tag("ByteTag")
else if (body.symbol == defn.CharClass) tag("CharTag")
else if (body.symbol == defn.ShortClass) tag("ShortTag")
else if (body.symbol == defn.IntClass) tag("IntTag")
else if (body.symbol == defn.LongClass) tag("LongTag")
else if (body.symbol == defn.FloatClass) tag("FloatTag")
else if (body.symbol == defn.DoubleClass) tag("DoubleTag")
else pickleAsTasty()
}
else ReifyQuotes.toValue(body) match {
case Some(value) => pickleAsValue(value)
case _ => pickleAsTasty()
Expand Down
14 changes: 9 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,15 @@ trait Implicits { self: Typer =>
val etag = inferImplicitArg(defn.ClassTagType.appliedTo(elemTp), pos)
if (etag.tpe.isError) EmptyTree else etag.select(nme.wrap)
case tp if hasStableErasure(tp) && !defn.isBottomClass(tp.typeSymbol) =>
ref(defn.ClassTagModule)
.select(nme.apply)
.appliedToType(tp)
.appliedTo(clsOf(erasure(tp)))
.withPos(pos)
val sym = tp.typeSymbol
if (sym == defn.UnitClass || sym == defn.AnyClass || sym == defn.AnyValClass)
ref(defn.ClassTagModule).select(sym.name.toTermName).withPos(pos)
else
ref(defn.ClassTagModule)
.select(nme.apply)
.appliedToType(tp)
.appliedTo(clsOf(erasure(tp)))
.withPos(pos)
case tp =>
EmptyTree
}
Expand Down
10 changes: 10 additions & 0 deletions tests/run-with-compiler/quote-type-tags.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
().asInstanceOf[Unit]
true.asInstanceOf[Boolean]
0.toByte.asInstanceOf[Byte]
'a'.asInstanceOf[Char]
1.toShort.asInstanceOf[Short]
2.asInstanceOf[Int]
3L.asInstanceOf[Long]
4.0.asInstanceOf[Float]
5.0.asInstanceOf[Double]
5.0.asInstanceOf[Boolean]
23 changes: 23 additions & 0 deletions tests/run-with-compiler/quote-type-tags.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import dotty.tools.dotc.quoted.Toolbox._

import scala.quoted._

object Test {
def main(args: Array[String]): Unit = {
def asof[T, U](x: Expr[T], t: Type[U]): Expr[U] =
'((~x).asInstanceOf[~t])

println(asof('(), '[Unit]).show)
println(asof('(true), '[Boolean]).show)
println(asof('(0.toByte), '[Byte]).show)
println(asof('( 'a' ), '[Char]).show)
println(asof('(1.toShort), '[Short]).show)
println(asof('(2), '[Int]).show)
println(asof('(3L), '[Long]).show)
println(asof('(4f), '[Float]).show)
println(asof('(5d), '[Double]).show)

println(asof('(5d), '[Boolean]).show) // Will clearly fail at runtime but the code can be generated
}
}
14 changes: 14 additions & 0 deletions tests/run/i4205.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
true
true
true
true
true
true
true
true
true
true
true
true
true
true
22 changes: 22 additions & 0 deletions tests/run/i4205.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scala.reflect.ClassTag
import scala.runtime.BoxedUnit

object Test {
def main(args: Array[String]): Unit = {
println(implicitly[ClassTag[Unit]] == ClassTag.Unit)
println(implicitly[ClassTag[Boolean]] == ClassTag.Boolean)
println(implicitly[ClassTag[Byte]] == ClassTag.Byte)
println(implicitly[ClassTag[Char]] == ClassTag.Char)
println(implicitly[ClassTag[Short]] == ClassTag.Short)
println(implicitly[ClassTag[Int]] == ClassTag.Int)
println(implicitly[ClassTag[Long]] == ClassTag.Long)
println(implicitly[ClassTag[Float]] == ClassTag.Float)
println(implicitly[ClassTag[Double]] == ClassTag.Double)
println(implicitly[ClassTag[Object]] == ClassTag.Object)
println(implicitly[ClassTag[Any]] == ClassTag.Any)
println(implicitly[ClassTag[AnyRef]] == ClassTag.AnyRef)
println(implicitly[ClassTag[AnyVal]] == ClassTag.AnyVal)

println(implicitly[ClassTag[BoxedUnit]] != ClassTag.Unit)
}
}