Skip to content

Fix #6606: Do not compute S on negative Ints #7544

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 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
12 changes: 11 additions & 1 deletion library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions tests/neg/i6606.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
val t0: Int = 0
val t1: scala.compiletime.S[-1] = 0 // error
val t2: scala.compiletime.S[Int] = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear to me why this line type checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is because type 1 =:= S[0] <:< S[Int]

Copy link
Contributor

@liufengyun liufengyun Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to figure out the precise spec for S, whether it's co-variant, contra-variant, non-variant in another issue.

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