-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve GADT reasoning for pattern alternatives #23205
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aaeedd3
Find necessary GADT constraints for pattern alternatives
Linyxus 48518e1
Document & test GADT reasoning for pattern alternatives
Linyxus 860a9ee
Update test file
Linyxus ff3df3c
Update compiler/src/dotty/tools/dotc/typer/Typer.scala
Linyxus 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
enum Expr[+T]: | ||
case I1() extends Expr[Int] | ||
case I2() extends Expr[Int] | ||
case B() extends Expr[Boolean] | ||
import Expr.* | ||
def foo[T](e: Expr[T]): T = | ||
e match | ||
case I1() | I2() => 42 // ok | ||
case B() => true | ||
def bar[T](e: Expr[T]): T = | ||
e match | ||
case I1() | B() => 42 // error | ||
case I2() => 0 |
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 @@ | ||
enum Expr[+T]: | ||
case I1() extends Expr[Int] | ||
case I2() extends Expr[Int] | ||
case I3() extends Expr[Int] | ||
case I4() extends Expr[Int] | ||
case I5() extends Expr[Int] | ||
case B() extends Expr[Boolean] | ||
import Expr.* | ||
def test1[T](e: Expr[T]): T = | ||
e match | ||
case I1() | I2() | I3() | I4() | I5() => 42 // ok | ||
case B() => true | ||
def test2[T](e: Expr[T]): T = | ||
e match | ||
case I1() | I2() | I3() | I4() | I5() | B() => 42 // error |
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,34 @@ | ||
trait A | ||
trait B extends A | ||
trait C extends B | ||
trait D | ||
enum Expr[+T]: | ||
case IsA() extends Expr[A] | ||
case IsB() extends Expr[B] | ||
case IsC() extends Expr[C] | ||
case IsD() extends Expr[D] | ||
import Expr.* | ||
def test1[T](e: Expr[T]): T = e match | ||
case IsA() => new A {} | ||
case IsB() => new B {} | ||
case IsC() => new C {} | ||
def test2[T](e: Expr[T]): T = e match | ||
case IsA() | IsB() => | ||
// IsA() implies T >: A | ||
// IsB() implies T >: B | ||
// So T >: B is chosen | ||
new B {} | ||
case IsC() => new C {} | ||
def test3[T](e: Expr[T]): T = e match | ||
case IsA() | IsB() | IsC() => | ||
// T >: C is chosen | ||
new C {} | ||
def test4[T](e: Expr[T]): T = e match | ||
case IsA() | IsB() | IsC() => | ||
new B {} // error | ||
def test5[T](e: Expr[T]): T = e match | ||
case IsA() | IsB() => | ||
new A {} // error | ||
def test6[T](e: Expr[T]): T = e match | ||
case IsA() | IsC() | IsD() => | ||
new C {} // error |
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,19 @@ | ||
trait A | ||
trait B extends A | ||
trait C extends B | ||
enum Expr[T]: | ||
case IsA() extends Expr[A] | ||
case IsB() extends Expr[B] | ||
case IsC() extends Expr[C] | ||
import Expr.* | ||
def test1[T](e: Expr[T]): T = e match | ||
case IsA() => new A {} | ||
case IsB() => new B {} | ||
case IsC() => new C {} | ||
def test2[T](e: Expr[T]): T = e match | ||
case IsA() | IsB() => | ||
// IsA() implies T =:= A | ||
// IsB() implies T =:= B | ||
// No necessary constraint can be found | ||
new B {} // error | ||
case IsC() => new C {} |
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,14 @@ | ||
trait A | ||
trait B extends A | ||
trait C extends B | ||
enum Expr[-T]: | ||
case IsA() extends Expr[A] | ||
case IsB() extends Expr[B] | ||
case IsC() extends Expr[C] | ||
import Expr.* | ||
def test1[T](e: Expr[T]): Unit = e match | ||
case IsA() | IsB() => | ||
val t1: T = ??? | ||
val t2: A = t1 | ||
val t3: B = t1 // error | ||
case IsC() => |
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,8 @@ | ||
trait Document[Doc <: Document[Doc]] | ||
sealed trait Conversion[Doc, V] | ||
|
||
case class C[Doc <: Document[Doc]]() extends Conversion[Doc, Doc] | ||
|
||
def Test[Doc <: Document[Doc], V](conversion: Conversion[Doc, V]) = | ||
conversion match | ||
case C() | C() => ??? // error |
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.