Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new EXPLICITtpt to TASTy format #17298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,12 @@ class TreePickler(pickler: TastyPickler) {
withLength {
writeNat(idx)
pickleType(tree.tpe, richTypes = true)
args.foreach(pickleTree)
args.foreach { arg =>
arg.tpe match
case _: TermRef if arg.isType => writeByte(EXPLICITtpt)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 3.3 we can "backport" this change as

arg.tpe match
  case _: TermRef if arg.isType => report.warning(em"Term reference used in type of nested quote. This quote may fail to unpickle due to a bug that cannot be fixed in 3.3.x. This issue is fixed in 3.4.0.", arg)
  case _ =>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smarter I added this new commit containing the check to identify the problematic cases and optimize the pickled quote size by eliding the EXPLICITtpt when it is unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also believe this should be an error not a warning, and if possible give a workaround that can be used in 3.3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have yet to find an example that fails in 3.3. We used the wrong pickling/unpickling for those term refs.

The issues only arose when trying to fix code that would not compile with some specific term-refs and this-types. When fixing those issues we start encountering the ambiguity in pickling/unpickling of those term-refs.

case _ =>
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 @@ -1243,6 +1243,8 @@ class TreeUnpickler(reader: TastyReader,
ByNameTypeTree(if withPureFuns then arg else arg.adaptByNameArgUnderPureFuns)
case NAMEDARG =>
NamedArg(readName(), readTree())
case EXPLICITtpt =>
readTpt()
case _ =>
readPathTree()
}
Expand Down Expand Up @@ -1468,10 +1470,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)(readTree())
Hole(true, idx, args, EmptyTree, tpe)
readHole(end, isTerm = true)
case _ =>
readPathTree()
}
Expand Down Expand Up @@ -1502,10 +1501,7 @@ class TreeUnpickler(reader: TastyReader,
case HOLE =>
readByte()
val end = readEnd()
val idx = readNat()
val tpe = readType()
val args = until(end)(readTree())
Hole(false, idx, args, EmptyTree, tpe)
readHole(end, isTerm = false)
case _ =>
if (isTypeTreeTag(nextByte)) readTree()
else {
Expand Down Expand Up @@ -1538,6 +1534,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)(readTree())
Hole(isTerm, idx, args, EmptyTree, tpe)

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

Expand Down
1 change: 1 addition & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ object MiMaFilters {
val Library: Seq[ProblemFilter] = Seq(
)
val TastyCore: Seq[ProblemFilter] = Seq(
ProblemFilters.exclude[DirectMissingMethodProblem]("dotty.tools.tasty.TastyFormat.EXPLICITtpt"),
)
val Interfaces: Seq[ProblemFilter] = Seq(
)
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")