Skip to content

Commit

Permalink
Handle pickled forward references in pickled expressions
Browse files Browse the repository at this point in the history
To compute forward references it was assumed that the owner of that
symbol can be found in the TASTy. This is not always the case with TASTy
expressions of pickled quotes. The owner might be outside the quote, in
this case the context already has the owner of the referenced symbol.
These are local symbols defined at the top-level of the TASTy.

Fixes #16843
  • Loading branch information
nicolasstucki committed Feb 8, 2023
1 parent 723d140 commit 6bfde43
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ class TreeUnpickler(reader: TastyReader,
case Some(sym) =>
sym
case None =>
val sym = forkAt(addr).createSymbol()(using ctx.withOwner(ownerTree.findOwner(addr)))
val ownerInTree = ownerTree.findOwner(addr)
val ctx1 =
if ownerInTree.exists then ctx.withOwner(ownerInTree)
else ctx // this is a pickled expression and the owner is located outside the expression (i.e. pickled quote)
val sym = forkAt(addr).createSymbol()(using ctx1)
report.log(i"forward reference to $sym")
sym
}
Expand Down Expand Up @@ -1620,7 +1624,6 @@ class TreeUnpickler(reader: TastyReader,
try cs match {
case ot :: cs1 =>
if (ot.addr.index == addr.index) {
assert(current.exists, i"no symbol at $addr")
current
}
else if (ot.addr.index < addr.index && addr.index < ot.end.index)
Expand Down
18 changes: 18 additions & 0 deletions tests/pos-macros/i16843/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import scala.quoted.*

inline def foo: Int = ${ fooImpl }

def fooImpl(using Quotes): Expr[Int] =
'{
val b = ${
val a = '{
(1: Int) match
case x @ (y: Int) => 0
}
a
}

(1: Int) match
case x @ (y: Int) => 0
}

1 change: 1 addition & 0 deletions tests/pos-macros/i16843/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test = foo

0 comments on commit 6bfde43

Please sign in to comment.