Skip to content

Commit 413753d

Browse files
committed
Readd typedUnadaptedWithBlock
1 parent afa4a09 commit 413753d

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

compiler/src/dotty/tools/dotc/core/Mode.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,6 @@ object Mode {
114114
val QuotedPattern: Mode = newMode(25, "QuotedPattern")
115115

116116
val UnsafeNullConversion: Mode = newMode(26, "UnsafeNullConversion")
117+
118+
val UnsafeNullConversionJava: Mode = newMode(27, "UnsafeNullConversionJava")
117119
}

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,9 @@ trait Applications extends Compatibility {
892892
val argsCtx =
893893
if ctx.explicitNulls && ctx.explicitNullsJavaCompatible then
894894
if funRef.symbol.is(JavaDefined) then
895-
ctx.addMode(Mode.UnsafeNullConversion)
895+
ctx.addMode(Mode.UnsafeNullConversionJava)
896896
else
897-
ctx.retractMode(Mode.UnsafeNullConversion)
897+
ctx.retractMode(Mode.UnsafeNullConversionJava)
898898
else ctx
899899
val app = ApplyTo(tree, fun1, funRef, proto, pt)(using argsCtx)
900900
convertNewGenericArray(

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,21 @@ object ProtoTypes {
366366
*/
367367
def typedArg(arg: untpd.Tree, formal: Type)(using Context): Tree = {
368368
val wideFormal = formal.widenExpr
369+
val javaCallUnsafeNull = ctx.mode.is(Mode.UnsafeNullConversionJava)
369370
val argCtx =
370-
(if wideFormal eq formal then ctx
371-
else ctx.withNotNullInfos(ctx.notNullInfos.retractMutables))
372-
.retractMode(Mode.UnsafeNullConversion)
371+
if wideFormal eq formal then ctx
372+
else ctx.withNotNullInfos(ctx.notNullInfos.retractMutables)
373+
val adaptCtx = if javaCallUnsafeNull then ctx.addMode(Mode.UnsafeNullConversion) else ctx
373374
val locked = ctx.typerState.ownedVars
374-
val targ = cacheTypedArg(arg,
375+
val targ = if javaCallUnsafeNull then
376+
cacheTypedArg(arg,
377+
typer.typedUnadaptedWithBlock(_, wideFormal, locked)(using argCtx.retractMode(Mode.UnsafeNullConversionJava)),
378+
force = true)
379+
else
380+
cacheTypedArg(arg,
375381
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),
376382
force = true)
377-
typer.adapt(targ, wideFormal, locked)
383+
typer.adapt(targ, wideFormal, locked)(using adaptCtx)
378384
}
379385

380386
/** The type of the argument `arg`, or `NoType` if `arg` has not been typed before

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,28 @@ class Typer extends Namer
25292529
}
25302530
}
25312531

2532+
/** A special typedUnadapted which calls typedUnadapted on the last expression of a block as well */
2533+
def typedUnadaptedWithBlock(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree = {
2534+
tree match {
2535+
case block: untpd.Block =>
2536+
val block1 = desugar.block(block)
2537+
inContext(ctx.fresh.setNewScope) {
2538+
val (stats1, exprCtx) = withoutMode(Mode.Pattern) {
2539+
typedBlockStats(block1.stats)
2540+
}
2541+
val expr1 = typedUnadaptedWithBlock(block1.expr, pt.dropIfProto, locked)(using exprCtx)
2542+
val expr1Tpe = expr1.tpe
2543+
ensureNoLocalRefs(
2544+
cpy.Block(block1)(stats1, expr1)
2545+
.withType(expr1Tpe)
2546+
.withNotNullInfo(stats1.foldRight(expr1.notNullInfo)(_.notNullInfo.seq(_))),
2547+
expr1Tpe, localSyms(stats1))
2548+
}
2549+
case _ =>
2550+
typedUnadapted(tree, pt, locked)
2551+
}
2552+
}
2553+
25322554
/** Interpolate and simplify the type of the given tree. */
25332555
protected def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): tree.type = {
25342556
if (!tree.denot.isOverloaded &&
@@ -3444,7 +3466,7 @@ class Typer extends Namer
34443466
case closure(Nil, id @ Ident(nme.ANON_FUN), _)
34453467
if defn.isFunctionType(wtp) && !defn.isFunctionType(pt) =>
34463468
val pt1 =
3447-
if adaptWithUnsafeNullConver then
3469+
if ctx.explicitNulls then
34483470
pt.stripNull
34493471
else pt
34503472
pt1 match {

0 commit comments

Comments
 (0)