Skip to content

Commit 9729df4

Browse files
committed
Move logic, handle FromJavaObject in glb and lub as well
1 parent b6eddaa commit 9729df4

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

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

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,33 +1505,12 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
15051505
false
15061506
}
15071507

1508-
// This & will try to preserve the FromJavaObjects type in upper bounds
1509-
// For example, (? <: FromJavaObjects | Null) & (? <: Any),
1510-
// we want to get (? <: FromJavaObjects | Null) intead of (? <: Any),
1511-
// because we may check FromJavaObjects | Null <:< Object | Null later.
1512-
def bounds_&(tp1: TypeBounds, tp2: TypeBounds) =
1513-
if tp1.hi.containsFromJavaObject
1514-
&& (tp1.hi frozen_<:< tp2.hi)
1515-
&& (tp2.lo frozen_<:< tp1.lo) then
1516-
// FromJavaObject in tp1.hi guarantees tp2.hi <:< tp1.hi
1517-
// prefer tp1 if FromJavaObject is in its hi
1518-
tp1
1519-
else if tp2.hi.containsFromJavaObject
1520-
&& (tp2.hi frozen_<:< tp1.hi)
1521-
&& (tp1.lo frozen_<:< tp2.lo) then
1522-
// Similarly, prefer tp2 if FromJavaObject is in its hi
1523-
tp2
1524-
else
1525-
// Use regular & to solve other cases
1526-
tp1 & tp2
1527-
15281508
def isSubArg(arg1: Type, arg2: Type): Boolean = arg2 match {
15291509
case arg2: TypeBounds =>
15301510
val arg1norm = arg1 match {
15311511
case arg1: TypeBounds =>
15321512
tparam match {
1533-
case tparam: Symbol =>
1534-
bounds_&(arg1, paramBounds(tparam))
1513+
case tparam: Symbol => arg1 & paramBounds(tparam)
15351514
case _ => arg1 // This case can only arise when a hk-type is illegally instantiated with a wildcard
15361515
}
15371516
case _ => arg1
@@ -2062,6 +2041,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
20622041
else if (!tp2.exists) tp1
20632042
else if tp1.isAny && !tp2.isLambdaSub || tp1.isAnyKind || isBottom(tp2) then tp2
20642043
else if tp2.isAny && !tp1.isLambdaSub || tp2.isAnyKind || isBottom(tp1) then tp1
2044+
else if tp1.isFromJavaObject && !tp2.isLambdaSub then tp2
2045+
else if tp2.isFromJavaObject && !tp1.isLambdaSub then tp1
20652046
else tp2 match
20662047
case tp2: LazyRef =>
20672048
glb(tp1, tp2.ref)
@@ -2110,6 +2091,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
21102091
if (tp1 eq tp2) tp1
21112092
else if (!tp1.exists) tp1
21122093
else if (!tp2.exists) tp2
2094+
else if tp1.isFromJavaObject && !tp2.isLambdaSub then tp1
2095+
else if tp2.isFromJavaObject && !tp1.isLambdaSub then tp2
21132096
else if tp1.isAny && !tp2.isLambdaSub || tp1.isAnyKind || isBottom(tp2) then tp1
21142097
else if tp2.isAny && !tp1.isLambdaSub || tp2.isAnyKind || isBottom(tp1) then tp2
21152098
else

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4938,8 +4938,23 @@ object Types {
49384938
}
49394939

49404940
def & (that: TypeBounds)(using Context): TypeBounds =
4941-
if ((this.lo frozen_<:< that.lo) && (that.hi frozen_<:< this.hi)) that
4942-
else if ((that.lo frozen_<:< this.lo) && (this.hi frozen_<:< that.hi)) this
4941+
// This will try to preserve the FromJavaObjects type in upper bounds.
4942+
// For example, (? <: FromJavaObjects | Null) & (? <: Any),
4943+
// we want to get (? <: FromJavaObjects | Null) intead of (? <: Any),
4944+
// because we may check the result <:< (? <: Object | Null) later.
4945+
if this.hi.containsFromJavaObject
4946+
&& (this.hi frozen_<:< that.hi)
4947+
&& (that.lo frozen_<:< this.lo) then
4948+
// FromJavaObject in tp1.hi guarantees tp2.hi <:< tp1.hi
4949+
// prefer tp1 if FromJavaObject is in its hi
4950+
this
4951+
else if that.hi.containsFromJavaObject
4952+
&& (that.hi frozen_<:< this.hi)
4953+
&& (this.lo frozen_<:< that.lo) then
4954+
// Similarly, prefer tp2 if FromJavaObject is in its hi
4955+
that
4956+
else if (this.lo frozen_<:< that.lo) && (that.hi frozen_<:< this.hi) then that
4957+
else if (that.lo frozen_<:< this.lo) && (this.hi frozen_<:< that.hi) then this
49434958
else TypeBounds(this.lo | that.lo, this.hi & that.hi)
49444959

49454960
def | (that: TypeBounds)(using Context): TypeBounds =

0 commit comments

Comments
 (0)