From af31907083b2fa3c4e5b6b6664e8bec844fbe98e Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Thu, 3 Feb 2022 23:41:34 -0500 Subject: [PATCH 1/2] Use correct stat context when transforming Block --- .../dotty/tools/dotc/ast/TreeMapWithImplicits.scala | 8 +------- compiler/src/dotty/tools/dotc/ast/tpd.scala | 7 +++++++ .../src/dotty/tools/dotc/transform/MegaPhase.scala | 5 ++--- tests/explicit-nulls/pos/unsafe-chain.scala | 10 ++++++++++ 4 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 tests/explicit-nulls/pos/unsafe-chain.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala b/compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala index 3f4ff4687787..999a80c5e446 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala @@ -48,13 +48,7 @@ class TreeMapWithImplicits extends tpd.TreeMapWithPreciseStatContexts { override def transform(tree: Tree)(using Context): Tree = { try tree match { case Block(stats, expr) => - inContext(nestedScopeCtx(stats)) { - if stats.exists(_.isInstanceOf[Import]) then - // need to transform stats and expr together to account for import visibility - val stats1 = transformStats(stats :+ expr, ctx.owner) - cpy.Block(tree)(stats1.init, stats1.last) - else super.transform(tree) - } + super.transform(tree)(using nestedScopeCtx(stats)) case tree: DefDef => inContext(localCtx(tree)) { cpy.DefDef(tree)( diff --git a/compiler/src/dotty/tools/dotc/ast/tpd.scala b/compiler/src/dotty/tools/dotc/ast/tpd.scala index 98ea6e0c5c44..eb040fce23ae 100644 --- a/compiler/src/dotty/tools/dotc/ast/tpd.scala +++ b/compiler/src/dotty/tools/dotc/ast/tpd.scala @@ -1195,6 +1195,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { * - imports are reflected in the contexts of subsequent statements */ class TreeMapWithPreciseStatContexts(cpy: TreeCopier = tpd.cpy) extends TreeMap(cpy): + override def transform(tree: Tree)(using Context): Tree = tree match + case Block(stats, expr) => + val stats1 = transformStats(stats :+ expr, ctx.owner) + cpy.Block(tree)(stats1.init, stats1.last) + case _ => + super.transform(tree) + override def transformStats(trees: List[Tree], exprOwner: Symbol)(using Context): List[Tree] = trees.mapStatements(exprOwner, transform(_)) diff --git a/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala b/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala index 1d8a2d6b9eee..95136873e1fa 100644 --- a/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala +++ b/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala @@ -296,9 +296,8 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase { } case tree: Block => inContext(prepBlock(tree, start)(using outerCtx)) { - val stats = transformStats(tree.stats, ctx.owner, start) - val expr = transformTree(tree.expr, start) - goBlock(cpy.Block(tree)(stats, expr), start) + val stats1 = transformStats(tree.stats :+ tree.expr, ctx.owner, start) + goBlock(cpy.Block(tree)(stats1.init, stats1.last), start) } case tree: TypeApply => inContext(prepTypeApply(tree, start)(using outerCtx)) { diff --git a/tests/explicit-nulls/pos/unsafe-chain.scala b/tests/explicit-nulls/pos/unsafe-chain.scala new file mode 100644 index 000000000000..76c80d0c53fe --- /dev/null +++ b/tests/explicit-nulls/pos/unsafe-chain.scala @@ -0,0 +1,10 @@ +import java.nio.file.FileSystems +import java.util.ArrayList + +def directorySeparator: String = + import scala.language.unsafeNulls + FileSystems.getDefault().getSeparator() + +def getFirstOfFirst(xs: ArrayList[ArrayList[ArrayList[String]]]): String = + import scala.language.unsafeNulls + xs.get(0).get(0).get(0) \ No newline at end of file From c9cad4aeb1b0b1e7a753b6085a35150b5ada1ab9 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Wed, 9 Feb 2022 19:07:48 -0500 Subject: [PATCH 2/2] Revert change in MegaPhase --- compiler/src/dotty/tools/dotc/transform/MegaPhase.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala b/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala index 95136873e1fa..1d8a2d6b9eee 100644 --- a/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala +++ b/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala @@ -296,8 +296,9 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase { } case tree: Block => inContext(prepBlock(tree, start)(using outerCtx)) { - val stats1 = transformStats(tree.stats :+ tree.expr, ctx.owner, start) - goBlock(cpy.Block(tree)(stats1.init, stats1.last), start) + val stats = transformStats(tree.stats, ctx.owner, start) + val expr = transformTree(tree.expr, start) + goBlock(cpy.Block(tree)(stats, expr), start) } case tree: TypeApply => inContext(prepTypeApply(tree, start)(using outerCtx)) {