Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register inheritance relationship for SAM-type lambda #1288

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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