Skip to content

Commit a29e8a3

Browse files
Merge pull request #6950 from dotty-staging/replaced-TaggedType-using-quoteContext
Replaced TaggedType using QuoteContext
2 parents 6d64440 + 3b1a53c commit a29e8a3

File tree

13 files changed

+117
-94
lines changed

13 files changed

+117
-94
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ object PickledQuotes {
5959
tpt
6060
case tpt => tpt
6161
}
62-
case expr: TaggedType[_] => classTagToTypeTree(expr.ct)
6362
case expr: TreeType[Tree] @unchecked => healOwner(expr.typeTree)
6463
}
6564

@@ -127,21 +126,6 @@ object PickledQuotes {
127126
tree
128127
}
129128

130-
private def classTagToTypeTree(ct: ClassTag[_])(implicit ctx: Context): TypeTree = {
131-
val tpe = ct match {
132-
case ClassTag.Unit => defn.UnitType
133-
case ClassTag.Boolean => defn.BooleanType
134-
case ClassTag.Byte => defn.ByteType
135-
case ClassTag.Char => defn.CharType
136-
case ClassTag.Short => defn.ShortType
137-
case ClassTag.Int => defn.IntType
138-
case ClassTag.Long => defn.LongType
139-
case ClassTag.Float => defn.FloatType
140-
case ClassTag.Double => defn.DoubleType
141-
}
142-
TypeTree(tpe)
143-
}
144-
145129
private def functionAppliedTo(fn: Tree, args: List[Tree])(implicit ctx: Context): Tree = {
146130
val (argVals, argRefs) = args.map(arg => arg.tpe match {
147131
case tpe: SingletonType if isIdempotentExpr(arg) => (Nil, arg)

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,27 +202,28 @@ class ReifyQuotes extends MacroTransform {
202202
}
203203

204204
private def pickledQuote(body: Tree, splices: List[Tree], originalTp: Type, isType: Boolean)(implicit ctx: Context) = {
205+
def qctx: Tree = {
206+
val qctx = ctx.typer.inferImplicitArg(defn.QuoteContextClass.typeRef, body.span)
207+
if (qctx.tpe.isInstanceOf[SearchFailureType])
208+
ctx.error(ctx.typer.missingArgMsg(qctx, defn.QuoteContextClass.typeRef, ""), ctx.source.atSpan(body.span))
209+
qctx
210+
}
205211

206212
def liftedValue[T](value: T, name: TermName, qctx: Tree) =
207213
ref(defn.LiftableModule).select(name).select("toExpr".toTermName).appliedTo(Literal(Constant(value))).select(nme.apply).appliedTo(qctx)
208214

209-
def pickleAsValue[T](value: T) = {
210-
val qctx = ctx.typer.inferImplicitArg(defn.QuoteContextClass.typeRef, body.span)
211-
if (qctx.tpe.isInstanceOf[SearchFailureType])
212-
ctx.error(ctx.typer.missingArgMsg(qctx, defn.QuoteContextClass.typeRef, ""), ctx.source.atSpan(body.span))
213-
value match {
214-
case null => ref(defn.QuotedExprModule).select("nullExpr".toTermName).appliedTo(qctx)
215-
case _: Unit => ref(defn.QuotedExprModule).select("unitExpr".toTermName).appliedTo(qctx)
216-
case _: Boolean => liftedValue(value, "Liftable_Boolean_delegate".toTermName, qctx)
217-
case _: Byte => liftedValue(value, "Liftable_Byte_delegate".toTermName, qctx)
218-
case _: Short => liftedValue(value, "Liftable_Short_delegate".toTermName, qctx)
219-
case _: Int => liftedValue(value, "Liftable_Int_delegate".toTermName, qctx)
220-
case _: Long => liftedValue(value, "Liftable_Long_delegate".toTermName, qctx)
221-
case _: Float => liftedValue(value, "Liftable_Float_delegate".toTermName, qctx)
222-
case _: Double => liftedValue(value, "Liftable_Double_delegate".toTermName, qctx)
223-
case _: Char => liftedValue(value, "Liftable_Char_delegate".toTermName, qctx)
224-
case _: String => liftedValue(value, "Liftable_String_delegate".toTermName, qctx)
225-
}
215+
def pickleAsValue[T](value: T) = value match {
216+
case null => ref(defn.QuotedExprModule).select("nullExpr".toTermName).appliedTo(qctx)
217+
case _: Unit => ref(defn.QuotedExprModule).select("unitExpr".toTermName).appliedTo(qctx)
218+
case _: Boolean => liftedValue(value, "Liftable_Boolean_delegate".toTermName, qctx)
219+
case _: Byte => liftedValue(value, "Liftable_Byte_delegate".toTermName, qctx)
220+
case _: Short => liftedValue(value, "Liftable_Short_delegate".toTermName, qctx)
221+
case _: Int => liftedValue(value, "Liftable_Int_delegate".toTermName, qctx)
222+
case _: Long => liftedValue(value, "Liftable_Long_delegate".toTermName, qctx)
223+
case _: Float => liftedValue(value, "Liftable_Float_delegate".toTermName, qctx)
224+
case _: Double => liftedValue(value, "Liftable_Double_delegate".toTermName, qctx)
225+
case _: Char => liftedValue(value, "Liftable_Char_delegate".toTermName, qctx)
226+
case _: String => liftedValue(value, "Liftable_String_delegate".toTermName, qctx)
226227
}
227228

228229
def pickleAsTasty() = {
@@ -239,7 +240,7 @@ class ReifyQuotes extends MacroTransform {
239240
}
240241
if (splices.nonEmpty) pickleAsTasty()
241242
else if (isType) {
242-
def tag(tagName: String) = ref(defn.QuotedTypeModule).select(tagName.toTermName)
243+
def tag(tagName: String) = ref(defn.QuotedTypeModule).select(tagName.toTermName).appliedTo(qctx)
243244
if (body.symbol.isPrimitiveValueClass) tag(s"${body.symbol.name}Tag")
244245
else pickleAsTasty()
245246
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
scala> import quoted._
2+
scala> implicit def toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
3+
def toolbox: quoted.Toolbox
4+
scala> def fn[T : Type](v : T) = println("ok")
5+
def fn[T](v: T)(implicit evidence$1: quoted.Type[T]): Unit
6+
scala> withQuoteContext { fn("foo") }
7+
ok
8+
scala> withQuoteContext { fn((1,2)) }
9+
ok
10+
scala> object O
11+
// defined object O
12+
scala> withQuoteContext { fn(O) }
13+
ok
14+
scala> withQuoteContext { fn(1) }
15+
ok

compiler/test-resources/repl/i6263

Lines changed: 0 additions & 13 deletions
This file was deleted.

library/src/scala/quoted/Type.scala

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala
22

33
package quoted {
4-
import scala.internal.quoted.TaggedType
54
import scala.quoted.show.SyntaxHighlight
65

76
sealed trait Type[T <: AnyKind] {
@@ -17,35 +16,66 @@ package quoted {
1716

1817
/** Some basic type tags, currently incomplete */
1918
object Type {
20-
given UnitTag as Type[Unit] = new TaggedType[Unit]
21-
given BooleanTag as Type[Boolean] = new TaggedType[Boolean]
22-
given ByteTag as Type[Byte] = new TaggedType[Byte]
23-
given CharTag as Type[Char] = new TaggedType[Char]
24-
given ShortTag as Type[Short] = new TaggedType[Short]
25-
given IntTag as Type[Int] = new TaggedType[Int]
26-
given LongTag as Type[Long] = new TaggedType[Long]
27-
given FloatTag as Type[Float] = new TaggedType[Float]
28-
given DoubleTag as Type[Double] = new TaggedType[Double]
19+
20+
given UnitTag as Type[Unit] given (qctx: QuoteContext) = {
21+
import qctx.tasty._
22+
definitions.UnitType.seal.asInstanceOf[quoted.Type[Unit]]
23+
}
24+
25+
given BooleanTag as Type[Boolean] given (qctx: QuoteContext) = {
26+
import qctx.tasty._
27+
definitions.BooleanType.seal.asInstanceOf[quoted.Type[Boolean]]
28+
}
29+
30+
given ByteTag as Type[Byte] given (qctx: QuoteContext) = {
31+
import qctx.tasty._
32+
definitions.ByteType.seal.asInstanceOf[quoted.Type[Byte]]
33+
}
34+
35+
given CharTag as Type[Char] given (qctx: QuoteContext) = {
36+
import qctx.tasty._
37+
definitions.CharType.seal.asInstanceOf[quoted.Type[Char]]
38+
}
39+
40+
given ShortTag as Type[Short] given (qctx: QuoteContext) = {
41+
import qctx.tasty._
42+
definitions.ShortType.seal.asInstanceOf[quoted.Type[Short]]
43+
}
44+
45+
given IntTag as Type[Int] given (qctx: QuoteContext) = {
46+
import qctx.tasty._
47+
definitions.IntType.seal.asInstanceOf[quoted.Type[Int]]
48+
}
49+
50+
given LongTag as Type[Long] given (qctx: QuoteContext) = {
51+
import qctx.tasty._
52+
definitions.LongType.seal.asInstanceOf[quoted.Type[Long]]
53+
}
54+
55+
given FloatTag as Type[Float] given (qctx: QuoteContext) = {
56+
import qctx.tasty._
57+
definitions.FloatType.seal.asInstanceOf[quoted.Type[Float]]
58+
}
59+
60+
given DoubleTag as Type[Double] given (qctx: QuoteContext) = {
61+
import qctx.tasty._
62+
definitions.DoubleType.seal.asInstanceOf[quoted.Type[Double]]
63+
}
64+
2965
}
3066

3167
}
3268

3369
package internal {
3470
package quoted {
3571
import scala.quoted.Type
36-
import scala.reflect.ClassTag
3772
import scala.runtime.quoted.Unpickler.Pickled
3873

3974
/** A Type backed by a pickled TASTY tree */
4075
final class TastyType[T](val tasty: Pickled, val args: Seq[Any]) extends Type[T] {
4176
override def toString(): String = s"Type(<pickled tasty>)"
4277
}
4378

44-
/** An Type backed by a value */
45-
final class TaggedType[T](implicit val ct: ClassTag[T]) extends Type[T] {
46-
override def toString: String = s"Type($ct)"
47-
}
48-
4979
/** An Type backed by a tree */
5080
final class TreeType[Tree](val typeTree: Tree) extends Type[Any] {
5181
override def toString: String = s"Type(<tasty tree>)"

tests/pos/typetags.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22

33
object Test {
44

5-
def f[T: Type] = {
5+
def f[T: Type] given QuoteContext = {
66
implicitly[Type[Int]]
77
implicitly[Type[List[Int]]]
88
implicitly[Type[T]]

tests/run-with-compiler/i3947b.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
scala.Predef.classOf[scala.Boolean].getCanonicalName()
3-
boolean
43

54
scala.Predef.classOf[scala.Byte].getCanonicalName()
6-
byte
75

86
scala.Predef.classOf[scala.Char].getCanonicalName()
7+
boolean
8+
byte
99
char

tests/run-with-compiler/i3947b.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import scala.quoted._
33

44
object Test {
55

6-
def main(args: Array[String]): Unit = {
7-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
def test[T: Type](clazz: java.lang.Class[T]): Unit = run {
6+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
7+
8+
def main(args: Array[String]): Unit = run {
9+
def test[T: Type](clazz: java.lang.Class[T]) = {
910
val lclazz = clazz.toExpr
1011
val name = '{ ($lclazz).getCanonicalName }
1112
println()
@@ -14,9 +15,11 @@ object Test {
1415
}
1516

1617
// primitives
17-
test(classOf[Boolean])
18-
test(classOf[Byte])
19-
test(classOf[Char])
18+
'{
19+
${test(classOf[Boolean])}
20+
${test(classOf[Byte])}
21+
${test(classOf[Char])}
22+
}
2023
}
2124

2225
}

tests/run-with-compiler/i3947b2.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
scala.Predef.classOf[scala.Short].getCanonicalName()
3-
short
43

54
scala.Predef.classOf[scala.Int].getCanonicalName()
6-
int
75

86
scala.Predef.classOf[scala.Long].getCanonicalName()
7+
short
8+
int
99
long

tests/run-with-compiler/i3947b2.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import scala.quoted._
33

44
object Test {
55

6-
def main(args: Array[String]): Unit = {
7-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
def test[T: Type](clazz: java.lang.Class[T]): Unit = run {
6+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
7+
8+
def main(args: Array[String]): Unit = run {
9+
def test[T: Type](clazz: given QuoteContext => java.lang.Class[T]) = {
910
val lclazz = clazz.toExpr
1011
val name = '{ ($lclazz).getCanonicalName }
1112
println()
@@ -14,9 +15,11 @@ object Test {
1415
}
1516

1617
// primitives
17-
test(classOf[Short])
18-
test(classOf[Int])
19-
test(classOf[Long])
18+
'{
19+
${test(classOf[Short])}
20+
${test(classOf[Int])}
21+
${test(classOf[Long])}
22+
}
2023
}
2124

2225
}

tests/run-with-compiler/i3947b3.check

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
21
scala.Predef.classOf[scala.Float].getCanonicalName()
3-
float
4-
52
scala.Predef.classOf[scala.Double].getCanonicalName()
6-
double
7-
83
scala.Predef.classOf[scala.Unit].getCanonicalName()
4+
float
5+
double
96
void

tests/run-with-compiler/i3947b3.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ import scala.quoted._
33

44
object Test {
55

6-
def main(args: Array[String]): Unit = {
7-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
def test[T: Type](clazz: java.lang.Class[T]): Unit = run {
6+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
7+
8+
def main(args: Array[String]): Unit = run {
9+
def test[T: Type](clazz: java.lang.Class[T]) = {
910
val lclazz = clazz.toExpr
1011
val name = '{ ($lclazz).getCanonicalName }
11-
println()
1212
println(name.show)
1313
'{ println($name) }
1414
}
1515

16-
// primitives
17-
test(classOf[Float])
18-
test(classOf[Double])
19-
test(classOf[Unit])
16+
'{
17+
${test(classOf[Float])}
18+
${test(classOf[Double])}
19+
${test(classOf[Unit])}
20+
}
2021
}
2122

2223
}

tests/run-with-compiler/i6263.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import quoted._
22
object Test {
33

4-
def main(args: Array[String]): Unit = {
4+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
5+
6+
def main(args: Array[String]): Unit = withQuoteContext {
57
fn("foo")
68
fn((1,2))
79
fn(O)

0 commit comments

Comments
 (0)