Skip to content

Commit 5e95030

Browse files
committed
Add new EXPLICITtpt to TASTy format
This is a new encoding of HOLE that differentiates between type and term arguments of the hole. ``` -- pickled quote trees: These trees can only appear in pickled quotes. They will never be in a TASTy file. EXPLICITtpt tpt_Term -- Tag for a type tree that in a context where it is not explicitly known that this tree is a type. HOLE Length idx_Nat tpe_Type arg_Tree* -- Splice hole with index `idx`, the type of the hole `tpe`, type and term arguments of the hole `arg`s ``` We will only have hole captured types if there is a type defined in a quote and used in a nested quote. Most of the time we do not have those types and therefore no overhead in the encoding compared to before this change.
1 parent 6980b2e commit 5e95030

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,10 @@ class TreePickler(pickler: TastyPickler) {
670670
withLength {
671671
writeNat(idx)
672672
pickleType(tpt.tpe, richTypes = true)
673-
args.foreach(pickleTree)
673+
args.foreach { arg =>
674+
if arg.isType then writeByte(EXPLICITtpt)
675+
pickleTree(arg)
676+
}
674677
}
675678
}
676679
catch {

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,8 @@ class TreeUnpickler(reader: TastyReader,
12331233
ByNameTypeTree(if knowsPureFuns then arg else arg.adaptByNameArgUnderPureFuns)
12341234
case NAMEDARG =>
12351235
NamedArg(readName(), readTerm())
1236+
case EXPLICITtpt =>
1237+
readTpt()
12361238
case _ =>
12371239
readPathTerm()
12381240
}
@@ -1436,10 +1438,7 @@ class TreeUnpickler(reader: TastyReader,
14361438
val alias = if currentAddr == end then EmptyTree else readTpt()
14371439
createNullableTypeBoundsTree(lo, hi, alias)
14381440
case HOLE =>
1439-
val idx = readNat()
1440-
val tpe = readType()
1441-
val args = until(end)(readTerm())
1442-
Hole(true, idx, args, EmptyTree, TypeTree(tpe)).withType(tpe)
1441+
readHole(end, isTerm = true)
14431442
case _ =>
14441443
readPathTerm()
14451444
}
@@ -1470,10 +1469,7 @@ class TreeUnpickler(reader: TastyReader,
14701469
case HOLE =>
14711470
readByte()
14721471
val end = readEnd()
1473-
val idx = readNat()
1474-
val tpe = readType()
1475-
val args = until(end)(readTerm())
1476-
Hole(false, idx, args, EmptyTree, TypeTree(tpe)).withType(tpe)
1472+
readHole(end, isTerm = false)
14771473
case _ =>
14781474
if (isTypeTreeTag(nextByte)) readTerm()
14791475
else {
@@ -1506,6 +1502,12 @@ class TreeUnpickler(reader: TastyReader,
15061502
setSpan(start, CaseDef(pat, guard, rhs))
15071503
}
15081504

1505+
def readHole(end: Addr, isTerm: Boolean)(using Context): Tree =
1506+
val idx = readNat()
1507+
val tpe = readType()
1508+
val args = until(end)(readTerm())
1509+
Hole(isTerm, idx, args, EmptyTree, TypeTree(tpe)).withType(tpe)
1510+
15091511
def readLater[T <: AnyRef](end: Addr, op: TreeReader => Context ?=> T)(using Context): Trees.Lazy[T] =
15101512
readLaterWithOwner(end, op)(ctx.owner)
15111513

tasty/src/dotty/tools/tasty/TastyFormat.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Standard-Section: "ASTs" TopLevelStat*
122122
MATCHtpt Length bound_Term? sel_Term CaseDef* -- sel match { CaseDef } where `bound` is optional upper bound of all rhs
123123
BYNAMEtpt underlying_Term -- => underlying
124124
SHAREDterm term_ASTRef -- Link to previously serialized term
125+
-- pickled quote trees: -- These trees can only appear in pickled quotes. They will never be in a TASTy file.
126+
EXPLICITtpt tpt_Term -- Tag for a type tree that in a context where it is not explicitly known that this tree is a type.
125127
HOLE Length idx_Nat tpe_Type arg_Tree* -- Splice hole with index `idx`, the type of the hole `tpe`, type and term arguments of the hole `arg`s
126128
127129
@@ -511,6 +513,8 @@ object TastyFormat {
511513
final val RECtype = 100
512514
final val SINGLETONtpt = 101
513515
final val BOUNDED = 102
516+
final val EXPLICITtpt = 103
517+
514518

515519
// Cat. 4: tag Nat AST
516520

@@ -659,6 +663,7 @@ object TastyFormat {
659663
| ANNOTATEDtpt
660664
| BYNAMEtpt
661665
| MATCHtpt
666+
| EXPLICITtpt
662667
| BIND => true
663668
case _ => false
664669
}
@@ -803,6 +808,7 @@ object TastyFormat {
803808
case ANNOTATION => "ANNOTATION"
804809
case PRIVATEqualified => "PRIVATEqualified"
805810
case PROTECTEDqualified => "PROTECTEDqualified"
811+
case EXPLICITtpt => "EXPLICITtpt"
806812
case HOLE => "HOLE"
807813
}
808814

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted.*
2+
3+
inline def foo[U](u: U): U = ${ fooImpl[U]('u) }
4+
5+
def fooImpl[U: Type](u: Expr[U])(using Quotes): Expr[U] = '{
6+
def f[T](x: T): T = ${ identity('{ x: T }) }
7+
f[U]($u)
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def test =
2+
foo(1)
3+
foo("abc")

0 commit comments

Comments
 (0)