Skip to content

Commit 965818a

Browse files
Add new EXPLICITtpt to TASTy format (#17298)
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 pickle type trees in `arg_Tree` using the `EXPLICITtpt` tag. 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. Alternative to #17225 Fixes #17137
2 parents 5759bb8 + 46a8d13 commit 965818a

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,12 @@ class TreePickler(pickler: TastyPickler) {
694694
withLength {
695695
writeNat(idx)
696696
pickleType(tree.tpe, richTypes = true)
697-
args.foreach(pickleTree)
697+
args.foreach { arg =>
698+
arg.tpe match
699+
case _: TermRef if arg.isType => writeByte(EXPLICITtpt)
700+
case _ =>
701+
pickleTree(arg)
702+
}
698703
}
699704
}
700705
catch {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,8 @@ class TreeUnpickler(reader: TastyReader,
12431243
ByNameTypeTree(if withPureFuns then arg else arg.adaptByNameArgUnderPureFuns)
12441244
case NAMEDARG =>
12451245
NamedArg(readName(), readTree())
1246+
case EXPLICITtpt =>
1247+
readTpt()
12461248
case _ =>
12471249
readPathTree()
12481250
}
@@ -1468,10 +1470,7 @@ class TreeUnpickler(reader: TastyReader,
14681470
val alias = if currentAddr == end then EmptyTree else readTpt()
14691471
createNullableTypeBoundsTree(lo, hi, alias)
14701472
case HOLE =>
1471-
val idx = readNat()
1472-
val tpe = readType()
1473-
val args = until(end)(readTree())
1474-
Hole(true, idx, args, EmptyTree, tpe)
1473+
readHole(end, isTerm = true)
14751474
case _ =>
14761475
readPathTree()
14771476
}
@@ -1502,10 +1501,7 @@ class TreeUnpickler(reader: TastyReader,
15021501
case HOLE =>
15031502
readByte()
15041503
val end = readEnd()
1505-
val idx = readNat()
1506-
val tpe = readType()
1507-
val args = until(end)(readTree())
1508-
Hole(false, idx, args, EmptyTree, tpe)
1504+
readHole(end, isTerm = false)
15091505
case _ =>
15101506
if (isTypeTreeTag(nextByte)) readTree()
15111507
else {
@@ -1538,6 +1534,12 @@ class TreeUnpickler(reader: TastyReader,
15381534
setSpan(start, CaseDef(pat, guard, rhs))
15391535
}
15401536

1537+
def readHole(end: Addr, isTerm: Boolean)(using Context): Tree =
1538+
val idx = readNat()
1539+
val tpe = readType()
1540+
val args = until(end)(readTree())
1541+
Hole(isTerm, idx, args, EmptyTree, tpe)
1542+
15411543
def readLater[T <: AnyRef](end: Addr, op: TreeReader => Context ?=> T)(using Context): Trees.Lazy[T] =
15421544
readLaterWithOwner(end, op)(ctx.owner)
15431545

project/MiMaFilters.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ object MiMaFilters {
99
// New API in 3.4.X
1010
)
1111
val TastyCore: Seq[ProblemFilter] = Seq(
12+
ProblemFilters.exclude[DirectMissingMethodProblem]("dotty.tools.tasty.TastyFormat.EXPLICITtpt"),
1213
)
1314
val Interfaces: Seq[ProblemFilter] = Seq(
1415
)

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)