@@ -109,6 +109,10 @@ object Applications {
109109 if (isValid) elemTp else NoType
110110 }
111111
112+ def namedTupleOrProductTypes (tp : Type )(using Context ): List [Type ] =
113+ if tp.isNamedTupleType then tp.namedTupleElementTypes.map(_(1 ))
114+ else productSelectorTypes(tp, NoSourcePosition )
115+
112116 def productSelectorTypes (tp : Type , errorPos : SrcPos )(using Context ): List [Type ] = {
113117 val sels = for (n <- Iterator .from(0 )) yield extractorMemberType(tp, nme.selectorName(n), errorPos)
114118 sels.takeWhile(_.exists).toList
@@ -177,9 +181,14 @@ object Applications {
177181 else fallback
178182
179183 private def tryAdaptPatternArgs (elems : List [untpd.Tree ], pt : Type )(using Context ): Option [List [untpd.Tree ]] =
180- tryEither[Option [List [untpd.Tree ]]]
181- (Some (desugar.adaptPatternArgs(elems, pt)))
182- ((_, _) => None )
184+ namedTupleOrProductTypes(pt) match
185+ case List (defn.NamedTuple (_, _))=>
186+ // if the product types list is a singleton named tuple, autotupling might be applied, so don't fail eagerly
187+ tryEither[Option [List [untpd.Tree ]]]
188+ (Some (desugar.adaptPatternArgs(elems, pt)))
189+ ((_, _) => None )
190+ case pts =>
191+ Some (desugar.adaptPatternArgs(elems, pt))
183192
184193 private def getUnapplySelectors (tp : Type )(using Context ): List [Type ] =
185194 // We treat patterns as product elements if
@@ -199,7 +208,7 @@ object Applications {
199208 else tp :: Nil
200209
201210 private def productUnapplySelectors (tp : Type )(using Context ): Option [List [Type ]] =
202- if defn.isProductSubType(tp) then
211+ if defn.isProductSubType(tp) && args.lengthCompare(productArity(tp)) <= 0 then
203212 tryAdaptPatternArgs(args, tp) match
204213 case Some (args1) if isProductMatch(tp, args1.length, pos) =>
205214 args = args1
0 commit comments