Skip to content

Commit ec53918

Browse files
committed
Fix type inference with scala.Singleton
When a type variable is upper bounded by scala.Singleton, its instantiation should not be widened
1 parent d929fc9 commit ec53918

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,11 @@ trait ConstraintHandling {
271271
var inst = approximation(param, fromBelow).simplified
272272

273273
// Then, approximate by (1.) - (3.) and simplify as follows.
274-
// 1. If instance is from below and is a singleton type, yet
275-
// upper bound is not a singleton type, widen the instance.
276-
if (fromBelow && isSingleton(inst) && !isSingleton(upperBound))
274+
// 1. If instance is from below and is a singleton type, yet upper bound is
275+
// not a singleton type or a reference to `scala.Singleton`, widen the
276+
// instance.
277+
if (fromBelow && isSingleton(inst) && !isSingleton(upperBound)
278+
&& !upperBound.isRef(defn.SingletonClass))
277279
inst = inst.widen
278280

279281
// 2. If instance is from below and is a fully-defined union type, yet upper bound

tests/pos/singletontrait.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
def foo[T <: Singleton](x: T): T = x
3+
4+
val a: 1 = foo(1)
5+
}

0 commit comments

Comments
 (0)