-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Force MT reduction simplification, disallow infinite match types
- Loading branch information
Showing
12 changed files
with
179 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,7 @@ | ||
// like mt-recur.scala, but covariant | ||
class Cov[+T] | ||
|
||
type Recur[X] = X match | ||
case Int => Cov[Recur[X]] | ||
|
||
def x = ??? : Recur[Int] // error |
This file contains 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,10 @@ | ||
// an example of an infinite recursion match type | ||
// using an _invariant_ type constructor | ||
// see mt-recur.cov.scala for covariant | ||
// used to track the behaviour of match type reduction | ||
class Inv[T] | ||
|
||
type Recur[X] = X match | ||
case Int => Inv[Recur[X]] | ||
|
||
def x = ??? : Recur[Int] // error |
This file contains 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,35 @@ | ||
import scala.compiletime.ops.int.* | ||
|
||
object NatExample { | ||
sealed trait Nat | ||
object Nat { | ||
case object Zero extends Nat | ||
case class Succ[N <: Nat](prev: N) extends Nat | ||
|
||
given zero: Zero.type = Zero | ||
given buildSucc[N <: Nat](using n: N): Succ[N] = Succ(n) | ||
|
||
def value[N <: Nat](using n: N): N = n | ||
|
||
type FromInt[I <: Int] <: Nat = I match | ||
case 0 => Zero.type | ||
case _ => Succ[FromInt[I - 1]] | ||
|
||
summon[FromInt[0] =:= Zero.type] | ||
summon[FromInt[1] =:= Succ[Zero.type]] | ||
summon[FromInt[2] =:= Succ[Succ[Zero.type]]] | ||
summon[FromInt[3] =:= Succ[Succ[Succ[Zero.type]]]] | ||
summon[FromInt[4] =:= Succ[Succ[Succ[Succ[Zero.type]]]]] | ||
|
||
@main def test = { | ||
require(summon[FromInt[0]] == Zero) | ||
require(summon[FromInt[1]] == Succ(Zero)) | ||
require(summon[FromInt[2]] == Succ(Succ(Zero))) | ||
require(summon[FromInt[3]] == Succ(Succ(Succ(Zero)))) | ||
// we can summon 4 if we write it out: | ||
require(summon[Succ[Succ[Succ[Succ[Zero.type]]]]] == Succ(Succ(Succ(Succ(Zero))))) | ||
// was: we cannot summon 4 using the match type | ||
require(summon[FromInt[4]] == Succ(Succ(Succ(Succ(Zero))))) | ||
} | ||
} | ||
} |
This file contains 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,28 @@ | ||
import scala.compiletime.ops.int | ||
|
||
type Count0[N,T] <: Tuple = (N,T) match | ||
case (0,_) => EmptyTuple | ||
case (N,String) => String *: Count0[int.-[N, 1], String] | ||
case (N,Int) => Int *: Count0[int.-[N, 1], Int] | ||
case (N,Float) => Float *: Count0[int.-[N, 1], Float] | ||
case (N,Double) => Double *: Count0[int.-[N, 1], Double] | ||
|
||
|
||
type Count1[N,T] <: Tuple = (N,T) match | ||
case (0,T) => EmptyTuple | ||
case (N,String) => String *: Count1[int.-[N, 1], String] | ||
case (N,Int) => Int *: Count1[int.-[N, 1], Int] | ||
case (N,Float) => Float *: Count1[int.-[N, 1], Float] | ||
case (N,Double) => Double *: Count1[int.-[N, 1], Double] | ||
|
||
def t01 = summon[Count0[1, Int] =:= Int *: EmptyTuple ] | ||
def t02 = summon[Count0[2, Int] =:= Int *: Int *: EmptyTuple] | ||
def t03 = summon[Count0[3, Int] =:= Int *: Int *: Int *: EmptyTuple] | ||
def t04 = summon[Count0[4, Int] =:= Int *: Int *: Int *: Int *: EmptyTuple] | ||
def t05 = summon[Count0[5, Int] =:= Int *: Int *: Int *: Int *: Int *: EmptyTuple] | ||
|
||
def t11 = summon[Count1[1, Int] =:= Int *: EmptyTuple ] | ||
def t12 = summon[Count1[2, Int] =:= Int *: Int *: EmptyTuple] | ||
def t13 = summon[Count1[3, Int] =:= Int *: Int *: Int *: EmptyTuple] // was: Fail from here | ||
def t14 = summon[Count1[4, Int] =:= Int *: Int *: Int *: Int *: EmptyTuple] | ||
def t15 = summon[Count1[5, Int] =:= Int *: Int *: Int *: Int *: Int *: EmptyTuple] |
This file contains 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 @@ | ||
import scala.compiletime.ops.int, int.- | ||
|
||
type Count[N, T] <: Tuple = (N, T) match | ||
case (0, T) => EmptyTuple | ||
case (N, T) => T *: Count[N - 1, T] | ||
|
||
val a: Count[3, Int] = (1, 2, 3) | ||
val b: Count[4, Int] = (1, 2, 3, 4) | ||
val c: Count[5, Int] = (1, 2, 3, 4, 5) | ||
val d: Count[6, Int] = (1, 2, 3, 4, 5, 6) | ||
val z: Count[23, Int] = ( | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | ||
21, 22, 23) |
This file contains 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,16 @@ | ||
// scalac: -Yno-deep-subtypes:false | ||
// Minimisation of tests/run-macros/i17257 | ||
// to understand how changes to match type reduction | ||
// impacted this use of Tuple.IsMappedBy. | ||
// | ||
// During match type reduction | ||
// if we do NOT simplify the case lambda parameter instances | ||
// then this large tuple make TypeComparer breach LogPendingSubTypesThreshold | ||
// which, under -Yno-deep-subtypes, crashes the compilation. | ||
class C[+A] | ||
def foo[T <: Tuple : Tuple.IsMappedBy[C]] = ??? | ||
def bar[X] = foo[( | ||
C[X], C[X], C[X], C[X], C[X], C[X], C[X], C[X], C[X], C[X], | ||
C[X], C[X], C[X], C[X], C[X], C[X], C[X], C[X], C[X], C[X], | ||
C[X], C[X], C[X], | ||
)] |