Skip to content

Commit 926b08e

Browse files
committed
Don't widen T | Null in widenUnion
1 parent ed00d96 commit 926b08e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,9 @@ object Types {
10681068
* instead of `ArrayBuffer[? >: Int | A <: Int & A]`
10691069
*/
10701070
def widenUnion(implicit ctx: Context): Type = widen match {
1071-
case OrType(tp1, tp2) =>
1072-
ctx.typeComparer.lub(tp1.widenUnion, tp2.widenUnion, canConstrain = true) match {
1071+
case tp @ OrType(tp1, tp2) =>
1072+
if tp1.isNull || tp2.isNull then tp
1073+
else ctx.typeComparer.lub(tp1.widenUnion, tp2.widenUnion, canConstrain = true) match {
10731074
case union: OrType => union.join
10741075
case res => res
10751076
}
@@ -1399,6 +1400,10 @@ object Types {
13991400
case _ => true
14001401
}
14011402

1403+
/** Is this (an alias of) the `scala.Null` type? */
1404+
final def isNull(given Context) =
1405+
isRef(defn.NullClass)
1406+
14021407
/** The resultType of a LambdaType, or ExprType, the type itself for others */
14031408
def resultType(implicit ctx: Context): Type = this
14041409

@@ -2293,7 +2298,7 @@ object Types {
22932298
}
22942299

22952300
/** The singleton type for path prefix#myDesignator.
2296-
*/
2301+
*/
22972302
abstract case class TermRef(override val prefix: Type,
22982303
private var myDesignator: Designator)
22992304
extends NamedType with SingletonType with ImplicitRef {
@@ -2886,6 +2891,23 @@ object Types {
28862891
else apply(tp1, tp2)
28872892
}
28882893

2894+
object OrNull with
2895+
private def stripNull(tp: Type)(given Context): Type = tp match
2896+
case tp @ OrType(tp1, tp2) =>
2897+
if tp1.isNull then tp2
2898+
else if tp2.isNull then tp1
2899+
else tp.derivedOrType(stripNull(tp1), stripNull(tp2))
2900+
case tp @ AndType(tp1, tp2) =>
2901+
tp.derivedAndType(stripNull(tp1), stripNull(tp2))
2902+
case _ =>
2903+
tp
2904+
def apply(tp: Type)(given Context) =
2905+
OrType(tp, defn.NullType)
2906+
def unapply(tp: Type)(given Context): Option[Type] =
2907+
val tp1 = stripNull(tp)
2908+
if tp1 ne tp then Some(tp1) else None
2909+
end OrNull
2910+
28892911
// ----- ExprType and LambdaTypes -----------------------------------
28902912

28912913
// Note: method types are cached whereas poly types are not. The reason

0 commit comments

Comments
 (0)