Skip to content

Commit 94e1ccc

Browse files
committed
Don't treat SAM class with Unit result type as a platform SAM
1 parent 95f9aff commit 94e1ccc

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

compiler/src/dotty/tools/dotc/config/JavaPlatform.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ class JavaPlatform extends Platform {
5050
cls.superClass == defn.ObjectClass &&
5151
cls.directlyInheritedTraits.forall(_.is(NoInits)) &&
5252
!ExplicitOuter.needsOuterIfReferenced(cls) &&
53-
cls.typeRef.fields.isEmpty // Superaccessors already show up as abstract methods here, so no test necessary
53+
// Superaccessors already show up as abstract methods here, so no test necessary
54+
cls.typeRef.fields.isEmpty &&
55+
// LambdaMetafactory can't handle SAMs with Unit return type unless it's a FunctionN itself
56+
!cls.typeRef.possibleSamMethods.exists(_.info.resultType.isRef(defn.UnitClass))
5457

5558
/** We could get away with excluding BoxedBooleanClass for the
5659
* purpose of equality testing since it need not compare equal

tests/run/i24573.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1
2+
2
3+
3
4+
42
5+
hello

tests/run/i24573.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
trait Con[-T] extends (T => Unit):
2+
def apply(t: T): Unit
3+
4+
trait Con2[-T] extends (T => Int):
5+
def apply(t: T): Int
6+
7+
trait Con3[+R] extends (() => R):
8+
def apply(): R
9+
10+
object Test:
11+
def main(args: Array[String]): Unit =
12+
val f1: (Int => Unit) = i => println(i)
13+
f1(1)
14+
15+
val c1: Con[Int] = i => println(i)
16+
c1(2)
17+
18+
val c2: Con2[Int] = i => { println(i); i }
19+
c2(3)
20+
21+
val c3: Con3[Int] = () => 42
22+
println(c3())
23+
24+
val c4: Con3[Unit] = () => println("hello")
25+
c4()

0 commit comments

Comments
 (0)