Skip bypassing unapply for scala 2 case classes to allow for single-element named tuple in unapply#23603
Merged
aherlihy merged 5 commits intoscala:mainfrom Jul 30, 2025
Merged
Skip bypassing unapply for scala 2 case classes to allow for single-element named tuple in unapply#23603aherlihy merged 5 commits intoscala:mainfrom
aherlihy merged 5 commits intoscala:mainfrom
Conversation
…lement named tuple in unapply
smarter
approved these changes
Jul 29, 2025
Member
smarter
left a comment
There was a problem hiding this comment.
I think this is fine as a workaround to avoid the runtime crash now, but it really needs a FIXME comment explaining the context and calling for a refactor of the code.
aherlihy
commented
Jul 29, 2025
| // enable selecting the field. See i23131.scala for test cases. | ||
| val wasSingleNamedArgForNamedTuple = | ||
| args.length == 1 && args.head.removeAttachment(FirstTransform.WasNamedArg).isDefined && | ||
| isGetMatch(unappType) && unapp.select(nme.get, _.info.isParameterless).tpe.widenDealias.isNamedTupleType |
Contributor
Author
There was a problem hiding this comment.
@smarter I added a more precise check but I'm not really sure this is the best way to do it
Member
There was a problem hiding this comment.
I think this is fine, since .select(nme.get, _.info.isParameterless) appears both here and below in val get = ..., it could be factored out in a method getOfGetMatch or something like that.
Contributor
Author
There was a problem hiding this comment.
Sure, I can add a method for that.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
scala3/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Lines 346 to 356 in 6146b90