@@ -1287,7 +1287,7 @@ object Types {
1287
1287
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
1288
1288
*/
1289
1289
def widenUnion (using Context ): Type = widen match
1290
- case tp @ OrNull (tp1): OrType =>
1290
+ case tp @ OrNull (tp1) =>
1291
1291
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
1292
1292
val tp1Widen = tp1.widenUnionWithoutNull
1293
1293
if (tp1Widen.isRef(defn.AnyClass )) tp1Widen
@@ -3497,10 +3497,31 @@ object Types {
3497
3497
*/
3498
3498
object OrNull {
3499
3499
def apply (tp : Type )(using Context ) =
3500
- if tp.isNullType then tp else OrType (tp, defn.NullType , soft = false )
3500
+ OrType (tp, defn.NullType , soft = false )
3501
+ def unapply (tp : OrType )(using Context ): Option [Type ] =
3502
+ if (ctx.explicitNulls) {
3503
+ val tp1 = tp.stripNull()
3504
+ if tp1 ne tp then Some (tp1) else None
3505
+ }
3506
+ else None
3507
+ }
3508
+
3509
+ /** An extractor object to pattern match against a Java-nullable union.
3510
+ * e.g.
3511
+ *
3512
+ * (tp: Type) match
3513
+ * case OrUncheckedNull(tp1) => // tp had the form `tp1 | UncheckedNull`
3514
+ * case _ => // tp was not a Java-nullable union
3515
+ */
3516
+ object OrUncheckedNull {
3517
+ def apply (tp : Type )(using Context ) =
3518
+ OrType (tp, defn.UncheckedNullAliasType , soft = false )
3501
3519
def unapply (tp : Type )(using Context ): Option [Type ] =
3502
- val tp1 = tp.stripNull
3503
- if tp1 ne tp then Some (tp1) else None
3520
+ if (ctx.explicitNulls) {
3521
+ val tp1 = tp.stripUncheckedNull
3522
+ if tp1 ne tp then Some (tp1) else None
3523
+ }
3524
+ else None
3504
3525
}
3505
3526
3506
3527
// ----- ExprType and LambdaTypes -----------------------------------
0 commit comments