@@ -1059,7 +1059,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
10591059 val (stats1, exprCtx) = withoutMode(Mode .Pattern ) {
10601060 typedBlockStats(tree.stats)
10611061 }
1062- val expr1 = typedExpr(tree.expr, pt.dropIfProto)(using exprCtx)
1062+ var expr1 = typedExpr(tree.expr, pt.dropIfProto)(using exprCtx)
1063+
1064+ // If unsafe nulls is enabled inside a block but not enabled outside
1065+ // and the type does not conform the expected type without unsafe nulls,
1066+ // we will cast the last expression to the expected type.
1067+ // See: tests/explicit-nulls/pos/unsafe-block.scala
1068+ if ctx.mode.is(Mode .SafeNulls )
1069+ && ! exprCtx.mode.is(Mode .SafeNulls )
1070+ && pt.isValueType
1071+ && ! inContext(exprCtx.addMode(Mode .SafeNulls ))(expr1.tpe <:< pt) then
1072+ expr1 = expr1.cast(pt)
1073+
10631074 ensureNoLocalRefs(
10641075 cpy.Block (tree)(stats1, expr1)
10651076 .withType(expr1.tpe)
@@ -2602,7 +2613,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
26022613 |The selector is not a member of an object or package. """ )
26032614 else typd(imp.expr, AnySelectionProto )
26042615
2605- def typedImport (imp : untpd.Import , sym : Symbol )(using Context ): Tree =
2616+ def typedImport (imp : untpd.Import )(using Context ): Tree =
2617+ val sym = retrieveSym(imp)
26062618 val expr1 = typedImportQualifier(imp, typedExpr(_, _)(using ctx.withOwner(sym)))
26072619 checkLegalImportPath(expr1)
26082620 val selectors1 = typedSelectors(imp.selectors)
@@ -2869,7 +2881,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
28692881 case tree : untpd.If => typedIf(tree, pt)
28702882 case tree : untpd.Function => typedFunction(tree, pt)
28712883 case tree : untpd.Closure => typedClosure(tree, pt)
2872- case tree : untpd.Import => typedImport(tree, retrieveSym(tree) )
2884+ case tree : untpd.Import => typedImport(tree)
28732885 case tree : untpd.Export => typedExport(tree)
28742886 case tree : untpd.Match => typedMatch(tree, pt)
28752887 case tree : untpd.Return => typedReturn(tree)
0 commit comments