diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index a881b361d553..c99f46f57de0 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -1022,7 +1022,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w def compareS(tp: AppliedType, other: Type, fromBelow: Boolean): Boolean = tp.args match { case arg :: Nil => natValue(arg) match { - case Some(n) => + case Some(n) if n != Int.MaxValue => val succ = ConstantType(Constant(n + 1)) if (fromBelow) recur(other, succ) else recur(succ, other) case none => diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index fb0f712a3e8f..6727147fe644 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -3552,7 +3552,8 @@ object Types { if (defn.isCompiletime_S(tycon.symbol) && args.length == 1) trace(i"normalize S $this", typr, show = true) { args.head.normalized match { - case ConstantType(Constant(n: Int)) => ConstantType(Constant(n + 1)) + case ConstantType(Constant(n: Int)) if n >= 0 && n < Int.MaxValue => + ConstantType(Constant(n + 1)) case none => tryMatchAlias } } diff --git a/library/src/scala/compiletime/package.scala b/library/src/scala/compiletime/package.scala index 636e2cbd6e9d..28ac8e519637 100644 --- a/library/src/scala/compiletime/package.scala +++ b/library/src/scala/compiletime/package.scala @@ -52,5 +52,15 @@ package object compiletime { */ inline def summonFrom[T](f: Nothing => T) <: T = ??? - type S[X <: Int] <: Int + /** Succesor of a natural number where zero is the type 0 and successors are reduced as if the definition was + * + * type S[N <: Int] <: Int = N match { + * case 0 => 1 + * case 1 => 2 + * case 2 => 3 + * ... + * case 2147483646 => 2147483647 + * } + */ + type S[N <: Int] <: Int } diff --git a/tests/neg/i6606.scala b/tests/neg/i6606.scala new file mode 100644 index 000000000000..74847e2e6f53 --- /dev/null +++ b/tests/neg/i6606.scala @@ -0,0 +1,8 @@ +val t0: Int = 0 +val t1: scala.compiletime.S[-1] = 0 // error +val t2: scala.compiletime.S[Int] = 1 +val t3: scala.compiletime.S[Int] = 0 // error +val t4: scala.compiletime.S[Int] = t0 // error + +val t5: scala.compiletime.S[2147483647] = -2147483648 // error +val t6: scala.compiletime.S[-2147483648] = -2147483647 // error