Skip to content

Commit 8626272

Browse files
authored
Merge pull request #7544 from dotty-staging/fix-#6606
Fix #6606: Do not compute S on negative Ints
2 parents 2b15184 + 94536a0 commit 8626272

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
10221022
def compareS(tp: AppliedType, other: Type, fromBelow: Boolean): Boolean = tp.args match {
10231023
case arg :: Nil =>
10241024
natValue(arg) match {
1025-
case Some(n) =>
1025+
case Some(n) if n != Int.MaxValue =>
10261026
val succ = ConstantType(Constant(n + 1))
10271027
if (fromBelow) recur(other, succ) else recur(succ, other)
10281028
case none =>

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3598,7 +3598,8 @@ object Types {
35983598
if (defn.isCompiletime_S(tycon.symbol) && args.length == 1)
35993599
trace(i"normalize S $this", typr, show = true) {
36003600
args.head.normalized match {
3601-
case ConstantType(Constant(n: Int)) => ConstantType(Constant(n + 1))
3601+
case ConstantType(Constant(n: Int)) if n >= 0 && n < Int.MaxValue =>
3602+
ConstantType(Constant(n + 1))
36023603
case none => tryMatchAlias
36033604
}
36043605
}

library/src/scala/compiletime/package.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,15 @@ package object compiletime {
5252
*/
5353
inline def summonFrom[T](f: Nothing => T) <: T = ???
5454

55-
type S[X <: Int] <: Int
55+
/** Succesor of a natural number where zero is the type 0 and successors are reduced as if the definition was
56+
*
57+
* type S[N <: Int] <: Int = N match {
58+
* case 0 => 1
59+
* case 1 => 2
60+
* case 2 => 3
61+
* ...
62+
* case 2147483646 => 2147483647
63+
* }
64+
*/
65+
type S[N <: Int] <: Int
5666
}

tests/neg/i6606.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val t0: Int = 0
2+
val t1: scala.compiletime.S[-1] = 0 // error
3+
val t2: scala.compiletime.S[Int] = 1
4+
val t3: scala.compiletime.S[Int] = 0 // error
5+
val t4: scala.compiletime.S[Int] = t0 // error
6+
7+
val t5: scala.compiletime.S[2147483647] = -2147483648 // error
8+
val t6: scala.compiletime.S[-2147483648] = -2147483647 // error

0 commit comments

Comments
 (0)