-
Notifications
You must be signed in to change notification settings - Fork 1.1k
i22587 Divergence check for Match Types #24661
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
Lluc24
wants to merge
2
commits into
scala:main
Choose a base branch
from
Lluc24:i22587-divergence-check-MT
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.
+125
−1
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Negative Test Case A: Direct Self-Reference Without Reduction | ||
| // This SHOULD diverge because Loop[Int] => Loop[Float] => Loop[Double] => Loop[String] => Loop[Int] => ... | ||
| // The type cycles without reaching a base case. | ||
|
|
||
| type Loop[X] = X match | ||
| case Int => Loop[Float] | ||
| case Float => Loop[Double] | ||
| case Double => Loop[String] | ||
| case String => Loop[Int] | ||
| case _ => String | ||
|
|
||
| @main def test02(): Unit = | ||
| val e1: Loop[Int] = ??? // error | ||
| println("Test 2 - Direct self-reference:") | ||
| println(s"e1 value: $e1") |
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,12 @@ | ||
| // Negative Test Case 2: Wrapping Type Without Progress | ||
| // This SHOULD diverge because Wrap[Int] => Wrap[List[Int]] => Wrap[List[List[Int]]] => ... | ||
| // The type grows infinitely without reaching a base case | ||
|
|
||
| type Wrap[X] = X match | ||
| case AnyVal => Wrap[List[X]] | ||
| case AnyRef => Wrap[List[X]] | ||
|
|
||
| @main def test03(): Unit = | ||
| val e1: Wrap[Int] = ??? // error | ||
| println("Test 3 - Wrapping without progress:") | ||
| println(s"e1 value: $e1") | ||
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,23 @@ | ||
| // Positive Test Case A: Simple Recursive Deconstruction | ||
| // This should NOT diverge because recursion always reduces the type structure | ||
| // by unwrapping one layer (Option[t] -> t) | ||
|
|
||
| type DoubleUnwrap[X, Y] = (X, Y) match | ||
| case (AnyVal, Y) => (X, Unwrap[Y]) | ||
| case (X, AnyVal) => (Unwrap[X], Y) | ||
| case (X, Y) => (Unwrap[X], Unwrap[Y]) | ||
|
|
||
| type Unwrap[X] = X match | ||
| case Option[t] => Unwrap[t] | ||
| case _ => X | ||
|
|
||
| @main def test01(): Unit = | ||
| val e1: Unwrap[Option[Option[Int]]] = 42 | ||
| println("Test 1 - Simple recursive unwrapping:") | ||
| println(s"e1 value: $e1") | ||
|
|
||
| val e2: DoubleUnwrap[Option[String], Option[Int]] = ("hello", 42) | ||
| println(s"e2 value: $e2") | ||
|
|
||
| val e3: DoubleUnwrap[Int, Int] = (1, 2) | ||
| println(s"e3 value: $e3") |
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,13 @@ | ||
| // Positive Test Case B: Binary Recursive construction | ||
| // This should NOT diverge because although types have same complexity, the structure | ||
| // changes by incrementing by one the numeric parameter until reaching a base case. | ||
|
|
||
|
|
||
| type Increase[X, Y, Z] = (X, Y, Z) match | ||
| case (X, Y, 0) => Increase[X, Y, 1] | ||
| case (X, 0, _) => Increase[X, 1, 0] | ||
| case (0, _, _) => Increase[1, 0, 0] | ||
| case _ => "done" | ||
|
|
||
| @main def test04(): Unit = | ||
| val e1: Increase[0, 0, 0] = "done" |
Oops, something went wrong.
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.
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.
List[Int]is not AnyVal , so then it stops there right?Uh oh!
There was an error while loading. Please reload this page.
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.
You're absolutely right. I've fixed the test case.
The divergence check currently applies even to match types that do not strictly reduce (stuck matches). Instead of simply stopping and returning
NoType(or the stuck type), the algorithm detects the increasing size of the scrutinee and correctly returns anErrorType.Thanks for the catch! I would have missed that otherwise.