Skip to content

Commit 6d02874

Browse files
committed
Extract QuoteTypeTags
1 parent 9cbc14c commit 6d02874

File tree

5 files changed

+59
-44
lines changed

5 files changed

+59
-44
lines changed

compiler/src/dotty/tools/dotc/staging/HealType.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dotty.tools.dotc.core.Symbols._
88
import dotty.tools.dotc.core.Types._
99
import dotty.tools.dotc.staging.StagingContext.*
1010
import dotty.tools.dotc.staging.StagingLevel.*
11+
import dotty.tools.dotc.staging.QuoteTypeTags.*
1112
import dotty.tools.dotc.transform.SymUtils._
1213
import dotty.tools.dotc.typer.Implicits.SearchFailureType
1314
import dotty.tools.dotc.util.SrcPos

compiler/src/dotty/tools/dotc/staging/PCPCheckAndHeal.scala

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import dotty.tools.dotc.core.Symbols._
1212
import dotty.tools.dotc.core.Types._
1313
import dotty.tools.dotc.staging.StagingContext.*
1414
import dotty.tools.dotc.staging.StagingLevel.*
15+
import dotty.tools.dotc.staging.QuoteTypeTags.*
1516
import dotty.tools.dotc.util.Property
1617
import dotty.tools.dotc.util.Spans._
1718
import dotty.tools.dotc.util.SrcPos
@@ -98,7 +99,7 @@ class PCPCheckAndHeal extends TreeMapWithStages {
9899

99100
/** Transform quoted trees while maintaining phase correctness */
100101
override protected def transformQuotation(body: Tree, quote: Apply)(using Context): Tree = {
101-
val taggedTypes = new PCPCheckAndHeal.QuoteTypeTags(quote.span)
102+
val taggedTypes = new QuoteTypeTags(quote.span)
102103

103104
if (ctx.property(InAnnotation).isDefined)
104105
report.error("Cannot have a quote in an annotation", quote.srcPos)
@@ -236,35 +237,3 @@ class PCPCheckAndHeal extends TreeMapWithStages {
236237
tp
237238
}
238239
}
239-
240-
object PCPCheckAndHeal {
241-
import tpd._
242-
243-
class QuoteTypeTags(span: Span)(using Context) {
244-
245-
private val tags = collection.mutable.LinkedHashMap.empty[Symbol, TypeDef]
246-
247-
def getTagRef(spliced: TermRef): TypeRef = {
248-
val typeDef = tags.getOrElseUpdate(spliced.symbol, mkTagSymbolAndAssignType(spliced))
249-
typeDef.symbol.typeRef
250-
}
251-
252-
def getTypeTags: List[TypeDef] = tags.valuesIterator.toList
253-
254-
private def mkTagSymbolAndAssignType(spliced: TermRef): TypeDef = {
255-
val splicedTree = tpd.ref(spliced).withSpan(span)
256-
val rhs = splicedTree.select(tpnme.Underlying).withSpan(span)
257-
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs, EmptyTree)
258-
val local = newSymbol(
259-
owner = ctx.owner,
260-
name = UniqueName.fresh((splicedTree.symbol.name.toString + "$_").toTermName).toTypeName,
261-
flags = Synthetic,
262-
info = TypeAlias(splicedTree.tpe.select(tpnme.Underlying)),
263-
coord = span).asType
264-
local.addAnnotation(Annotation(defn.QuotedRuntime_SplicedTypeAnnot, span))
265-
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local)
266-
}
267-
268-
}
269-
270-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dotty.tools.dotc.staging
2+
3+
import dotty.tools.dotc.ast.{tpd, untpd}
4+
import dotty.tools.dotc.core.Annotations._
5+
import dotty.tools.dotc.core.Contexts._
6+
import dotty.tools.dotc.core.Decorators._
7+
import dotty.tools.dotc.core.Flags._
8+
import dotty.tools.dotc.core.NameKinds._
9+
import dotty.tools.dotc.core.StdNames._
10+
import dotty.tools.dotc.core.Symbols._
11+
import dotty.tools.dotc.core.Types._
12+
import dotty.tools.dotc.staging.StagingLevel.*
13+
import dotty.tools.dotc.util.Property
14+
import dotty.tools.dotc.util.Spans._
15+
16+
object QuoteTypeTags {
17+
18+
private val TaggedTypes = new Property.Key[QuoteTypeTags]
19+
20+
def contextWithQuoteTypeTags(taggedTypes: QuoteTypeTags)(using Context) =
21+
ctx.fresh.setProperty(TaggedTypes, taggedTypes)
22+
23+
def getQuoteTypeTags(using Context): QuoteTypeTags =
24+
ctx.property(TaggedTypes).get
25+
}
26+
27+
class QuoteTypeTags(span: Span)(using Context) {
28+
import tpd.*
29+
30+
private val tags = collection.mutable.LinkedHashMap.empty[Symbol, TypeDef]
31+
32+
def getTagRef(spliced: TermRef): TypeRef = {
33+
val typeDef = tags.getOrElseUpdate(spliced.symbol, mkTagSymbolAndAssignType(spliced))
34+
typeDef.symbol.typeRef
35+
}
36+
37+
def getTypeTags: List[TypeDef] = tags.valuesIterator.toList
38+
39+
private def mkTagSymbolAndAssignType(spliced: TermRef): TypeDef = {
40+
val splicedTree = tpd.ref(spliced).withSpan(span)
41+
val rhs = splicedTree.select(tpnme.Underlying).withSpan(span)
42+
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs, EmptyTree)
43+
val local = newSymbol(
44+
owner = ctx.owner,
45+
name = UniqueName.fresh((splicedTree.symbol.name.toString + "$_").toTermName).toTypeName,
46+
flags = Synthetic,
47+
info = TypeAlias(splicedTree.tpe.select(tpnme.Underlying)),
48+
coord = span).asType
49+
local.addAnnotation(Annotation(defn.QuotedRuntime_SplicedTypeAnnot, span))
50+
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local)
51+
}
52+
}

compiler/src/dotty/tools/dotc/staging/StagingContext.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@ object StagingContext {
1414
*/
1515
private val QuotesStack = new Property.Key[List[tpd.Tree]]
1616

17-
private val TaggedTypes = new Property.Key[PCPCheckAndHeal.QuoteTypeTags]
18-
1917
/** Context with an incremented quotation level and pushes a reference to a Quotes on the quote context stack */
2018
def pushQuotes(qctxRef: tpd.Tree)(using Context): Context =
2119
val old = ctx.property(QuotesStack).getOrElse(List.empty)
2220
quoteContext.setProperty(QuotesStack, qctxRef :: old)
2321

24-
def contextWithQuoteTypeTags(taggedTypes: PCPCheckAndHeal.QuoteTypeTags)(using Context) =
25-
ctx.fresh.setProperty(TaggedTypes, taggedTypes)
26-
27-
def getQuoteTypeTags(using Context): PCPCheckAndHeal.QuoteTypeTags =
28-
ctx.property(TaggedTypes).get
29-
3022
/** Context with a decremented quotation level and pops the Some of top of the quote context stack or None if the stack is empty.
3123
* The quotation stack could be empty if we are in a top level splice or an erroneous splice directly within a top level splice.
3224
*/

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import dotty.tools.dotc.config.ScalaRelease.*
2424
import dotty.tools.dotc.staging.PCPCheckAndHeal
2525
import dotty.tools.dotc.staging.StagingContext.*
2626
import dotty.tools.dotc.staging.StagingLevel.*
27+
import dotty.tools.dotc.staging.QuoteTypeTags
2728

2829
import scala.annotation.constructorOnly
2930

@@ -191,7 +192,7 @@ class Splicing extends MacroTransform:
191192
private var refBindingMap = mutable.Map.empty[Symbol, (Tree, Symbol)]
192193
/** Reference to the `Quotes` instance of the current level 1 splice */
193194
private var quotes: Tree | Null = null // TODO: add to the context
194-
private var healedTypes: PCPCheckAndHeal.QuoteTypeTags | Null = null // TODO: add to the context
195+
private var healedTypes: QuoteTypeTags | Null = null // TODO: add to the context
195196

196197
def transformSplice(tree: tpd.Tree, tpe: Type, holeIdx: Int)(using Context): tpd.Tree =
197198
assert(level == 0)
@@ -261,7 +262,7 @@ class Splicing extends MacroTransform:
261262
private def transformLevel0QuoteContent(tree: Tree)(using Context): Tree =
262263
// transform and collect new healed types
263264
val old = healedTypes
264-
healedTypes = new PCPCheckAndHeal.QuoteTypeTags(tree.span)
265+
healedTypes = new QuoteTypeTags(tree.span)
265266
val tree1 = transform(tree)
266267
val newHealedTypes = healedTypes.nn.getTypeTags
267268
healedTypes = old
@@ -359,7 +360,7 @@ class Splicing extends MacroTransform:
359360

360361
private def capturedPartTypes(tpt: Tree)(using Context): Tree =
361362
val old = healedTypes
362-
healedTypes = PCPCheckAndHeal.QuoteTypeTags(tpt.span)
363+
healedTypes = QuoteTypeTags(tpt.span)
363364
val capturePartTypes = new TypeMap {
364365
def apply(tp: Type) = tp match {
365366
case typeRef @ TypeRef(prefix, _) if isCaptured(prefix.typeSymbol) || isCaptured(prefix.termSymbol) =>

0 commit comments

Comments
 (0)