Skip to content

Commit 58575f9

Browse files
authored
Merge pull request #7835 from dotty-staging/fix-#7807
Fix #7807: Add case for MatchTypes to ApproximatingTypeMap
2 parents 0254b1b + 2662cdc commit 58575f9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3970,7 +3970,6 @@ object Types {
39703970
* and `X_1,...X_n` are the type variables bound in `patternType`
39713971
*/
39723972
abstract case class MatchType(bound: Type, scrutinee: Type, cases: List[Type]) extends CachedProxyType with ValueType {
3973-
39743973
def derivedMatchType(bound: Type, scrutinee: Type, cases: List[Type])(implicit ctx: Context): MatchType =
39753974
if (bound.eq(this.bound) && scrutinee.eq(this.scrutinee) && cases.eqElements(this.cases)) this
39763975
else MatchType(bound, scrutinee, cases)
@@ -4949,6 +4948,15 @@ object Types {
49494948
override protected def derivedWildcardType(tp: WildcardType, bounds: Type): WildcardType =
49504949
tp.derivedWildcardType(rangeToBounds(bounds))
49514950

4951+
override protected def derivedMatchType(tp: MatchType, bound: Type, scrutinee: Type, cases: List[Type]): Type =
4952+
bound match
4953+
case Range(lo, hi) =>
4954+
range(derivedMatchType(tp, lo, scrutinee, cases), derivedMatchType(tp, hi, scrutinee, cases))
4955+
case _ =>
4956+
scrutinee match
4957+
case Range(lo, hi) => range(bound.bounds.lo, bound.bounds.hi)
4958+
case _ => tp.derivedMatchType(bound, scrutinee, cases)
4959+
49524960
override protected def derivedSkolemType(tp: SkolemType, info: Type): Type = info match {
49534961
case Range(lo, hi) =>
49544962
range(tp.derivedSkolemType(lo), tp.derivedSkolemType(hi))

tests/pos/i7807.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test with
2+
3+
def flip: (x: 0 | 1) => x.type match { case 0 => 1 case 1 => 0 } = ???
4+
5+
flip(0): 1
6+
flip(1): 0
7+
8+
flip(if ??? then 0 else 1)
9+
val n: 0 | 1 = if ??? then 0 else 1
10+
flip(n)
11+
12+
val m: n.type match { case 0 => 1 case 1 => 0 } = flip(n)
13+
14+
// The following do not work, see discussion in https://github.com/lampepfl/dotty/pull/7835/files/6e60814e69be5c8d60265d4ce4bc1758863c23d8#r361741296:
15+
// flip(m)
16+
// flip(flip(n))

0 commit comments

Comments
 (0)