Skip to content

Commit 9736c78

Browse files
authored
Fix under-compilation when the method type in a SAM changes (#16996)
A lambda that implements a SAM type desugars (either at compile-time or runtime via invokedynamic) into a class that extends the SAM type, therefore we need to register an inheritance relationship to get the proper invalidation logic. Fixes sbt/zinc#830.
2 parents ba481c6 + 7f0fe63 commit 9736c78

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
306306
}
307307
}
308308

309+
private def addInheritanceDependencies(tree: Closure)(using Context): Unit =
310+
// If the tpt is empty, this is a non-SAM lambda, so no need to register
311+
// an inheritance relationship.
312+
if !tree.tpt.isEmpty then
313+
val from = resolveDependencySource
314+
_dependencies += ClassDependency(from, tree.tpt.tpe.classSymbol, LocalDependencyByInheritance)
315+
309316
private def addInheritanceDependencies(tree: Template)(using Context): Unit =
310317
if (tree.parents.nonEmpty) {
311318
val depContext = depContextOf(tree.symbol.owner)
@@ -369,6 +376,8 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
369376
case ref: RefTree =>
370377
addMemberRefDependency(ref.symbol)
371378
addTypeDependency(ref.tpe)
379+
case t: Closure =>
380+
addInheritanceDependencies(t)
372381
case t: Template =>
373382
addInheritanceDependencies(t)
374383
case _ =>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trait A {
2+
def foo(): Int
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class B:
2+
val f: A = () => 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scalaVersion := sys.props("plugin.scalaVersion")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trait A {
2+
def foo(): String
3+
}

sbt-test/source-dependencies/sam/test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
> compile
2+
3+
# change the SAM type
4+
$ copy-file changes/A.scala A.scala
5+
6+
# Both A.scala and B.scala should be recompiled, producing a compile error
7+
-> compile

0 commit comments

Comments
 (0)