Skip to content

Commit 85c3ea2

Browse files
committed
Make scala#19607 work, although with a (minor) change of spec.
1 parent 5c628d9 commit 85c3ea2

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,29 +3410,34 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
34103410
// Actual matching logic
34113411

34123412
val instances = Array.fill[Type](spec.captureCount)(NoType)
3413+
val noInstances = mutable.ListBuffer.empty[(TypeName, TypeBounds)]
34133414

34143415
def rec(pattern: MatchTypeCasePattern, scrut: Type, variance: Int, scrutIsWidenedAbstract: Boolean): Boolean =
34153416
pattern match
34163417
case MatchTypeCasePattern.Capture(num, isWildcard) =>
3418+
def addNoInstances(bounds: TypeBounds): TypeBounds =
3419+
noInstances += spec.origMatchCase.paramNames(num) -> bounds
3420+
bounds
3421+
34173422
instances(num) = scrut match
34183423
case scrut: TypeBounds =>
34193424
if isWildcard then
34203425
// anything will do, as long as it conforms to the bounds for the subsequent `scrut <:< instantiatedPat` test
3421-
scrut.hi
3426+
scrut
34223427
else if scrutIsWidenedAbstract then
34233428
// always keep the TypeBounds so that we can report the correct NoInstances
3424-
scrut
3429+
addNoInstances(scrut)
34253430
else
34263431
variance match
34273432
case 1 => scrut.hi
34283433
case -1 => scrut.lo
3429-
case 0 => scrut
3434+
case 0 => addNoInstances(scrut)
34303435
case _ =>
34313436
if !isWildcard && scrutIsWidenedAbstract && variance != 0 then
34323437
// force a TypeBounds to report the correct NoInstances
34333438
// the Nothing and Any bounds are used so that they are not displayed; not for themselves in particular
3434-
if variance > 0 then TypeBounds(defn.NothingType, scrut)
3435-
else TypeBounds(scrut, defn.AnyType)
3439+
if variance > 0 then addNoInstances(TypeBounds(defn.NothingType, scrut))
3440+
else addNoInstances(TypeBounds(scrut, defn.AnyType))
34363441
else
34373442
scrut
34383443
!instances(num).isError
@@ -3507,24 +3512,31 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
35073512
else
35083513
MatchResult.Stuck
35093514

3515+
//println(spec.pattern)
35103516
if rec(spec.pattern, scrut, variance = 1, scrutIsWidenedAbstract = false) then
3511-
if instances.exists(_.isInstanceOf[TypeBounds]) then
3512-
MatchResult.NoInstance {
3517+
if noInstances.nonEmpty then
3518+
//println(1)
3519+
MatchResult.NoInstance(noInstances.toList)
3520+
/* {
35133521
constrainedCaseLambda.paramNames.zip(instances).collect {
35143522
case (name, bounds: TypeBounds) => (name, bounds)
35153523
}
3516-
}
3524+
}*/
35173525
else
35183526
val defn.MatchCase(instantiatedPat, reduced) =
35193527
instantiateParamsSpec(instances, constrainedCaseLambda)(constrainedCaseLambda.resultType): @unchecked
3528+
//println(i"$scrut <:< $instantiatedPat")
35203529
if scrut <:< instantiatedPat then
3530+
//println(2)
35213531
if disjoint then
35223532
MatchResult.ReducedAndDisjoint
35233533
else
35243534
MatchResult.Reduced(reduced)
35253535
else
3536+
//println(3)
35263537
tryDisjoint
35273538
else
3539+
//println(4)
35283540
tryDisjoint
35293541
end matchSpeccedPatMat
35303542

tests/pos/i19607.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait Foo
2+
trait Bar[T]
3+
4+
type MatchType[T] = T match
5+
case Bar[?] => Nothing
6+
case _ => T
7+
8+
object Test:
9+
def foo(b: Bar[? >: Foo]): MatchType[b.type] = ???
10+
11+
def bar(b: Bar[? >: Foo]): Nothing = foo(b)
12+
end Test

0 commit comments

Comments
 (0)