Skip to content

Commit 4d73d89

Browse files
committed
Add support for non-singleton arguments in singleton ops
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.
1 parent cb3ab69 commit 4d73d89

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,10 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
10391039

10401040
def compareCompiletimeAppliedType(tp: AppliedType, other: Type, fromBelow: Boolean): Boolean = {
10411041
if (defn.isCompiletime_S(tp.tycon.typeSymbol)) compareS(tp, other, fromBelow)
1042-
else tp.tryCompiletimeConstantFold.exists(folded => recur(folded, other))
1042+
else {
1043+
val reduced = tp.tryCompiletimeConstantFold.getOrElse(tp.superType)
1044+
recur(reduced, other)
1045+
}
10431046
}
10441047

10451048
/** Like tp1 <:< tp2, but returns false immediately if we know that

tests/neg/singleton-ops.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ object Test {
66
summon[2 + 2 =:= 3] // error
77
summon[29 * 31 =:= 900] // error
88

9+
val a: Int + Int = 3
10+
val c: 1 + Int = 2
11+
val d: Int + 1 = 1
12+
summon[Int + 1 =:= Int]
13+
summon[1 + 1 =:= Int] // error
14+
915
val t0: 2 + 3 = 5
1016
val t1: 2 + 2 = 5 // error
1117
val t2: -1 + 1 = 0

0 commit comments

Comments
 (0)