Skip to content

Commit d95a239

Browse files
committed
WIP: Try to simplify widenInferred algorithm
1 parent f484851 commit d95a239

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ trait ConstraintHandling[AbstractContext] {
308308
widenOr(widenSingle(tp)).dropRepeatedAnnot
309309
}
310310

311+
def widenInferred2(inst: Type, param: TypeParamRef)(implicit actx: AbstractContext): Type = {
312+
def widenSingle(tp: Type) = {
313+
val tpw = tp.widen
314+
if ((tpw ne tp) && tpw <:< param) tpw else tp
315+
}
316+
def widenOr(tp: Type) = {
317+
val tpw = tp.widenUnion
318+
if ((tpw ne tp) && tpw <:< param) tpw else tp
319+
}
320+
widenOr(widenSingle(inst)).dropRepeatedAnnot
321+
}
322+
311323
/** The instance type of `param` in the current constraint (which contains `param`).
312324
* If `fromBelow` is true, the instance type is the lub of the parameter's
313325
* lower bounds; otherwise it is the glb of its upper bounds. However,
@@ -316,7 +328,7 @@ trait ConstraintHandling[AbstractContext] {
316328
*/
317329
def instanceType(param: TypeParamRef, fromBelow: Boolean)(implicit actx: AbstractContext): Type = {
318330
val inst = approximation(param, fromBelow).simplified
319-
if (fromBelow) widenInferred(inst, constraint.fullUpperBound(param)) else inst
331+
if (fromBelow) widenInferred2(inst, /*constraint.fullUpperBound*/(param)) else inst
320332
}
321333

322334
/** Constraint `c1` subsumes constraint `c2`, if under `c2` as constraint we have

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
15251525
/** The greatest lower bound of a list types */
15261526
final def glb(tps: List[Type]): Type = ((AnyType: Type) /: tps)(glb)
15271527

1528+
def widenInUnions(implicit ctx: Context): Boolean = ctx.scala2Mode || ctx.erasedTypes || true
1529+
15281530
/** The least upper bound of two types
15291531
* @param canConstrain If true, new constraints might be added to simplify the lub.
15301532
* @note We do not admit singleton types in or-types as lubs.
@@ -1536,7 +1538,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
15361538
if ((tp1 isRef AnyClass) || (tp1 isRef AnyKindClass) || (tp2 isRef NothingClass)) return tp1
15371539
if ((tp2 isRef AnyClass) || (tp2 isRef AnyKindClass) || (tp1 isRef NothingClass)) return tp2
15381540
val atoms1 = tp1.atoms
1539-
if (atoms1.nonEmpty && !ctx.scala2Mode && !ctx.erasedTypes) {
1541+
if (atoms1.nonEmpty && !widenInUnions) {
15401542
val atoms2 = tp2.atoms
15411543
if (atoms2.nonEmpty) {
15421544
if (atoms1.subsetOf(atoms2)) return tp2
@@ -1550,8 +1552,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
15501552
val t2 = mergeIfSuper(tp2, tp1, canConstrain)
15511553
if (t2.exists) return t2
15521554

1553-
def widen(tp: Type) =
1554-
if (ctx.scala2Mode || ctx.erasedTypes) tp.widen else tp.widenIfUnstable
1555+
def widen(tp: Type) = if (widenInUnions) tp.widen else tp.widenIfUnstable
15551556
val tp1w = widen(tp1)
15561557
val tp2w = widen(tp2)
15571558
if ((tp1 ne tp1w) || (tp2 ne tp2w)) lub(tp1w, tp2w)

0 commit comments

Comments
 (0)