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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aherlihy
Copy link
Contributor

Fixes #23131

From my understanding, for Scala 2 case classes, the compiler will generate an unapply that returns an Option[(...)] which contains the fields of the class as a tuple and this first case:

if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
def tupleSel(sym: Symbol) =
// If scrutinee is a named tuple, cast to underlying tuple, so that we can
// continue to select with _1, _2, ...
ref(scrutinee).ensureConforms(scrutinee.info.stripNamedTuple).select(sym)
val isGenericTuple = defn.isTupleClass(caseClass) &&
!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
val components =
if isGenericTuple then caseAccessors.indices.toList.map(tupleApp(_, ref(scrutinee)))
else caseAccessors.map(tupleSel)
matchArgsPlan(components, args, onSuccess)
is to bypass this unapply and directly select the case class parameters for performance. However that causes the fixes added by this PR: #22953 to be skipped since the check comes first. I think the simplest fix is to skip the Scala 2 special case for a single-element selector, but not 100% sure.

@aherlihy aherlihy requested a review from smarter July 25, 2025 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClassCastException when using named pattern one single-component named tuple
1 participant