Skip to content

Commit

Permalink
Add new EXPLICITtpt to TASTy format
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicolasstucki committed Apr 18, 2023
1 parent 6980b2e commit 5e95030
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,10 @@ class TreePickler(pickler: TastyPickler) {
withLength {
writeNat(idx)
pickleType(tpt.tpe, richTypes = true)
args.foreach(pickleTree)
args.foreach { arg =>
if arg.isType then writeByte(EXPLICITtpt)
pickleTree(arg)
}
}
}
catch {
Expand Down
18 changes: 10 additions & 8 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ class TreeUnpickler(reader: TastyReader,
ByNameTypeTree(if knowsPureFuns then arg else arg.adaptByNameArgUnderPureFuns)
case NAMEDARG =>
NamedArg(readName(), readTerm())
case EXPLICITtpt =>
readTpt()
case _ =>
readPathTerm()
}
Expand Down Expand Up @@ -1436,10 +1438,7 @@ class TreeUnpickler(reader: TastyReader,
val alias = if currentAddr == end then EmptyTree else readTpt()
createNullableTypeBoundsTree(lo, hi, alias)
case HOLE =>
val idx = readNat()
val tpe = readType()
val args = until(end)(readTerm())
Hole(true, idx, args, EmptyTree, TypeTree(tpe)).withType(tpe)
readHole(end, isTerm = true)
case _ =>
readPathTerm()
}
Expand Down Expand Up @@ -1470,10 +1469,7 @@ class TreeUnpickler(reader: TastyReader,
case HOLE =>
readByte()
val end = readEnd()
val idx = readNat()
val tpe = readType()
val args = until(end)(readTerm())
Hole(false, idx, args, EmptyTree, TypeTree(tpe)).withType(tpe)
readHole(end, isTerm = false)
case _ =>
if (isTypeTreeTag(nextByte)) readTerm()
else {
Expand Down Expand Up @@ -1506,6 +1502,12 @@ class TreeUnpickler(reader: TastyReader,
setSpan(start, CaseDef(pat, guard, rhs))
}

def readHole(end: Addr, isTerm: Boolean)(using Context): Tree =
val idx = readNat()
val tpe = readType()
val args = until(end)(readTerm())
Hole(isTerm, idx, args, EmptyTree, TypeTree(tpe)).withType(tpe)

def readLater[T <: AnyRef](end: Addr, op: TreeReader => Context ?=> T)(using Context): Trees.Lazy[T] =
readLaterWithOwner(end, op)(ctx.owner)

Expand Down
6 changes: 6 additions & 0 deletions tasty/src/dotty/tools/tasty/TastyFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Standard-Section: "ASTs" TopLevelStat*
MATCHtpt Length bound_Term? sel_Term CaseDef* -- sel match { CaseDef } where `bound` is optional upper bound of all rhs
BYNAMEtpt underlying_Term -- => underlying
SHAREDterm term_ASTRef -- Link to previously serialized term
-- 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
Expand Down Expand Up @@ -511,6 +513,8 @@ object TastyFormat {
final val RECtype = 100
final val SINGLETONtpt = 101
final val BOUNDED = 102
final val EXPLICITtpt = 103


// Cat. 4: tag Nat AST

Expand Down Expand Up @@ -659,6 +663,7 @@ object TastyFormat {
| ANNOTATEDtpt
| BYNAMEtpt
| MATCHtpt
| EXPLICITtpt
| BIND => true
case _ => false
}
Expand Down Expand Up @@ -803,6 +808,7 @@ object TastyFormat {
case ANNOTATION => "ANNOTATION"
case PRIVATEqualified => "PRIVATEqualified"
case PROTECTEDqualified => "PROTECTEDqualified"
case EXPLICITtpt => "EXPLICITtpt"
case HOLE => "HOLE"
}

Expand Down
8 changes: 8 additions & 0 deletions tests/pos-macros/captured-type/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted.*

inline def foo[U](u: U): U = ${ fooImpl[U]('u) }

def fooImpl[U: Type](u: Expr[U])(using Quotes): Expr[U] = '{
def f[T](x: T): T = ${ identity('{ x: T }) }
f[U]($u)
}
3 changes: 3 additions & 0 deletions tests/pos-macros/captured-type/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test =
foo(1)
foo("abc")

0 comments on commit 5e95030

Please sign in to comment.