Skip to content

Commit 73273ee

Browse files
committed
Fix #2732: Allow wildcards in SAM types
The `isInstantiable` test as written is incorrect: Given tp = `java.util.function.Function[String, _ <: String]`, we will have: tref = skolem TermRef with underlying info `tp` selfType = java.util.function.Function[tref#T, tref#R]` (because of the `asSeenFrom`) `tref <:< selfType` is false, because `tref.underlying` is just `tp` and its parameters are bounded wildcards, not references with `tref` as a prefix. In any case, I cannot think of a case where this function should return false, and no test failed when I removed it. So I'm removing it until someone can come up with a counter-example.
1 parent b7fbb39 commit 73273ee

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3717,16 +3717,8 @@ object Types {
37173717
case _ =>
37183718
NoType
37193719
}
3720-
def isInstantiatable(tp: Type)(implicit ctx: Context): Boolean = zeroParamClass(tp) match {
3721-
case cinfo: ClassInfo =>
3722-
val tref = tp.narrow
3723-
val selfType = cinfo.selfType.asSeenFrom(tref, cinfo.cls)
3724-
tref <:< selfType
3725-
case _ =>
3726-
false
3727-
}
37283720
def unapply(tp: Type)(implicit ctx: Context): Option[SingleDenotation] =
3729-
if (isInstantiatable(tp)) {
3721+
if (zeroParamClass(tp).exists) {
37303722
val absMems = tp.abstractTermMembers
37313723
// println(s"absMems: ${absMems map (_.show) mkString ", "}")
37323724
if (absMems.size == 1)

tests/pos/i2732.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Foo {
2+
// Example 1
3+
val fun: java.util.function.Function[String, _ <: String] = x => x
4+
5+
// Example 2
6+
val map = new java.util.HashMap[String, String]
7+
map.computeIfAbsent("hello", foo => "world")
8+
}

0 commit comments

Comments
 (0)