Skip to content

Honor language imports when unpickling #14962

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

Merged
merged 3 commits into from
Apr 19, 2022
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
24 changes: 17 additions & 7 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1059,14 +1059,25 @@ class TreeUnpickler(reader: TastyReader,
else
Nil

def readIndexedStats(exprOwner: Symbol, end: Addr)(using Context): List[Tree] =
until(end)(readIndexedStat(exprOwner))
def readIndexedStats[T](exprOwner: Symbol, end: Addr, k: (List[Tree], Context) => T = sameTrees)(using Context): T =
val buf = new mutable.ListBuffer[Tree]
var curCtx = ctx
while currentAddr.index < end.index do
val stat = readIndexedStat(exprOwner)(using curCtx)
buf += stat
stat match
case stat: Import => curCtx = ctx.importContext(stat, stat.symbol)
case _ =>
assert(currentAddr.index == end.index)
k(buf.toList, curCtx)

def readStats(exprOwner: Symbol, end: Addr)(using Context): List[Tree] = {
def readStats[T](exprOwner: Symbol, end: Addr, k: (List[Tree], Context) => T = sameTrees)(using Context): T = {
fork.indexStats(end)
readIndexedStats(exprOwner, end)
readIndexedStats(exprOwner, end, k)
}

private def sameTrees(xs: List[Tree], ctx: Context) = xs

def readIndexedParams[T <: MemberDef](tag: Int)(using Context): List[T] =
collectWhile(nextByte == tag) { readIndexedDef().asInstanceOf[T] }

Expand Down Expand Up @@ -1174,9 +1185,8 @@ class TreeUnpickler(reader: TastyReader,
case BLOCK =>
val exprReader = fork
skipTree()
val stats = readStats(ctx.owner, end)
val expr = exprReader.readTerm()
Block(stats, expr)
readStats(ctx.owner, end,
(stats, ctx) => Block(stats, exprReader.readTerm()(using ctx)))
case INLINED =>
val exprReader = fork
skipTree()
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class CompilationTests {
compileFilesInDir("tests/explicit-nulls/pos-patmat", explicitNullsOptions and "-Xfatal-warnings"),
compileFilesInDir("tests/explicit-nulls/unsafe-common", explicitNullsOptions and "-language:unsafeNulls"),
compileFile("tests/explicit-nulls/pos-special/i14682.scala", explicitNullsOptions and "-Ysafe-init"),
compileFile("tests/explicit-nulls/pos-special/i14947.scala", explicitNullsOptions and "-Ytest-pickler" and "-Xprint-types"),
)
}.checkCompile()

Expand Down
14 changes: 14 additions & 0 deletions tests/explicit-nulls/pos-special/i14947.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class B:
def g: String | Null = ???

def f =
import scala.language.unsafeNulls
if ??? then "" else g

import scala.language.unsafeNulls
class C {
def g: String | Null = ???

def f =
if ??? then "" else g
}