Skip to content

Commit

Permalink
Add support for non-singleton arguments in singleton ops
Browse files Browse the repository at this point in the history
These are simply checked as the supertype. For instance, `Int + Int`
is subtype of `Int`. This allows for more graceful failure when more 
general type arguments are provided.
  • Loading branch information
MaximeKjaer committed Dec 2, 2019
1 parent cb3ab69 commit 4d73d89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Expand Up @@ -1039,7 +1039,10 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w

def compareCompiletimeAppliedType(tp: AppliedType, other: Type, fromBelow: Boolean): Boolean = {
if (defn.isCompiletime_S(tp.tycon.typeSymbol)) compareS(tp, other, fromBelow)
else tp.tryCompiletimeConstantFold.exists(folded => recur(folded, other))
else {
val reduced = tp.tryCompiletimeConstantFold.getOrElse(tp.superType)
recur(reduced, other)
}
}

/** Like tp1 <:< tp2, but returns false immediately if we know that
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/singleton-ops.scala
Expand Up @@ -6,6 +6,12 @@ object Test {
summon[2 + 2 =:= 3] // error
summon[29 * 31 =:= 900] // error

val a: Int + Int = 3
val c: 1 + Int = 2
val d: Int + 1 = 1
summon[Int + 1 =:= Int]
summon[1 + 1 =:= Int] // error

val t0: 2 + 3 = 5
val t1: 2 + 2 = 5 // error
val t2: -1 + 1 = 0
Expand Down

0 comments on commit 4d73d89

Please sign in to comment.