@@ -1253,26 +1253,35 @@ class Typer extends Namer
1253
1253
if (tree.tpt.isEmpty)
1254
1254
meth1.tpe.widen match {
1255
1255
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 {
1257
1265
case SAMType (sam)
1258
- if ! defn.isFunctionType(pt) && mt <:< sam =>
1266
+ if ! defn.isFunctionType(pt1) && mt <:< sam ||
1267
+ adaptWithUnsafeNullConver && mt.isUnsafeConvertable(sam) =>
1259
1268
// SAMs of the form C[?] where C is a class cannot be conversion targets.
1260
1269
// The resulting class `class $anon extends C[?] {...}` would be illegal,
1261
1270
// since type arguments to `C`'s super constructor cannot be constructed.
1262
1271
def isWildcardClassSAM =
1263
- ! pt .classSymbol.is(Trait ) && pt .argInfos.exists(_.isInstanceOf [TypeBounds ])
1272
+ ! pt1 .classSymbol.is(Trait ) && pt1 .argInfos.exists(_.isInstanceOf [TypeBounds ])
1264
1273
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
1268
1277
// Replace the underspecified expected type by one based on the closure method type
1269
1278
defn.PartialFunctionOf (mt.firstParamTypes.head, mt.resultType)
1270
1279
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)
1276
1285
}
1277
1286
TypeTree (targetTpe)
1278
1287
case _ =>
0 commit comments