Skip to content

Skip bypassing unapply for scala 2 case classes to allow for single-element named tuple in unapply #23603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,13 @@ object PatternMatcher {
receiver.ensureConforms(defn.NonEmptyTupleTypeRef), // If scrutinee is a named tuple, cast to underlying tuple
Literal(Constant(i)))

if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
def getOfGetMatch(gm: Tree) = gm.select(nme.get, _.info.isParameterless)
// Disable Scala2Unapply optimization if the argument is a named argument for a single-element named tuple to
// enable selecting the field. See i23131.scala for test cases.
val wasUnaryNamedTupleSelectArgForNamedTuple =
args.length == 1 && args.head.removeAttachment(FirstTransform.WasNamedArg).isDefined &&
isGetMatch(unappType) && getOfGetMatch(unapp).tpe.widenDealias.isNamedTupleType
if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length && !wasUnaryNamedTupleSelectArgForNamedTuple)
def tupleSel(sym: Symbol) =
// If scrutinee is a named tuple, cast to underlying tuple, so that we can
// continue to select with _1, _2, ...
Expand Down Expand Up @@ -376,7 +382,7 @@ object PatternMatcher {
else {
assert(isGetMatch(unappType))
val argsPlan = {
val get = ref(unappResult).select(nme.get, _.info.isParameterless)
val get = getOfGetMatch(ref(unappResult))
val arity = productArity(get.tpe.stripNamedTuple, unapp.srcPos)
if (isUnapplySeq)
letAbstract(get) { getResult =>
Expand All @@ -386,17 +392,14 @@ object PatternMatcher {
}
else
letAbstract(get) { getResult =>
def isUnaryNamedTupleSelectArg(arg: Tree) =
get.tpe.widenDealias.isNamedTupleType
&& arg.removeAttachment(FirstTransform.WasNamedArg).isDefined
// Special case: Normally, we pull out the argument wholesale if
// there is only one. But if the argument is a named argument for
// a single-element named tuple, we have to select the field instead.
// NamedArg trees are eliminated in FirstTransform but for named arguments
// of patterns we add a WasNamedArg attachment, which is used to guide the
// logic here. See i22900.scala for test cases.
val selectors = args match
case arg :: Nil if !isUnaryNamedTupleSelectArg(arg) =>
case arg :: Nil if !wasUnaryNamedTupleSelectArgForNamedTuple =>
ref(getResult) :: Nil
case _ =>
productSelectors(getResult.info).map(ref(getResult).select(_))
Expand Down
6 changes: 6 additions & 0 deletions tests/run/i23131.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.NamedTuple
@main
def Test =
Some((name = "Bob")) match {
case Some(name = a) => println(a)
}
Loading