[Scala3] Add support for handling unapplys in NamingPhase#5198
[Scala3] Add support for handling unapplys in NamingPhase#5198adkian-sifive merged 13 commits intomainfrom
Conversation
Move NamePluginSpec
| } | ||
|
|
||
| // For each synthetic tuple val, find the subsequent selecting ValDefs | ||
| syntheticTupleVals.foreach { case (syntheticName, arity) => |
There was a problem hiding this comment.
refactor this handling
There was a problem hiding this comment.
I'm unresolving this because I think this should be cleaned up.
| }.toMap | ||
|
|
||
| // Pattern to match tuple element accessors like _1, _2, etc. | ||
| val TupleIndex = "_([0-9]+)".r |
There was a problem hiding this comment.
I do not want regex matching in performance critical places like compiler plugins, can we do this a different way?
There was a problem hiding this comment.
Some ideas--Name has some methods that might be useful, like is(kind: NameKind). Alternatively, if we know the qual is of type tuple and that the name is the right kind of name (probably isTermName? or one of the name kinds), then we can potentially extract the number directly, or just .tail the String if we're sure.
There was a problem hiding this comment.
tuple accessor names are predictable, so we can just check for tuple types and extract accessor names manually based on the pattern. i'll try it out
There was a problem hiding this comment.
If there is an actual flag or type in the AST that indicates a tuple extraction that would be better though. It is not wise to rely on the names being predictable in case that predictability changes.
There was a problem hiding this comment.
Also we want to distinguish it from user code that might look similar, e.g. what happens if I write this:
val foo = returnsATuple(...)
val bar = foo._1If the plugin matches that, it's a bad thing.
Here's why, what if someone wrote this?
val foo @ (fizz, _) = returnsATuple(...)
// then later for some reason:
val bar = foo._1It's important that we actually distinguish what is an unapply and not just match the pattern of extractor names.
There was a problem hiding this comment.
yeah that's exactly the case i'm checking. this is the issue with unapplys in scala 3 in general though, iirc there's no way to tell between unapplys and tuple destructuring
There was a problem hiding this comment.
yeah that's exactly the case i'm checking. this is the issue with unapplys in scala 3 in general though, iirc there's no way to tell between unapplys and tuple destructuring
Is that true in Scala 3 in general or is it true because we're running as a later phase?
Also, what about Flags like Synthetic, Case, CaseAccessor?
There was a problem hiding this comment.
Pretty sure this was still an issue in the original naming PR and we were running earlier then
Regarding flags, these are used in helpers to filter stuff but these same flags are used in, for example, Match-based unapplys so on their own won't help here
Synthetic tuple val extraction is not O(n+m)
jackkoenig
left a comment
There was a problem hiding this comment.
I'm approving this to unblock moving tests over, but I still want several improvements, see comments but certainly including:
- Do
collectUnapplyNamesin a single traversal of the statements - Remove regex in collectUnapplyNames
- Clean up syntheicTupleValues stuff
- Get rid of String-y stuff in okVal
- Use built-ins for
isTupleTypeand probablytupleArity
| } | ||
|
|
||
| // For each synthetic tuple val, find the subsequent selecting ValDefs | ||
| syntheticTupleVals.foreach { case (syntheticName, arity) => |
There was a problem hiding this comment.
I'm unresolving this because I think this should be cleaned up.
Build mutable maps for tuples and selects
Missing feature from #4889
Tried to keep the implementation as logically similar as possible to the Scala 2 version
Contributor Checklist
docs/src?Type of Improvement
Desired Merge Strategy
Release Notes
NamingPhase updated to handle unapplys similar to Scala 2
Reviewer Checklist (only modified by reviewer)
3.6.x,5.x, or6.xdepending on impact, API modification or big change:7.0)?Enable auto-merge (squash)and clean up the commit message.Create a merge commit.