Skip to content

Commit 56cdadc

Browse files
committed
optimize widenUnion
1 parent f009775 commit 56cdadc

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,8 @@ class Definitions {
295295
@tu lazy val AnyRefAlias: TypeSymbol = enterAliasType(tpnme.AnyRef, ObjectType)
296296
def AnyRefType: TypeRef = AnyRefAlias.typeRef
297297

298-
@tu lazy val Object_eq: TermSymbol = {
299-
// If explicit nulls is enabled, then we want to allow `(x: String).eq(null)`, so we need
300-
// to adjust the signature of `eq` accordingly.
301-
enterMethod(ObjectClass, nme.eq, methOfAnyRefOrNull(BooleanType), Final)
302-
}
303-
@tu lazy val Object_ne: TermSymbol = {
304-
// If explicit nulls is enabled, then we want to allow `(x: String).ne(null)`, so we need
305-
// to adjust the signature of `ne` accordingly.
306-
enterMethod(ObjectClass, nme.ne, methOfAnyRefOrNull(BooleanType), Final)
307-
}
298+
@tu lazy val Object_eq: TermSymbol = enterMethod(ObjectClass, nme.eq, methOfAnyRef(BooleanType), Final)
299+
@tu lazy val Object_ne: TermSymbol = enterMethod(ObjectClass, nme.ne, methOfAnyRef(BooleanType), Final)
308300
@tu lazy val Object_synchronized: TermSymbol = enterPolyMethod(ObjectClass, nme.synchronized_, 1,
309301
pt => MethodType(List(pt.paramRefs(0)), pt.paramRefs(0)), Final)
310302
@tu lazy val Object_clone: TermSymbol = enterMethod(ObjectClass, nme.clone_, MethodType(Nil, ObjectType), Protected)
@@ -839,7 +831,7 @@ class Definitions {
839831
// convenient one-parameter method types
840832
def methOfAny(tp: Type): MethodType = MethodType(List(AnyType), tp)
841833
def methOfAnyVal(tp: Type): MethodType = MethodType(List(AnyValType), tp)
842-
def methOfAnyRefOrNull(tp: Type): MethodType = MethodType(List(ObjectType.maybeNullable), tp)
834+
def methOfAnyRef(tp: Type): MethodType = MethodType(List(ObjectType), tp)
843835

844836
// Derived types
845837

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,30 +1102,30 @@ object Types {
11021102
* Exception (if `-YexplicitNulls` is set): if this type is a nullable union (i.e. of the form `T | Null`),
11031103
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
11041104
*/
1105-
def widenUnion(implicit ctx: Context): Type = {
1106-
widen match {
1107-
case tp @ OrType(lhs, rhs) =>
1108-
tp match {
1109-
case OrNull(tp1) =>
1110-
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
1111-
val tp1Widen = tp1.widenUnion
1112-
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
1113-
else tp.derivedOrType(tp1Widen, defn.NullType)
1114-
case _ =>
1115-
ctx.typeComparer.lub(lhs.widenUnion, rhs.widenUnion, canConstrain = true) match {
1116-
case union: OrType => union.join
1117-
case res => res
1118-
}
1119-
}
1120-
case tp @ AndType(tp1, tp2) =>
1121-
tp derived_& (tp1.widenUnion, tp2.widenUnion)
1122-
case tp: RefinedType =>
1123-
tp.derivedRefinedType(tp.parent.widenUnion, tp.refinedName, tp.refinedInfo)
1124-
case tp: RecType =>
1125-
tp.rebind(tp.parent.widenUnion)
1126-
case tp =>
1127-
tp
1128-
}
1105+
def widenUnion(implicit ctx: Context): Type = widen match {
1106+
case tp @ OrNull(tp1): OrType =>
1107+
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
1108+
val tp1Widen = tp1.widenUnion
1109+
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
1110+
else tp.derivedOrType(tp1Widen, defn.NullType)
1111+
case tp =>
1112+
tp.widenUnionWithoutNull
1113+
}
1114+
1115+
def widenUnionWithoutNull(implicit ctx: Context): Type = widen match {
1116+
case tp @ OrType(lhs, rhs) =>
1117+
ctx.typeComparer.lub(lhs.widenUnionWithoutNull, rhs.widenUnionWithoutNull, canConstrain = true) match {
1118+
case union: OrType => union.join
1119+
case res => res
1120+
}
1121+
case tp @ AndType(tp1, tp2) =>
1122+
tp derived_& (tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull)
1123+
case tp: RefinedType =>
1124+
tp.derivedRefinedType(tp.parent.widenUnion, tp.refinedName, tp.refinedInfo)
1125+
case tp: RecType =>
1126+
tp.rebind(tp.parent.widenUnion)
1127+
case tp =>
1128+
tp
11291129
}
11301130

11311131
/** Widen all top-level singletons reachable by dealiasing

0 commit comments

Comments
 (0)