@@ -1068,8 +1068,9 @@ object Types {
1068
1068
* instead of `ArrayBuffer[? >: Int | A <: Int & A]`
1069
1069
*/
1070
1070
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 {
1073
1074
case union : OrType => union.join
1074
1075
case res => res
1075
1076
}
@@ -1399,6 +1400,10 @@ object Types {
1399
1400
case _ => true
1400
1401
}
1401
1402
1403
+ /** Is this (an alias of) the `scala.Null` type? */
1404
+ final def isNull (given Context ) =
1405
+ isRef(defn.NullClass )
1406
+
1402
1407
/** The resultType of a LambdaType, or ExprType, the type itself for others */
1403
1408
def resultType (implicit ctx : Context ): Type = this
1404
1409
@@ -2293,7 +2298,7 @@ object Types {
2293
2298
}
2294
2299
2295
2300
/** The singleton type for path prefix#myDesignator.
2296
- */
2301
+ */
2297
2302
abstract case class TermRef (override val prefix : Type ,
2298
2303
private var myDesignator : Designator )
2299
2304
extends NamedType with SingletonType with ImplicitRef {
@@ -2886,6 +2891,23 @@ object Types {
2886
2891
else apply(tp1, tp2)
2887
2892
}
2888
2893
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
+
2889
2911
// ----- ExprType and LambdaTypes -----------------------------------
2890
2912
2891
2913
// Note: method types are cached whereas poly types are not. The reason
0 commit comments