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/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