-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
…lement named tuple in unapply
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smarter I added a more precise check but I'm not really sure this is the best way to do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can add a method for that.
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