Skip to content

Commit 530ffb7

Browse files
committed
Simplify Typer
- Drop tryUnsafeNullConver - Drop special condition in Synthesizer
1 parent 28a0d1f commit 530ffb7

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
114114
cmpWithBoxed(cls1, cls2)
115115
else if cls2.isPrimitiveValueClass then
116116
cmpWithBoxed(cls2, cls1)
117-
else if ctx.explicitNulls && !config.Feature.enabled(nme.unsafeNulls) then
117+
else if ctx.mode.is(Mode.SafeNulls) then
118118
// If explicit nulls is enabled, and unsafeNulls is not enabled,
119119
// we want to disallow comparison between Object and Null.
120120
// If a nullable value has a non-nullable type, we can still cast it to

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

+20-32
Original file line numberDiff line numberDiff line change
@@ -1345,27 +1345,26 @@ class Typer extends Namer
13451345
if (tree.tpt.isEmpty)
13461346
meth1.tpe.widen match {
13471347
case mt: MethodType =>
1348-
val pt1 = pt.stripNull
1349-
pt1 match {
1350-
case SAMType(sam)
1348+
pt.stripNull match {
1349+
case pt @ SAMType(sam)
13511350
if !defn.isFunctionType(pt) && mt <:< sam =>
13521351
// SAMs of the form C[?] where C is a class cannot be conversion targets.
13531352
// The resulting class `class $anon extends C[?] {...}` would be illegal,
13541353
// since type arguments to `C`'s super constructor cannot be constructed.
13551354
def isWildcardClassSAM =
1356-
!pt1.classSymbol.is(Trait) && pt1.argInfos.exists(_.isInstanceOf[TypeBounds])
1355+
!pt.classSymbol.is(Trait) && pt.argInfos.exists(_.isInstanceOf[TypeBounds])
13571356
val targetTpe =
1358-
if isFullyDefined(pt1, ForceDegree.all) && !isWildcardClassSAM then
1359-
pt1
1360-
else if pt1.isRef(defn.PartialFunctionClass) then
1357+
if isFullyDefined(pt, ForceDegree.all) && !isWildcardClassSAM then
1358+
pt
1359+
else if pt.isRef(defn.PartialFunctionClass) then
13611360
// Replace the underspecified expected type by one based on the closure method type
13621361
defn.PartialFunctionOf(mt.firstParamTypes.head, mt.resultType)
13631362
else
1364-
report.error(ex"result type of lambda is an underspecified SAM type $pt1", tree.srcPos)
1365-
pt1
1366-
if (pt1.classSymbol.isOneOf(FinalOrSealed)) {
1367-
val offendingFlag = pt1.classSymbol.flags & FinalOrSealed
1368-
report.error(ex"lambda cannot implement $offendingFlag ${pt1.classSymbol}", tree.srcPos)
1363+
report.error(ex"result type of lambda is an underspecified SAM type $pt", tree.srcPos)
1364+
pt
1365+
if (pt.classSymbol.isOneOf(FinalOrSealed)) {
1366+
val offendingFlag = pt.classSymbol.flags & FinalOrSealed
1367+
report.error(ex"lambda cannot implement $offendingFlag ${pt.classSymbol}", tree.srcPos)
13691368
}
13701369
TypeTree(targetTpe)
13711370
case _ =>
@@ -3645,15 +3644,6 @@ class Typer extends Namer
36453644
if target <:< pt then
36463645
return readapt(tree.cast(target))
36473646

3648-
def tryUnsafeNullConver(fail: => Tree): Tree =
3649-
// In explcit-nulls, if first subtyping fails, the unsafe-nulls subtyping
3650-
// (where `Null` is subtype of all reference types) is used here to
3651-
// recheck the types.
3652-
if pt.isValueType && (wtp <:< pt) then
3653-
// If unsafe-nulls subtyping successes, we can cast the tree to `pt` directly
3654-
tree.cast(pt)
3655-
else fail
3656-
36573647
def recover(failure: SearchFailureType) =
36583648
if canDefineFurther(wtp) then readapt(tree)
36593649
else err.typeMismatch(tree, pt, failure)
@@ -3682,18 +3672,16 @@ class Typer extends Namer
36823672
checkImplicitConversionUseOK(found)
36833673
withoutMode(Mode.ImplicitsEnabled)(readapt(found))
36843674
case failure: SearchFailure =>
3685-
tryUnsafeNullConver {
3686-
if (pt.isInstanceOf[ProtoType] && !failure.isAmbiguous) then
3687-
// don't report the failure but return the tree unchanged. This
3688-
// will cause a failure at the next level out, which usually gives
3689-
// a better error message. To compensate, store the encountered failure
3690-
// as an attachment, so that it can be reported later as an addendum.
3691-
rememberSearchFailure(tree, failure)
3692-
tree
3693-
else recover(failure.reason)
3694-
}
3675+
if (pt.isInstanceOf[ProtoType] && !failure.isAmbiguous) then
3676+
// don't report the failure but return the tree unchanged. This
3677+
// will cause a failure at the next level out, which usually gives
3678+
// a better error message. To compensate, store the encountered failure
3679+
// as an attachment, so that it can be reported later as an addendum.
3680+
rememberSearchFailure(tree, failure)
3681+
tree
3682+
else recover(failure.reason)
36953683
case _ =>
3696-
tryUnsafeNullConver(recover(NoMatchingImplicits))
3684+
recover(NoMatchingImplicits)
36973685
end adaptToSubType
36983686

36993687
def adaptType(tp: Type): Tree = {

0 commit comments

Comments
 (0)