Skip to content

Commit 86a92e4

Browse files
Merge pull request #9448 from dotty-staging/move-staging-phase
Remove quoted.Type synthesization
2 parents 63b84cb + 7c4fa0c commit 86a92e4

File tree

17 files changed

+30
-59
lines changed

17 files changed

+30
-59
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class Compiler {
3838
protected def frontendPhases: List[List[Phase]] =
3939
List(new FrontEnd) :: // Compiler frontend: scanner, parser, namer, typer
4040
List(new YCheckPositions) :: // YCheck positions
41-
List(new Staging) :: // Check PCP, heal quoted types and expand macros
4241
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
4342
List(new semanticdb.ExtractSemanticDB) :: // Extract info into .semanticdb files
4443
List(new PostTyper) :: // Additional checks and cleanups after type checking
44+
List(new Staging) :: // Check PCP, heal quoted types and expand macros
4545
List(new sbt.ExtractAPI) :: // Sends a representation of the API of classes to sbt via callbacks
4646
List(new SetRootTree) :: // Set the `rootTreeOrProvider` on class symbols
4747
Nil

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
397397
case New(_) | Closure(_, _, _) =>
398398
Pure
399399
case TypeApply(fn, _) =>
400-
if (fn.symbol.is(Erased) || fn.symbol == defn.InternalQuoted_typeQuote || fn.symbol == defn.Predef_classOf) Pure else exprPurity(fn)
400+
if (fn.symbol.is(Erased) || fn.symbol == defn.QuotedTypeModule_apply || fn.symbol == defn.Predef_classOf) Pure else exprPurity(fn)
401401
case Apply(fn, args) =>
402402
def isKnownPureOp(sym: Symbol) =
403403
sym.owner.isPrimitiveValueClass

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,6 @@ class Definitions {
699699
@tu lazy val InternalQuoted_exprQuote : Symbol = InternalQuotedModule.requiredMethod("exprQuote")
700700
@tu lazy val InternalQuoted_exprSplice : Symbol = InternalQuotedModule.requiredMethod("exprSplice")
701701
@tu lazy val InternalQuoted_exprNestedSplice : Symbol = InternalQuotedModule.requiredMethod("exprNestedSplice")
702-
@tu lazy val InternalQuoted_typeQuote : Symbol = InternalQuotedModule.requiredMethod("typeQuote")
703702
@tu lazy val InternalQuoted_QuoteTypeTagAnnot: ClassSymbol = InternalQuotedModule.requiredClass("quoteTypeTag")
704703

705704
@tu lazy val InternalQuotedMatcher: Symbol = requiredModule("scala.internal.quoted.Matcher")
@@ -719,6 +718,7 @@ class Definitions {
719718
@tu lazy val QuotedType_splice: Symbol = QuotedTypeClass.requiredType(tpnme.spliceType)
720719

721720
@tu lazy val QuotedTypeModule: Symbol = QuotedTypeClass.companionModule
721+
@tu lazy val QuotedTypeModule_apply: Symbol = QuotedTypeModule.requiredMethod("apply")
722722

723723
@tu lazy val TastyReflectionClass: ClassSymbol = requiredClass("scala.tasty.Reflection")
724724

compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,5 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
7575

7676
override protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text =
7777
if (tree.symbol eq defn.InternalQuoted_exprQuote) "'"
78-
else if (tree.symbol eq defn.InternalQuoted_typeQuote) "'[" ~ toTextGlobal(tree.args, ", ") ~ "]"
7978
else super.typeApplyText(tree)
8079
}

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
266266
("{" ~ toText(trees, "\n") ~ "}").close
267267

268268
protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = {
269-
val isQuote = !printDebug && tree.fun.hasType && tree.fun.symbol == defn.InternalQuoted_typeQuote
270-
val (open, close) = if (isQuote) (keywordStr("'["), keywordStr("]")) else ("[", "]")
271-
val funText = toTextLocal(tree.fun).provided(!isQuote)
269+
val funText = toTextLocal(tree.fun)
272270
tree.fun match {
273271
case Select(New(tpt), nme.CONSTRUCTOR) if tpt.typeOpt.dealias.isInstanceOf[AppliedType] =>
274272
funText // type was already printed by toText(new)
275273
case _ =>
276-
funText ~ open ~ toTextGlobal(tree.args, ", ") ~ close
274+
funText ~ "[" ~ toTextGlobal(tree.args, ", ") ~ "]"
277275
}
278276
}
279277

@@ -632,9 +630,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
632630
}
633631
case Number(digits, kind) =>
634632
digits
635-
case Quote(tree) =>
636-
if (tree.isType) keywordStr("'[") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("]")
637-
else keywordStr("'{") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
633+
case Quote(tree) if tree.isTerm =>
634+
keywordStr("'{") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
638635
case Splice(tree) =>
639636
keywordStr("${") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
640637
case TypSplice(tree) =>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
175175
tp match
176176
case tp: TypeRef =>
177177
tp.prefix match
178-
case NoPrefix if level > levelOf(tp.symbol) =>
179-
tryHeal(tp.symbol, tp, pos)
178+
case NoPrefix if level > levelOf(tp.symbol) && !tp.typeSymbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot) =>
179+
val tp1 = tp.dealias
180+
if tp1 != tp then apply(tp1)
181+
else tryHeal(tp.symbol, tp, pos)
180182
case prefix: ThisType if !tp.symbol.isStatic && level > levelOf(prefix.cls) =>
181183
tryHeal(tp.symbol, tp, pos)
182184
case prefix: TermRef if tp.symbol.isTypeSplice =>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
273273
val patterns1 = transform(patterns)
274274
cpy.UnApply(tree)(transform(fun), transform(implicits), patterns1)
275275
case tree: TypeApply =>
276+
if tree.symbol.isQuote then
277+
ctx.compilationUnit.needsStaging = true
276278
val tree1 @ TypeApply(fn, args) = normalizeTypeArgs(tree)
277279
args.foreach(checkInferredWellFormed)
278280
if (fn.symbol != defn.ChildAnnot.primaryConstructor)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class ReifyQuotes extends MacroTransform {
127127
* core and splices as arguments.
128128
*/
129129
override protected def transformQuotation(body: Tree, quote: Tree)(using Context): Tree = {
130-
val isType = quote.symbol eq defn.InternalQuoted_typeQuote
130+
val isType = quote.symbol eq defn.QuotedTypeModule_apply
131131
if (level > 0) {
132132
val body1 = nested(isQuote = true).transform(body)(using quoteContext)
133133
super.transformQuotation(body1, quote)
@@ -364,7 +364,7 @@ class ReifyQuotes extends MacroTransform {
364364
transform(tree)(using ctx.withSource(tree.source))
365365
else reporting.trace(i"Reifier.transform $tree at $level", show = true) {
366366
tree match {
367-
case Apply(Select(TypeApply(fn, (body: RefTree) :: Nil), _), _) if fn.symbol == defn.InternalQuoted_typeQuote && isCaptured(body.symbol, level + 1) =>
367+
case Apply(Select(TypeApply(fn, (body: RefTree) :: Nil), _), _) if fn.symbol == defn.QuotedTypeModule_apply && isCaptured(body.symbol, level + 1) =>
368368
// Optimization: avoid the full conversion when capturing `x`
369369
// in '{ x } to '{ ${x$1} } and go directly to `x$1`
370370
capturers(body.symbol)(body)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ object Splicer {
141141
case Apply(Select(Apply(fn, quoted :: Nil), nme.apply), _) if fn.symbol == defn.InternalQuoted_exprQuote =>
142142
// OK
143143

144-
case TypeApply(fn, quoted :: Nil) if fn.symbol == defn.InternalQuoted_typeQuote =>
144+
case TypeApply(fn, quoted :: Nil) if fn.symbol == defn.QuotedTypeModule_apply =>
145145
// OK
146146

147147
case Literal(Constant(value)) =>
@@ -231,7 +231,7 @@ object Splicer {
231231
}
232232
interpretQuote(quoted1)
233233

234-
case Apply(Select(TypeApply(fn, quoted :: Nil), _), _) if fn.symbol == defn.InternalQuoted_typeQuote =>
234+
case Apply(Select(TypeApply(fn, quoted :: Nil), _), _) if fn.symbol == defn.QuotedTypeModule_apply =>
235235
interpretTypeQuote(quoted)
236236

237237
case Literal(Constant(value)) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ object SymUtils {
203203

204204
/** Is symbol a quote operation? */
205205
def isQuote(using Context): Boolean =
206-
self == defn.InternalQuoted_exprQuote || self == defn.InternalQuoted_typeQuote
206+
self == defn.InternalQuoted_exprQuote || self == defn.QuotedTypeModule_apply
207207

208208
/** Is symbol a term splice operation? */
209209
def isExprSplice(using Context): Boolean =

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
528528
case New(_) | Closure(_, _, _) =>
529529
true
530530
case TypeApply(fn, _) =>
531-
if (fn.symbol.is(Erased) || fn.symbol == defn.InternalQuoted_typeQuote) true else apply(fn)
531+
if (fn.symbol.is(Erased) || fn.symbol == defn.QuotedTypeModule_apply) true else apply(fn)
532532
case Apply(fn, args) =>
533533
def isKnownPureOp(sym: Symbol) =
534534
sym.owner.isPrimitiveValueClass
@@ -1192,7 +1192,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
11921192
}
11931193

11941194
override def typedIdent(tree: untpd.Ident, pt: Type)(using Context): Tree =
1195-
checkStaging(tryInline(tree.asInstanceOf[tpd.Tree]) `orElse` super.typedIdent(tree, pt))
1195+
tryInline(tree.asInstanceOf[tpd.Tree]) `orElse` super.typedIdent(tree, pt)
11961196

11971197
override def typedSelect(tree: untpd.Select, pt: Type)(using Context): Tree = {
11981198
assert(tree.hasType, tree)
@@ -1204,14 +1204,9 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
12041204
else
12051205
val res = resMaybeReduced
12061206
ensureAccessible(res.tpe, tree.qualifier.isInstanceOf[untpd.Super], tree.sourcePos)
1207-
checkStaging(res)
1207+
res
12081208
}
12091209

1210-
private def checkStaging(tree: Tree): tree.type =
1211-
if tree.symbol.isQuote then
1212-
ctx.compilationUnit.needsStaging = true
1213-
tree
1214-
12151210
override def typedIf(tree: untpd.If, pt: Type)(using Context): Tree =
12161211
typed(tree.cond, defn.BooleanType) match {
12171212
case cond1 @ ConstantValue(b: Boolean) =>

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ trait QuotesAndSplices {
4040
*/
4141
def typedQuote(tree: untpd.Quote, pt: Type)(using Context): Tree = {
4242
record("typedQuote")
43-
ctx.compilationUnit.needsStaging = true
4443
tree.quoted match {
4544
case untpd.Splice(innerExpr) if tree.isTerm =>
4645
report.warning("Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.", tree.sourcePos)
@@ -59,7 +58,7 @@ trait QuotesAndSplices {
5958
if ctx.mode.is(Mode.Pattern) then
6059
typedQuotePattern(tree, pt, qctx)
6160
else if (tree.quoted.isType)
62-
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuote.termRef), tree.quoted :: Nil), pt)(using quoteContext).select(nme.apply).appliedTo(qctx)
61+
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedTypeModule_apply.termRef), tree.quoted :: Nil), pt)(using quoteContext).select(nme.apply).appliedTo(qctx)
6362
else
6463
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprQuote.termRef), tree.quoted), pt)(using pushQuoteContext(qctx)).select(nme.apply).appliedTo(qctx)
6564
tree1.withSpan(tree.span)
@@ -68,7 +67,6 @@ trait QuotesAndSplices {
6867
/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
6968
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {
7069
record("typedSplice")
71-
ctx.compilationUnit.needsStaging = true
7270
checkSpliceOutsideQuote(tree)
7371
tree.expr match {
7472
case untpd.Quote(innerExpr) if innerExpr.isTerm =>
@@ -147,7 +145,6 @@ trait QuotesAndSplices {
147145
/** Translate ${ t: Type[T] }` into type `t.splice` while tracking the quotation level in the context */
148146
def typedTypSplice(tree: untpd.TypSplice, pt: Type)(using Context): Tree = {
149147
record("typedTypSplice")
150-
ctx.compilationUnit.needsStaging = true
151148
checkSpliceOutsideQuote(tree)
152149
tree.expr match {
153150
case untpd.Quote(innerType) if innerType.isType =>
@@ -442,7 +439,7 @@ trait QuotesAndSplices {
442439
val quoteClass = if (tree.quoted.isTerm) defn.QuotedExprClass else defn.QuotedTypeClass
443440
val quotedPattern =
444441
if (tree.quoted.isTerm) ref(defn.InternalQuoted_exprQuote.termRef).appliedToType(defn.AnyType).appliedTo(shape).select(nme.apply).appliedTo(qctx)
445-
else ref(defn.InternalQuoted_typeQuote.termRef).appliedToTypeTree(shape).select(nme.apply).appliedTo(qctx)
442+
else ref(defn.QuotedTypeModule_apply.termRef).appliedToTypeTree(shape).select(nme.apply).appliedTo(qctx)
446443
UnApply(
447444
fun = ref(unapplySym.termRef).appliedToTypeTrees(typeBindingsTuple :: TypeTree(patType) :: Nil),
448445
implicits = quotedPattern :: Literal(Constant(typeBindings.nonEmpty)) :: qctx :: Nil,

compiler/src/dotty/tools/dotc/typer/Synthesizer.scala

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,6 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
4545
case _ => EmptyTree
4646
end synthesizedClassTag
4747

48-
/** Synthesize the tree for `'[T]` for an implicit `scala.quoted.Type[T]`.
49-
* `T` is deeply dealiased to avoid references to local type aliases.
50-
*/
51-
val synthesizedTypeTag: SpecialHandler = (formal, span) =>
52-
def quotedType(t: Type) =
53-
if StagingContext.level == 0 then
54-
ctx.compilationUnit.needsStaging = true // We will need to run ReifyQuotes
55-
val qctx = ctx.typer.inferImplicitArg(defn.QuoteContextClass.typeRef, span)
56-
qctx.tpe match
57-
case tpe: Implicits.SearchFailureType => report.error(tpe.msg, ctx.source.atSpan(span))
58-
case _ =>
59-
ref(defn.InternalQuoted_typeQuote).appliedToType(t).select(nme.apply).appliedTo(qctx)
60-
formal.argInfos match
61-
case arg :: Nil =>
62-
val deepDealias = new TypeMap:
63-
def apply(tp: Type): Type = mapOver(tp.dealias)
64-
quotedType(deepDealias(arg))
65-
case _ =>
66-
EmptyTree
67-
end synthesizedTypeTag
68-
6948
val synthesizedTupleFunction: SpecialHandler = (formal, span) =>
7049
formal match
7150
case AppliedType(_, funArgs @ fun :: tupled :: Nil) =>
@@ -395,7 +374,6 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
395374

396375
val specialHandlers = List(
397376
defn.ClassTagClass -> synthesizedClassTag,
398-
defn.QuotedTypeClass -> synthesizedTypeTag,
399377
defn.EqlClass -> synthesizedEql,
400378
defn.TupledFunctionClass -> synthesizedTupleFunction,
401379
defn.ValueOfClass -> synthesizedValueOf,

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ object CompileTime {
2020
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.exprNestedSplice`")
2121
def exprNestedSplice[T](ctx: QuoteContext)(x: ctx.Nested ?=> Expr[T]): T = ???
2222

23-
/** A type quote is desugared by the compiler into a call to this method */
24-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.typeQuote`")
25-
def typeQuote[T <: AnyKind]: QuoteContext ?=> Type[T] = ???
26-
2723
/** Artifact of pickled type splices
2824
*
2925
* During quote reification a quote `'{ ... F[$t] ... }` will be transformed into

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.quoted
22

3+
import scala.annotation.compileTimeOnly
34
import scala.quoted.show.SyntaxHighlight
45

56
/** Quoted type (or kind) `T` */
@@ -22,6 +23,10 @@ abstract class Type[X <: AnyKind] private[scala] {
2223
/** Some basic type tags, currently incomplete */
2324
object Type {
2425

26+
/** Return a quoted.Type with the given type */
27+
@compileTimeOnly("Reference to `scala.quoted.Type.apply` was not handled by ReifyQuotes")
28+
given apply[T <: AnyKind] as (QuoteContext ?=> Type[T]) = ???
29+
2530
def UnitTag: QuoteContext ?=> Type[Unit] =
2631
qctx.tasty.defn.UnitType.seal.asInstanceOf[quoted.Type[Unit]]
2732

tests/run-macros/i7048/Lib_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ given [U] as IsExpr[Expr[U]] = new IsExpr[Expr[U]] {
1212

1313
def f(x: Any): String = x.toString
1414

15-
def g[T](x: T)(using e: IsExpr[T], tu: Type[e.Underlying]): QuoteContext ?=> Expr[String] = {
15+
def g[T](x: T)(using e: IsExpr[T])(using tu: Type[e.Underlying]): QuoteContext ?=> Expr[String] = {
1616
val underlying: Expr[e.Underlying] = e.toExpr(x)
1717
'{f($underlying)}
1818
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
((qctx: scala.quoted.QuoteContext) ?=> {
2-
val t: scala.quoted.Type[scala.Predef.String] = scala.internal.quoted.CompileTime.typeQuote[scala.Predef.String].apply(using qctx)
2+
val t: scala.quoted.Type[scala.Predef.String] = scala.quoted.Type.apply[scala.Predef.String].apply(using qctx)
33

44
(t: scala.quoted.Type[scala.Predef.String])
55
})

0 commit comments

Comments
 (0)