Skip to content

Commit

Permalink
Merge pull request #1288 from Friendseeker/sam
Browse files Browse the repository at this point in the history
Register inheritance relationship for SAM-type lambda
  • Loading branch information
eed3si9n authored Dec 7, 2023
2 parents be2bb54 + c5c1c64 commit c04cd23
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ abstract class Compat {
import global._

protected def processOriginalTreeAttachment(in: Tree)(func: Tree => Unit): Unit = ()

protected def processSAMAttachment(f: Function)(addDependency: Symbol => Unit): Unit =
()
}
object Compat {
// IR is renamed to Results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ abstract class Compat {
protected def processOriginalTreeAttachment(in: Tree)(func: Tree => Unit): Unit = {
Compat.OriginalTreeTraverser.Instance.traverseOriginal(in)(func)
}

protected def processSAMAttachment(f: Function)(addDependency: Symbol => Unit): Unit = {
f.attachments.get[SAMFunction].foreach(sam => {
addDependency(sam.samTp.typeSymbol)
})
}
}
object Compat {
// IR is renamed to Results
Expand Down
10 changes: 10 additions & 0 deletions internal/compiler-bridge/src/main/scala/xsbt/Dependency.scala
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,16 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
val sym = if (tree.symbol.isModule) tree.symbol.moduleClass else tree.symbol
localToNonLocalClass.resolveNonLocal(sym)
super.traverse(tree)

case f: Function =>
processSAMAttachment(f)(symbol => {
addDependency(symbol)
// Not using addInheritanceDependency as it would incorrectly classify dependency as non-local
// ref: https://github.com/scala/scala/pull/10617/files#r1415226169
val from = resolveDependencySource
addClassDependency(_localInheritanceCache, processor.localInheritance, from, symbol)
})
super.traverse(tree)
case other => super.traverse(other)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ abstract class Compat {
}

protected def processOriginalTreeAttachment(in: Tree)(func: Tree => Unit): Unit = ()
protected def processSAMAttachment(f: Function)(addDependency: Symbol => Unit): Unit =
()
}

/** Defines compatibility utils for [[ZincCompiler]]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ abstract class Compat {
func(a.original)
}
}

protected def processSAMAttachment(f: Function)(addDependency: Symbol => Unit): Unit = {
f.attachments.get[SAMFunction].foreach(sam => {
addDependency(sam.samTp.typeSymbol)
})
}
}
object Compat {
// IR is renamed to Results
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

trait A {
def foo(): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class B {
val f: A = () => 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class C extends B
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait A {
def foo(): AnyVal
}
13 changes: 13 additions & 0 deletions zinc/src/sbt-test/source-dependencies/sam-local-inheritance/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
> compile

> checkRecompilations 0 A B C

# change the SAM type
$ copy-file changes/A.scala A.scala

> compile

> checkIterations 3
> checkRecompilations 1 A
# SAM type change does not affect C, hence C should not be recompiled
> checkRecompilations 2 B
4 changes: 4 additions & 0 deletions zinc/src/sbt-test/source-dependencies/sam/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

trait A {
def foo(): Int
}
3 changes: 3 additions & 0 deletions zinc/src/sbt-test/source-dependencies/sam/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class B {
val f: A = () => 1
}
3 changes: 3 additions & 0 deletions zinc/src/sbt-test/source-dependencies/sam/changes/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait A {
def foo(): String
}
7 changes: 7 additions & 0 deletions zinc/src/sbt-test/source-dependencies/sam/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
> compile

# change the SAM type
$ copy-file changes/A.scala A.scala

# Both A.scala and B.scala should be recompiled, producing a compile error
-> compile

0 comments on commit c04cd23

Please sign in to comment.