@@ -307,7 +307,7 @@ object Types extends TypeUtils {
307
307
isRef(defn.ObjectClass ) && (typeSymbol eq defn.FromJavaObjectSymbol )
308
308
309
309
def containsFromJavaObject (using Context ): Boolean = this match
310
- case tp : FlexibleType => tp.original .containsFromJavaObject
310
+ case tp : FlexibleType => tp.underlying .containsFromJavaObject
311
311
case tp : OrType => tp.tp1.containsFromJavaObject || tp.tp2.containsFromJavaObject
312
312
case tp : AndType => tp.tp1.containsFromJavaObject && tp.tp2.containsFromJavaObject
313
313
case _ => isFromJavaObject
@@ -376,7 +376,7 @@ object Types extends TypeUtils {
376
376
case AppliedType (tycon, args) => tycon.unusableForInference || args.exists(_.unusableForInference)
377
377
case RefinedType (parent, _, rinfo) => parent.unusableForInference || rinfo.unusableForInference
378
378
case TypeBounds (lo, hi) => lo.unusableForInference || hi.unusableForInference
379
- case FlexibleType (underlying) => underlying.unusableForInference
379
+ case tp : FlexibleType => tp. underlying.unusableForInference
380
380
case tp : AndOrType => tp.tp1.unusableForInference || tp.tp2.unusableForInference
381
381
case tp : LambdaType => tp.resultType.unusableForInference || tp.paramInfos.exists(_.unusableForInference)
382
382
case WildcardType (optBounds) => optBounds.unusableForInference
@@ -3450,49 +3450,43 @@ object Types extends TypeUtils {
3450
3450
3451
3451
/* A flexible type is a type with a custom subtyping relationship.
3452
3452
* It is used by explicit nulls to represent a type coming from Java which can be
3453
- * consider as nullable or non-nullable depending on the context, in a similar way to Platform
3453
+ * considered as nullable or non-nullable depending on the context, in a similar way to Platform
3454
3454
* Types in Kotlin. A `FlexibleType(T)` generally behaves like a type variable with special bounds
3455
3455
* `T | Null .. T`, so that `T | Null <: FlexibleType(T) <: T`.
3456
3456
* A flexible type will be erased to its original type `T`.
3457
3457
*/
3458
- case class FlexibleType (original : Type , lo : Type , hi : Type ) extends CachedProxyType with ValueType {
3459
- def underlying (using Context ): Type = original
3458
+ case class FlexibleType (lo : Type , hi : Type ) extends CachedProxyType with ValueType {
3460
3459
3461
- override def superType (using Context ): Type = hi
3460
+ override def underlying (using Context ): Type = hi
3462
3461
3463
- def derivedFlexibleType (original : Type )(using Context ): Type =
3464
- if this .original eq original then this else FlexibleType (original )
3462
+ def derivedFlexibleType (hi : Type )(using Context ): Type =
3463
+ if hi eq this .hi then this else FlexibleType (hi )
3465
3464
3466
- override def computeHash (bs : Binders ): Int = doHash(bs, original )
3465
+ override def computeHash (bs : Binders ): Int = doHash(bs, hi )
3467
3466
3468
- override final def baseClasses (using Context ): List [ClassSymbol ] = original .baseClasses
3467
+ override final def baseClasses (using Context ): List [ClassSymbol ] = hi .baseClasses
3469
3468
}
3470
3469
3471
3470
object FlexibleType {
3472
- def apply (original : Type )(using Context ): Type = original match {
3471
+ def apply (tp : Type )(using Context ): Type = tp match {
3473
3472
case ft : FlexibleType => ft
3474
3473
case _ =>
3475
- // val original1 = original .stripNull
3476
- // if original1 .isNullType then
3474
+ // val tp1 = tp .stripNull
3475
+ // if tp1 .isNullType then
3477
3476
// // (Null)? =:= ? >: Null <: (Object & Null)
3478
- // FlexibleType(defn.NullType, original , AndType(defn.ObjectType, defn.NullType))
3477
+ // FlexibleType(tp , AndType(defn.ObjectType, defn.NullType))
3479
3478
// else
3480
3479
// // (T | Null)? =:= ? >: T | Null <: T
3481
3480
// // (T)? =:= ? >: T | Null <: T
3482
- // val hi = original1
3483
- // val lo = if hi eq original then OrNull(hi) else original
3484
- // FlexibleType(original, lo, hi)
3481
+ // val hi = tp1
3482
+ // val lo = if hi eq tp then OrNull(hi) else tp
3483
+ // FlexibleType(lo, hi)
3485
3484
//
3486
3485
// The commented out code does more work to analyze the original type to ensure the
3487
3486
// flexible type is always a subtype of the original type and the Object type.
3488
3487
// It is not necessary according to the use cases, so we choose to use a simpler
3489
3488
// rule.
3490
- FlexibleType (original, OrNull (original), original)
3491
- }
3492
-
3493
- def unapply (tp : Type )(using Context ): Option [Type ] = tp match {
3494
- case ft : FlexibleType => Some (ft.original)
3495
- case _ => None
3489
+ FlexibleType (OrNull (tp), tp)
3496
3490
}
3497
3491
}
3498
3492
@@ -5998,7 +5992,7 @@ object Types extends TypeUtils {
5998
5992
case tp : AnnotatedType =>
5999
5993
samClass(tp.underlying)
6000
5994
case tp : FlexibleType =>
6001
- samClass(tp.superType )
5995
+ samClass(tp.underlying )
6002
5996
case _ =>
6003
5997
NoSymbol
6004
5998
@@ -6129,8 +6123,8 @@ object Types extends TypeUtils {
6129
6123
tp.derivedJavaArrayType(elemtp)
6130
6124
protected def derivedExprType (tp : ExprType , restpe : Type ): Type =
6131
6125
tp.derivedExprType(restpe)
6132
- protected def derivedFlexibleType (tp : FlexibleType , under : Type ): Type =
6133
- tp.derivedFlexibleType(under )
6126
+ protected def derivedFlexibleType (tp : FlexibleType , hi : Type ): Type =
6127
+ tp.derivedFlexibleType(hi )
6134
6128
// note: currying needed because Scala2 does not support param-dependencies
6135
6129
protected def derivedLambdaType (tp : LambdaType )(formals : List [tp.PInfo ], restpe : Type ): Type =
6136
6130
tp.derivedLambdaType(tp.paramNames, formals, restpe)
@@ -6255,7 +6249,7 @@ object Types extends TypeUtils {
6255
6249
derivedOrType(tp, this (tp.tp1), this (tp.tp2))
6256
6250
6257
6251
case tp : FlexibleType =>
6258
- derivedFlexibleType(tp, this (tp.underlying ))
6252
+ derivedFlexibleType(tp, this (tp.hi ))
6259
6253
6260
6254
case tp : MatchType =>
6261
6255
val bound1 = this (tp.bound)
@@ -6544,14 +6538,17 @@ object Types extends TypeUtils {
6544
6538
if (underlying.isExactlyNothing) underlying
6545
6539
else tp.derivedAnnotatedType(underlying, annot)
6546
6540
}
6547
- override protected def derivedFlexibleType (tp : FlexibleType , underlying : Type ): Type =
6548
- underlying match {
6541
+
6542
+ override protected def derivedFlexibleType (tp : FlexibleType , hi : Type ): Type =
6543
+ hi match {
6549
6544
case Range (lo, hi) =>
6550
- range(tp.derivedFlexibleType(lo), tp.derivedFlexibleType(hi))
6545
+ // We know FlexibleType(t).hi = t and FlexibleType(t).lo = OrNull(t)
6546
+ range(OrNull (lo), hi)
6551
6547
case _ =>
6552
- if (underlying .isExactlyNothing) underlying
6553
- else tp.derivedFlexibleType(underlying )
6548
+ if (hi .isExactlyNothing) hi
6549
+ else tp.derivedFlexibleType(hi )
6554
6550
}
6551
+
6555
6552
override protected def derivedCapturingType (tp : Type , parent : Type , refs : CaptureSet ): Type =
6556
6553
parent match // TODO ^^^ handle ranges in capture sets as well
6557
6554
case Range (lo, hi) =>
0 commit comments