Skip to content

Commit ee6d240

Browse files
committed
Fix tuple selection in pattern matcher
The pattern matcher selects tuples up to 22 using _1, _2, ... But if the scrutinee is a named tuple this only works if it is cast to a regular tuple first.
1 parent 31381e1 commit ee6d240

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,20 @@ object PatternMatcher {
347347
def tupleApp(i: Int, receiver: Tree) = // manually inlining the call to NonEmptyTuple#apply, because it's an inline method
348348
ref(defn.RuntimeTuplesModule)
349349
.select(defn.RuntimeTuples_apply)
350-
.appliedTo(receiver, Literal(Constant(i)))
350+
.appliedTo(
351+
receiver.ensureConforms(defn.NonEmptyTupleTypeRef), // If scrutinee is a named tuple, cast to underlying tuple
352+
Literal(Constant(i)))
351353

352354
if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
353-
def tupleSel(sym: Symbol) = ref(scrutinee).select(sym)
355+
def tupleSel(sym: Symbol) =
356+
// If scrutinee is a named tuple, cast to underlying tuple, so that we can
357+
// continue to select with _1, _2, ...
358+
ref(scrutinee).ensureConforms(scrutinee.info.stripNamedTuple).select(sym)
354359
val isGenericTuple = defn.isTupleClass(caseClass) &&
355360
!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
356-
val components = if isGenericTuple then caseAccessors.indices.toList.map(tupleApp(_, ref(scrutinee))) else caseAccessors.map(tupleSel)
361+
val components =
362+
if isGenericTuple then caseAccessors.indices.toList.map(tupleApp(_, ref(scrutinee)))
363+
else caseAccessors.map(tupleSel)
357364
matchArgsPlan(components, args, onSuccess)
358365
else if unappType.isRef(defn.BooleanClass) then
359366
TestPlan(GuardTest, unapp, unapp.span, onSuccess)

0 commit comments

Comments
 (0)