-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for Java records in patterns. #19577
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
Draft
yilinwei
wants to merge
13
commits into
scala:main
Choose a base branch
from
yilinwei:java-record-patterns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
670664f
Add support for Java records in patterns.
yilinwei 89cf837
Add extra regression test for generic record.
yilinwei 24343bf
Check if run tests are working on the CI.
yilinwei 406c039
Add 0 arity test
yilinwei 7b44500
Add generic record run test.
yilinwei 0d277a1
First-class Java record pattern matching.
yilinwei 143d5b3
Add initial experiments on mirror support.
yilinwei d653059
Add back previous change.
yilinwei b749672
Change return type
yilinwei df184af
Tentative classfile changes
yilinwei 6da240b
Hack around with a flag for now.
yilinwei 3aa99e7
Switch over to using `ParamAccessor` as discriminant.
yilinwei 353d9d8
Adding step to reduce `unapply`.
yilinwei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,10 @@ object Applications { | |
ref.info.widenExpr.annotatedToRepeated | ||
} | ||
|
||
// TODO: Error here | ||
def isJavaRecordMatch(tp: Type, numArgs: Int, errorPos: SrcPos = NoSourcePosition)(using Context): Boolean = | ||
defn.isJavaRecordClass(tp.typeSymbol) | ||
|
||
/** Does `tp` fit the "product match" conditions as an unapply result type | ||
* for a pattern with `numArgs` subpatterns? | ||
* This is the case if `tp` has members `_1` to `_N` where `N == numArgs`. | ||
|
@@ -108,6 +112,20 @@ object Applications { | |
if (isValid) elemTp else NoType | ||
} | ||
|
||
def javaRecordComponentTypes(tp: Type, errorPos: SrcPos)(using Context): List[Type] = { | ||
|
||
val params = tp.typeSymbol.javaRecordComponents.map(_.info.resultType) | ||
|
||
tp match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is surely too standard to need to do it by hand... |
||
case tp: AppliedType => | ||
val argsIter = tp.args.iterator | ||
for (param <- params) yield param match | ||
case param if param.typeSymbol.isTypeParam => argsIter.next() | ||
case param => param | ||
case _ => params | ||
|
||
} | ||
|
||
def productSelectorTypes(tp: Type, errorPos: SrcPos)(using Context): List[Type] = { | ||
val sels = for (n <- Iterator.from(0)) yield extractorMemberType(tp, nme.selectorName(n), errorPos) | ||
sels.takeWhile(_.exists).toList | ||
|
@@ -192,6 +210,8 @@ object Applications { | |
// which is better than the message in `fail`. | ||
else if unapplyResult.derivesFrom(defn.NonEmptyTupleClass) then | ||
foldApplyTupleType(unapplyResult) | ||
else if (isJavaRecordMatch(unapplyResult, args.length, pos)) then | ||
javaRecordComponentTypes(unapplyResult, pos) | ||
else fail | ||
} | ||
} | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
object C: | ||
|
||
def useR0: Unit = | ||
val r = R0() | ||
|
||
// unapply in valdef | ||
val R0() = r | ||
|
||
// unapply in patmatch | ||
r match { | ||
case R0() => | ||
} | ||
|
||
|
||
def useR1: Int = | ||
val r = R1(1, "foo") | ||
|
||
// unapply in valdef | ||
val R1(i, _) = r | ||
val a: Int = i | ||
|
||
// unapply in patmatch | ||
r match { | ||
case R1(i, _) => i | ||
} | ||
|
||
def useR2: String = | ||
val r = R2("asd") | ||
r match { | ||
case R2(s) => s | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record R0() {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record R1(int i, String s) {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record R2<T>(T t) {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record R0() {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record R1(int i) {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record R2<T>(T t, int i) {} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// scalajs: --skip | ||
|
||
object Test: | ||
def main(args: Array[String]): Unit = | ||
val r0 = R0() | ||
r0 match | ||
case R0() => | ||
|
||
val r1 = R1(42) | ||
r1 match | ||
case R1(i) => assert(i == 42) | ||
|
||
val R1(i) = r1 | ||
assert(i == 42) | ||
|
||
val r2 = R2("foo", 9) | ||
val R2(s, _) = r2 | ||
assert(s == "foo") | ||
|
||
r2 match | ||
case R2(_, i) => assert(i == 9) |
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.
Uh oh!
There was an error while loading. Please reload this page.