Skip to content

Commit dcb0d15

Browse files
authored
Merge pull request #14962 from dotty-staging/fix-14947
Honor language imports when unpickling
2 parents 1a4db89 + b34964b commit dcb0d15

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,14 +1059,25 @@ class TreeUnpickler(reader: TastyReader,
10591059
else
10601060
Nil
10611061

1062-
def readIndexedStats(exprOwner: Symbol, end: Addr)(using Context): List[Tree] =
1063-
until(end)(readIndexedStat(exprOwner))
1062+
def readIndexedStats[T](exprOwner: Symbol, end: Addr, k: (List[Tree], Context) => T = sameTrees)(using Context): T =
1063+
val buf = new mutable.ListBuffer[Tree]
1064+
var curCtx = ctx
1065+
while currentAddr.index < end.index do
1066+
val stat = readIndexedStat(exprOwner)(using curCtx)
1067+
buf += stat
1068+
stat match
1069+
case stat: Import => curCtx = ctx.importContext(stat, stat.symbol)
1070+
case _ =>
1071+
assert(currentAddr.index == end.index)
1072+
k(buf.toList, curCtx)
10641073

1065-
def readStats(exprOwner: Symbol, end: Addr)(using Context): List[Tree] = {
1074+
def readStats[T](exprOwner: Symbol, end: Addr, k: (List[Tree], Context) => T = sameTrees)(using Context): T = {
10661075
fork.indexStats(end)
1067-
readIndexedStats(exprOwner, end)
1076+
readIndexedStats(exprOwner, end, k)
10681077
}
10691078

1079+
private def sameTrees(xs: List[Tree], ctx: Context) = xs
1080+
10701081
def readIndexedParams[T <: MemberDef](tag: Int)(using Context): List[T] =
10711082
collectWhile(nextByte == tag) { readIndexedDef().asInstanceOf[T] }
10721083

@@ -1174,9 +1185,8 @@ class TreeUnpickler(reader: TastyReader,
11741185
case BLOCK =>
11751186
val exprReader = fork
11761187
skipTree()
1177-
val stats = readStats(ctx.owner, end)
1178-
val expr = exprReader.readTerm()
1179-
Block(stats, expr)
1188+
readStats(ctx.owner, end,
1189+
(stats, ctx) => Block(stats, exprReader.readTerm()(using ctx)))
11801190
case INLINED =>
11811191
val exprReader = fork
11821192
skipTree()

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ class CompilationTests {
251251
compileFilesInDir("tests/explicit-nulls/pos-patmat", explicitNullsOptions and "-Xfatal-warnings"),
252252
compileFilesInDir("tests/explicit-nulls/unsafe-common", explicitNullsOptions and "-language:unsafeNulls"),
253253
compileFile("tests/explicit-nulls/pos-special/i14682.scala", explicitNullsOptions and "-Ysafe-init"),
254+
compileFile("tests/explicit-nulls/pos-special/i14947.scala", explicitNullsOptions and "-Ytest-pickler" and "-Xprint-types"),
254255
)
255256
}.checkCompile()
256257

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class B:
2+
def g: String | Null = ???
3+
4+
def f =
5+
import scala.language.unsafeNulls
6+
if ??? then "" else g
7+
8+
import scala.language.unsafeNulls
9+
class C {
10+
def g: String | Null = ???
11+
12+
def f =
13+
if ??? then "" else g
14+
}

0 commit comments

Comments
 (0)