@@ -343,7 +343,13 @@ object PatternMatcher {
343
343
receiver.ensureConforms(defn.NonEmptyTupleTypeRef ), // If scrutinee is a named tuple, cast to underlying tuple
344
344
Literal (Constant (i)))
345
345
346
- if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
346
+ def getOfGetMatch (gm : Tree ) = gm.select(nme.get, _.info.isParameterless)
347
+ // Disable Scala2Unapply optimization if the argument is a named argument for a single-element named tuple to
348
+ // enable selecting the field. See i23131.scala for test cases.
349
+ val wasUnaryNamedTupleSelectArgForNamedTuple =
350
+ args.length == 1 && args.head.removeAttachment(FirstTransform .WasNamedArg ).isDefined &&
351
+ isGetMatch(unappType) && getOfGetMatch(unapp).tpe.widenDealias.isNamedTupleType
352
+ if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length && ! wasUnaryNamedTupleSelectArgForNamedTuple)
347
353
def tupleSel (sym : Symbol ) =
348
354
// If scrutinee is a named tuple, cast to underlying tuple, so that we can
349
355
// continue to select with _1, _2, ...
@@ -376,7 +382,7 @@ object PatternMatcher {
376
382
else {
377
383
assert(isGetMatch(unappType))
378
384
val argsPlan = {
379
- val get = ref(unappResult).select(nme.get, _.info.isParameterless )
385
+ val get = getOfGetMatch( ref(unappResult))
380
386
val arity = productArity(get.tpe.stripNamedTuple, unapp.srcPos)
381
387
if (isUnapplySeq)
382
388
letAbstract(get) { getResult =>
@@ -386,17 +392,14 @@ object PatternMatcher {
386
392
}
387
393
else
388
394
letAbstract(get) { getResult =>
389
- def isUnaryNamedTupleSelectArg (arg : Tree ) =
390
- get.tpe.widenDealias.isNamedTupleType
391
- && arg.removeAttachment(FirstTransform .WasNamedArg ).isDefined
392
395
// Special case: Normally, we pull out the argument wholesale if
393
396
// there is only one. But if the argument is a named argument for
394
397
// a single-element named tuple, we have to select the field instead.
395
398
// NamedArg trees are eliminated in FirstTransform but for named arguments
396
399
// of patterns we add a WasNamedArg attachment, which is used to guide the
397
400
// logic here. See i22900.scala for test cases.
398
401
val selectors = args match
399
- case arg :: Nil if ! isUnaryNamedTupleSelectArg(arg) =>
402
+ case arg :: Nil if ! wasUnaryNamedTupleSelectArgForNamedTuple =>
400
403
ref(getResult) :: Nil
401
404
case _ =>
402
405
productSelectors(getResult.info).map(ref(getResult).select(_))
0 commit comments