Skip to content

Commit c699d5b

Browse files
committed
FIx sam in typedClosure
1 parent 413753d commit c699d5b

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,26 +1253,35 @@ class Typer extends Namer
12531253
if (tree.tpt.isEmpty)
12541254
meth1.tpe.widen match {
12551255
case mt: MethodType =>
1256-
pt match {
1256+
val pt1 =
1257+
if ctx.explicitNulls then
1258+
pt.stripNull
1259+
else pt
1260+
val adaptWithUnsafeNullConver =
1261+
ctx.explicitNulls && (
1262+
config.Feature.enabled(nme.unsafeNulls) ||
1263+
ctx.mode.is(Mode.UnsafeNullConversion))
1264+
pt1 match {
12571265
case SAMType(sam)
1258-
if !defn.isFunctionType(pt) && mt <:< sam =>
1266+
if !defn.isFunctionType(pt1) && mt <:< sam ||
1267+
adaptWithUnsafeNullConver && mt.isUnsafeConvertable(sam) =>
12591268
// SAMs of the form C[?] where C is a class cannot be conversion targets.
12601269
// The resulting class `class $anon extends C[?] {...}` would be illegal,
12611270
// since type arguments to `C`'s super constructor cannot be constructed.
12621271
def isWildcardClassSAM =
1263-
!pt.classSymbol.is(Trait) && pt.argInfos.exists(_.isInstanceOf[TypeBounds])
1272+
!pt1.classSymbol.is(Trait) && pt1.argInfos.exists(_.isInstanceOf[TypeBounds])
12641273
val targetTpe =
1265-
if isFullyDefined(pt, ForceDegree.all) && !isWildcardClassSAM then
1266-
pt
1267-
else if pt.isRef(defn.PartialFunctionClass) then
1274+
if isFullyDefined(pt1, ForceDegree.all) && !isWildcardClassSAM then
1275+
pt1
1276+
else if pt1.isRef(defn.PartialFunctionClass) then
12681277
// Replace the underspecified expected type by one based on the closure method type
12691278
defn.PartialFunctionOf(mt.firstParamTypes.head, mt.resultType)
12701279
else
1271-
report.error(ex"result type of lambda is an underspecified SAM type $pt", tree.srcPos)
1272-
pt
1273-
if (pt.classSymbol.isOneOf(FinalOrSealed)) {
1274-
val offendingFlag = pt.classSymbol.flags & FinalOrSealed
1275-
report.error(ex"lambda cannot implement $offendingFlag ${pt.classSymbol}", tree.srcPos)
1280+
report.error(ex"result type of lambda is an underspecified SAM type $pt1", tree.srcPos)
1281+
pt1
1282+
if (pt1.classSymbol.isOneOf(FinalOrSealed)) {
1283+
val offendingFlag = pt1.classSymbol.flags & FinalOrSealed
1284+
report.error(ex"lambda cannot implement $offendingFlag ${pt1.classSymbol}", tree.srcPos)
12761285
}
12771286
TypeTree(targetTpe)
12781287
case _ =>

0 commit comments

Comments
 (0)