Skip to content

Commit 08889fe

Browse files
committed
Add a minimisation for joining OrTypes in TupleN test
1 parent 9ea195f commit 08889fe

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ object PatternMatcher {
336336

337337
if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
338338
def tupleSel(sym: Symbol) = ref(scrutinee).select(sym)
339-
val isGenericTuple = defn.isTupleClass(caseClass) && !defn.isTupleNType(tree.tpe)
339+
val isGenericTuple = defn.isTupleClass(caseClass) &&
340+
!defn.isTupleNType(tree.tpe match { case tp: OrType => tp.join case tp => tp }) // widen even hard unions, to see if it's a union of tuples
340341
val components = if isGenericTuple then caseAccessors.indices.toList.map(tupleApp(_, ref(scrutinee))) else caseAccessors.map(tupleSel)
341342
matchArgsPlan(components, args, onSuccess)
342343
else if (unapp.tpe <:< (defn.BooleanType))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// scalac: -Werror
2+
class Test:
3+
type Foo = Option[String] | Option[Int]
4+
5+
def test(foo: Foo) =
6+
val (_, foo2: Foo) = // was: the type test for Test.this.Foo cannot be checked at runtime
7+
foo match
8+
case Some(s: String) => ((), s)
9+
case _ => ((), 0)
10+
foo2

0 commit comments

Comments
 (0)