scala / scala-dev

Scala 2 team issues. Not for user-facing bugs or directly actionable user-facing improvements. For build/test/infra and for longer-term planning and idea tracking. Our bug tracker is at https://github.com/scala/bug/issues
Apache License 2.0
130 stars 14 forks source link

Class dependency tracking #716

Open eed3si9n opened 4 years ago

eed3si9n commented 4 years ago

In Zinc meeting with Scala Center 2020-07-21 (@lrytz, @dwijnand, @sjrd, @eed3si9n), an idea came up today about informal hook for Class dependency tracking.

In addition (or instead) of Zinc injecting xsbt-dependency phase and try to figure out all instances of class dependencies, the idea is for the compiler to keep track of dependency information during the compilation as it happens, and Zinc can collect it afterwards. This is because we think it would be easier / more accurate if we keep track of dependency information as it happens, and for certain lookups it can only be observed in a certain phase.

Here are some of the example that may benefit from this approach..

SAM type inference

https://github.com/sbt/zinc/issues/830

Wildcard import

This is an example @sjrd mentioned. Suppose x is coming from A.x:

object Test {
  import A._
  {
    import B._
    println(x)
  }
}

The compiler still tried to look up x from B._, which may be tracked. The idea is that if B later adds x it should trigger recompilation. (Is current name hashing able to catch this?)

Dependency

Here is the current implementation of https://github.com/sbt/zinc/blob/v1.4.0-M6/internal/compiler-bridge/src/main/scala/xsbt/Dependency.scala

3 callback called into Zinc are:

callback.dependencyPhaseCompleted()
callback.binaryDependency(file, binaryClassName, fromClassName, sourceFile, context)
callback.classDependency(onClassName, fromClassName, context)

where context is defined by

public enum DependencyContext {
    /**
     * Represents a direct dependency between two symbols :
     * object Foo
     * object Bar { def foo = Foo }
     */
    DependencyByMemberRef,
    /**
     * Represents an inheritance dependency between two symbols :
     * class A
     * class B extends A
     */
    DependencyByInheritance,
    /**
     * Represents an inheritance dependency between a local class
     * and a non local class:
     * // A.scala
     * class A
     * // B.scala
     * class B {
     *   def foo = {
     *     class Local extends A
     *   }
     * }
     */
     LocalDependencyByInheritance;       
 }