Skip to content

Commit

Permalink
Fixes undercompilation on inheritance on same source
Browse files Browse the repository at this point in the history
### background

In sbt 0.13 days, we could ignore the relationship between two classes defined
in the same `*.scala` source file, because they will be compiled anyway, and
the invalidation was done at the source file level. With class-based namehashing,
the invalidation is done at the class level, so we can no longer ignore inheritance
relationship coming from the same source, but we still have old assumptions scattered
around the xsbt-dependency implementation.

### what we see without the fix

```
[info] Compiling 1 Scala source to ...
....
[debug] [inv]   internalDependencies:
[debug] [inv]     DependencyByInheritance Relation [
[debug] [inv]     xx.EvaluatorTestSupport -> gg.table.SliceTransforms
[debug] [inv]     xx.StringLibSpecs -> xx.EvaluatorSpecification
[debug] [inv] ]
[debug] [inv]     DependencyByMemberRef Relation [
[debug] [inv]     xx.EvaluatorTestSupport -> gg.table.SliceTransforms
[debug] [inv]     xx.Hello -> gg.table.SliceTransforms
[debug] [inv]     xx.StringLibSpecs -> xx.EvaluatorSpecification
[debug] [inv] ]
....
Caused by: java.lang.AbstractMethodError: xx.StringLibSpecs.buildNonemptyObjects(II)V
```

First, we see that `xx.EvaluatorSpecification -> xx.EvaluatorTestSupport DependencyByInheritance` relationship is missing. Second, the error message seen is `java.lang.AbstractMethodError` happening on `xx.StringLibSpecs`.

### what this changes

This change changes two if expressions that was used to filter out dependency info coming from the same source.

One might wonder why it's necessary to keep the local inheritance info, if two classes involved are compiled together anyways. The answer is transitive dependencies.
Here's likely what was happening:

1. `gg.table.SliceTransforms` was changed,
2. causing `xx.EvaluatorTestSupport` to invalidate.
3. However, because of the missing same-source inheritance, it did not invalidate `xx.EvaluatorSpecification`.
4. This meant that neither `xx.StringLibSpecs` was invalidated.
5. Calling transform method on a new `xx.StringLibSpecs` causes runtime error.

By tracking same-source inheritance, we will now correctly invalidate `xx.EvaluatorSpecification` and `xx.StringLibSpecs`.
I think the assumption that's broken here is that "we don't need to track inheritance that is happening between two classes in a same source."

### Is this 2.11 only issue?

No. The simple trait-trait inheritance reproduction alone will not cause problem in 2.12
because in Scala 2.12 because of the [compile-to-interface](http://www.scala-lang.org/news/2.12.0/#traits-compile-to-interfaces) traits.
However, not all traits will compile to interface.
This means that if we want to take advantage of the compile-to-interface traits,
we still should keep track of the same-source inheritance, but introduce some more
logic to determine whether recompilation is necessary.

Fixes sbt#417
  • Loading branch information
eed3si9n committed Oct 11, 2017
1 parent f4a7b98 commit eab9dbc
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 32 deletions.
21 changes: 13 additions & 8 deletions internal/compiler-bridge/src/main/scala/xsbt/Dependency.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,21 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
}

// Define processor reusing `processDependency` definition
val memberRef = processDependency(DependencyByMemberRef) _
val inheritance = processDependency(DependencyByInheritance) _
val localInheritance = processDependency(LocalDependencyByInheritance) _
val memberRef = processDependency(DependencyByMemberRef, false) _
val inheritance = processDependency(DependencyByInheritance, true) _
val localInheritance = processDependency(LocalDependencyByInheritance, true) _

@deprecated("Use processDependency that takes allowLocal.", "1.1.0")
def processDependency(context: DependencyContext)(dep: ClassDependency): Unit =
processDependency(context, true)(dep)

/*
* Handles dependency on given symbol by trying to figure out if represents a term
* that is coming from either source code (not necessarily compiled in this compilation
* run) or from class file and calls respective callback method.
*/
def processDependency(context: DependencyContext)(dep: ClassDependency): Unit = {
def processDependency(context: DependencyContext, allowLocal: Boolean)(
dep: ClassDependency): Unit = {
val fromClassName = classNameAsString(dep.from)

def binaryDependency(file: File, binaryClassName: String) =
Expand Down Expand Up @@ -133,11 +138,12 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
case None =>
debuglog(Feedback.noOriginFileForExternalSymbol(dep.to))
}
} else if (onSource.file != sourceFile) {
// Dependency is internal -- but from other file / compilation unit
} else if (onSource.file != sourceFile || allowLocal) {
// We cannot ignore dependencies coming from the same source file because
// the dependency info needs to propagate. See source-dependencies/trait-trait-211.
val onClassName = classNameAsString(dep.to)
callback.classDependency(onClassName, fromClassName, context)
} else () // Comes from the same file, ignore
}
}
}

Expand Down Expand Up @@ -227,7 +233,6 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
val depClass = enclOrModuleClass(dep)
val dependency = ClassDependency(fromClass, depClass)
if (!cache.contains(dependency) &&
fromClass.associatedFile != depClass.associatedFile &&
!depClass.isRefinementClass) {
process(dependency)
cache.add(dependency)
Expand Down
40 changes: 19 additions & 21 deletions internal/zinc-core/src/main/scala/sbt/internal/inc/Relations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ object Relations {
case o: ClassDependencies => internal == o.internal && external == o.external
case _ => false
}
override def toString: String = s"ClassDependencies(internal = $internal, external = $external)"

override def hashCode = (internal, external).hashCode
}
Expand Down Expand Up @@ -661,26 +662,23 @@ private class MRelationsNameHashing(
override def hashCode =
(srcProd :: libraryDep :: libraryClassName :: memberRef :: inheritance :: classes :: Nil).hashCode

override def toString = (
"""
override def toString: String = {
val internalDepsStr = (internalDependencies.dependencies map {
case (k, vs) => k + " " + relation_s(vs)
}).mkString("\n ", "\n ", "")
val externalDepsStr = (externalDependencies.dependencies map {
case (k, vs) => k + " " + relation_s(vs)
}).mkString("\n ", "\n ", "")
s"""
|Relations (with name hashing enabled):
| products: %s
| library deps: %s
| library class names: %s
| class deps: %s
| ext deps: %s
| class names: %s
| used names: %s
| product class names: %s
""".trim.stripMargin.format(
List(srcProd,
libraryDep,
libraryClassName,
internalClassDep,
externalClassDep,
classes,
names,
productClassName) map relation_s: _*)
)

| products: ${relation_s(srcProd)}
| library deps: ${relation_s(libraryDep)}
| library class names: ${relation_s(libraryClassName)}
| internalDependencies: $internalDepsStr
| externalDependencies: $externalDepsStr
| class names: ${relation_s(classes)}
| used names: ${relation_s(names)}
| product class names: ${relation_s(productClassName)}
""".trim.stripMargin
}
}
4 changes: 1 addition & 3 deletions zinc/src/sbt-test/source-dependencies/patMat-scope/test
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ $ sleep 1000
$ exists target/classes/foo/SealedUsedInPatMatScope.class
$ exists target/classes/foo/SealedNameUsedInDefaultScope.class

# Default scopes should not change
$ newer beforeFirstCompilation target/classes/foo/SealedNameUsedInDefaultScope.class
# But PatMat and direct usage of child should be recompiled
# PatMat and direct usage of child should be recompiled
$ newer target/classes/foo/SealedUsedInPatMatScope.class beforeFirstCompilation

# Change type of Sealed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"projects": [
{
"name": "mirtest",
"scalaVersion": "2.11.8"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gg
package table

trait SliceTransforms {
def transform: Unit = {
// the use site is updated
buildNonemptyObjects(0, 1)
}

// add extra parameter here
def buildNonemptyObjects(a: Int, b: Int): Unit = ()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package xx

import gg.table._

trait EvaluatorSpecification extends EvaluatorTestSupport {
}

trait EvaluatorTestSupport extends SliceTransforms {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package xx

object Hello extends App {
val consumer = new StringLibSpecs
consumer.transform
}

class StringLibSpecs extends EvaluatorSpecification
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg
package table

trait SliceTransforms {
def transform: Unit = {
buildNonemptyObjects(0)
}

def buildNonemptyObjects(a: Int): Unit = ()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
relationsDebug = true
5 changes: 5 additions & 0 deletions zinc/src/sbt-test/source-dependencies/trait-trait-211/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> mirtest/run

## After copying the Good implementation, we should be able to run successfully.
$ copy-file changes/SliceTransforms1.scala mirtest/SliceTransforms.scala
> mirtest/run

0 comments on commit eab9dbc

Please sign in to comment.