@@ -1059,7 +1059,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1059
1059
val (stats1, exprCtx) = withoutMode(Mode .Pattern ) {
1060
1060
typedBlockStats(tree.stats)
1061
1061
}
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
+
1063
1074
ensureNoLocalRefs(
1064
1075
cpy.Block (tree)(stats1, expr1)
1065
1076
.withType(expr1.tpe)
@@ -2602,7 +2613,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2602
2613
|The selector is not a member of an object or package. """ )
2603
2614
else typd(imp.expr, AnySelectionProto )
2604
2615
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)
2606
2618
val expr1 = typedImportQualifier(imp, typedExpr(_, _)(using ctx.withOwner(sym)))
2607
2619
checkLegalImportPath(expr1)
2608
2620
val selectors1 = typedSelectors(imp.selectors)
@@ -2869,7 +2881,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2869
2881
case tree : untpd.If => typedIf(tree, pt)
2870
2882
case tree : untpd.Function => typedFunction(tree, pt)
2871
2883
case tree : untpd.Closure => typedClosure(tree, pt)
2872
- case tree : untpd.Import => typedImport(tree, retrieveSym(tree) )
2884
+ case tree : untpd.Import => typedImport(tree)
2873
2885
case tree : untpd.Export => typedExport(tree)
2874
2886
case tree : untpd.Match => typedMatch(tree, pt)
2875
2887
case tree : untpd.Return => typedReturn(tree)
0 commit comments