diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 50e01e0f2c68..e78cd15aa706 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -3966,7 +3966,6 @@ object Types { * and `X_1,...X_n` are the type variables bound in `patternType` */ abstract case class MatchType(bound: Type, scrutinee: Type, cases: List[Type]) extends CachedProxyType with ValueType { - def derivedMatchType(bound: Type, scrutinee: Type, cases: List[Type])(implicit ctx: Context): MatchType = if (bound.eq(this.bound) && scrutinee.eq(this.scrutinee) && cases.eqElements(this.cases)) this else MatchType(bound, scrutinee, cases) @@ -4945,6 +4944,15 @@ object Types { override protected def derivedWildcardType(tp: WildcardType, bounds: Type): WildcardType = tp.derivedWildcardType(rangeToBounds(bounds)) + override protected def derivedMatchType(tp: MatchType, bound: Type, scrutinee: Type, cases: List[Type]): Type = + bound match + case Range(lo, hi) => + range(derivedMatchType(tp, lo, scrutinee, cases), derivedMatchType(tp, hi, scrutinee, cases)) + case _ => + scrutinee match + case Range(lo, hi) => range(bound.bounds.lo, bound.bounds.hi) + case _ => tp.derivedMatchType(bound, scrutinee, cases) + override protected def derivedSkolemType(tp: SkolemType, info: Type): Type = info match { case Range(lo, hi) => range(tp.derivedSkolemType(lo), tp.derivedSkolemType(hi)) diff --git a/tests/pos/i7807.scala b/tests/pos/i7807.scala new file mode 100644 index 000000000000..36193f26a972 --- /dev/null +++ b/tests/pos/i7807.scala @@ -0,0 +1,16 @@ +object Test with + + def flip: (x: 0 | 1) => x.type match { case 0 => 1 case 1 => 0 } = ??? + + flip(0): 1 + flip(1): 0 + + flip(if ??? then 0 else 1) + val n: 0 | 1 = if ??? then 0 else 1 + flip(n) + + val m: n.type match { case 0 => 1 case 1 => 0 } = flip(n) + + // The following do not work, see discussion in https://github.com/lampepfl/dotty/pull/7835/files/6e60814e69be5c8d60265d4ce4bc1758863c23d8#r361741296: + // flip(m) + // flip(flip(n))