Skip to content

Commit c245d33

Browse files
committed
Fix CI: add facility to infer union and singleton types
- infer singleton types: tests/patmat/i4227.scala - infer union types: tests/patmat/i9190.scala
1 parent 149d4de commit c245d33

File tree

5 files changed

+13
-23
lines changed

5 files changed

+13
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ trait ConstraintHandling {
371371
* a lower bound instantiation can be a singleton type only if the upper bound
372372
* is also a singleton type.
373373
*/
374-
def instanceType(param: TypeParamRef, fromBelow: Boolean)(using Context): Type = {
374+
def instanceType(param: TypeParamRef, fromBelow: Boolean, widen: Boolean = true)(using Context): Type = {
375375
val approx = approximation(param, fromBelow).simplified
376-
if fromBelow then
376+
if fromBelow && widen then
377377
val widened = widenInferred(approx, param)
378378
// Widening can add extra constraints, in particular the widened type might
379379
// be a type variable which is now instantiated to `param`, and therefore

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,8 +2601,8 @@ object TypeComparer {
26012601
def subtypeCheckInProgress(using Context): Boolean =
26022602
comparing(_.subtypeCheckInProgress)
26032603

2604-
def instanceType(param: TypeParamRef, fromBelow: Boolean)(using Context): Type =
2605-
comparing(_.instanceType(param, fromBelow))
2604+
def instanceType(param: TypeParamRef, fromBelow: Boolean, widen: Boolean = true)(using Context): Type =
2605+
comparing(_.instanceType(param, fromBelow, widen))
26062606

26072607
def approximation(param: TypeParamRef, fromBelow: Boolean)(using Context): Type =
26082608
comparing(_.approximation(param, fromBelow))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,8 +4287,8 @@ object Types {
42874287
* instantiation can be a singleton type only if the upper bound
42884288
* is also a singleton type.
42894289
*/
4290-
def instantiate(fromBelow: Boolean)(using Context): Type =
4291-
instantiateWith(avoidCaptures(TypeComparer.instanceType(origin, fromBelow)))
4290+
def instantiate(fromBelow: Boolean, widen: Boolean = true)(using Context): Type =
4291+
instantiateWith(avoidCaptures(TypeComparer.instanceType(origin, fromBelow, widen)))
42924292

42934293
/** For uninstantiated type variables: Is the lower bound different from Nothing? */
42944294
def hasLowerBound(using Context): Boolean =

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,6 @@ class SpaceEngine(using Context) extends SpaceLogic {
527527
/** Parameter types of the case class type `tp`. Adapted from `unapplyPlan` in patternMatcher */
528528
def signature(unapp: TermRef, scrutineeTp: Type, argLen: Int): List[Type] = {
529529
val unappSym = unapp.symbol
530-
def caseClass = unappSym.owner.linkedClass
531-
532-
// println("scrutineeTp = " + scrutineeTp.show)
533-
534-
lazy val caseAccessors = caseClass.caseAccessors.filter(_.is(Method))
535530

536531
def isSyntheticScala2Unapply(sym: Symbol) =
537532
sym.isAllOf(SyntheticCase) && sym.owner.is(Scala2x)
@@ -543,10 +538,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
543538
val tvars = pt.paramInfos.map(newTypeVar)
544539
val mt = pt.instantiate(tvars).asInstanceOf[MethodType]
545540
scrutineeTp <:< mt.paramInfos(0)
546-
// force type inference to infer a narrower type: could be singleton
547-
// see tests/patmat/i4227.scala
548-
mt.paramInfos(0) <:< scrutineeTp
549-
isFullyDefined(mt, ForceDegree.all)
541+
isFullyDefined(mt, ForceDegree.all, widen = false)
550542
mt
551543
}
552544
}
@@ -564,9 +556,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
564556
val resTp = mt.finalResultType
565557

566558
val sig =
567-
if (isSyntheticScala2Unapply(unappSym) && caseAccessors.length == argLen)
568-
caseAccessors.map(_.info.asSeenFrom(mt.paramInfos.head, caseClass).widenExpr)
569-
else if (resTp.isRef(defn.BooleanClass))
559+
if (resTp.isRef(defn.BooleanClass))
570560
List()
571561
else {
572562
val isUnapplySeq = unappSym.name == nme.unapplySeq
@@ -584,7 +574,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
584574
if (arity > 0)
585575
productSelectorTypes(resTp, unappSym.srcPos)
586576
else {
587-
val getTp = resTp.select(nme.get).finalResultType.widen
577+
val getTp = resTp.select(nme.get).finalResultType.widenTermRefExpr
588578
if (argLen == 1) getTp :: Nil
589579
else productSelectorTypes(getTp, unappSym.srcPos)
590580
}

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ object Inferencing {
3131
* but only if the overall result of `isFullyDefined` is `true`.
3232
* Variables that are successfully minimized do not count as uninstantiated.
3333
*/
34-
def isFullyDefined(tp: Type, force: ForceDegree.Value)(using Context): Boolean = {
34+
def isFullyDefined(tp: Type, force: ForceDegree.Value, widen: Boolean = true)(using Context): Boolean = {
3535
val nestedCtx = ctx.fresh.setNewTyperState()
36-
val result = new IsFullyDefinedAccumulator(force)(using nestedCtx).process(tp)
36+
val result = new IsFullyDefinedAccumulator(force, widen = widen)(using nestedCtx).process(tp)
3737
if (result) nestedCtx.typerState.commit()
3838
result
3939
}
@@ -109,11 +109,11 @@ object Inferencing {
109109
* 2nd Phase: If first phase was successful, instantiate all remaining type variables
110110
* to their upper bound.
111111
*/
112-
private class IsFullyDefinedAccumulator(force: ForceDegree.Value, minimizeSelected: Boolean = false)
112+
private class IsFullyDefinedAccumulator(force: ForceDegree.Value, minimizeSelected: Boolean = false, widen: Boolean = true)
113113
(using Context) extends TypeAccumulator[Boolean] {
114114

115115
private def instantiate(tvar: TypeVar, fromBelow: Boolean): Type = {
116-
val inst = tvar.instantiate(fromBelow)
116+
val inst = tvar.instantiate(fromBelow, widen)
117117
typr.println(i"forced instantiation of ${tvar.origin} = $inst")
118118
inst
119119
}

0 commit comments

Comments
 (0)