Skip to content

Commit

Permalink
SI-5167 An impl class method should refer to its own parameter symbols.
Browse files Browse the repository at this point in the history
Rather than those of the original method in the trait.

If they are shared, parameter renaming in the implementaion class
is visible in the original method. This led to a crash in the resident
compiler when looking up the default argument getter.
  • Loading branch information
retronym committed Jun 3, 2012
1 parent 85cd96d commit 09bf956
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/compiler/scala/tools/nsc/transform/AddInterfaces.scala
Expand Up @@ -257,11 +257,21 @@ abstract class AddInterfaces extends InfoTransform { self: Erasure =>
/** Transforms the member tree containing the implementation
* into a member of the impl class.
*/
private def implMethodDef(tree: Tree): Tree = (
implMethodMap get tree.symbol
map (impl => new ChangeOwnerAndReturnTraverser(tree.symbol, impl)(tree setSymbol impl))
getOrElse abort("implMethod missing for " + tree.symbol)
)
private def implMethodDef(tree: Tree): Tree = {
val impl = implMethodMap.getOrElse(tree.symbol, abort("implMethod missing for " + tree.symbol))

val newTree = if (impl.isErroneous) tree else { // e.g. res/t687
// SI-5167: Ensure that the tree that we are grafting refers the parameter symbols from the
// new method symbol `impl`, rather than the symbols of the original method signature in
// the trait. `tree setSymbol impl` does *not* suffice!
val DefDef(_, _, _, vparamss, _, _) = tree
val oldSyms = vparamss.flatten.map(_.symbol)
val newSyms = impl.info.paramss.flatten
assert(oldSyms.length == newSyms.length, (oldSyms, impl, impl.info))
tree.substTreeSyms(oldSyms, newSyms)
}
new ChangeOwnerAndReturnTraverser(newTree.symbol, impl)(newTree setSymbol impl)
}

/** Add mixin constructor definition
* def $init$(): Unit = ()
Expand Down
4 changes: 4 additions & 0 deletions test/files/res/t5167.check
@@ -0,0 +1,4 @@

nsc>
nsc>
nsc>
2 changes: 2 additions & 0 deletions test/files/res/t5167.res
@@ -0,0 +1,2 @@
t5167/t5167_1.scala
t5167/t5167_2.scala
12 changes: 12 additions & 0 deletions test/files/res/t5167/t5167_1.scala
@@ -0,0 +1,12 @@
package compilerbug

trait SadTrait {
def buggyMethod[T](argWithDefault1: Int = 0)(argWithDefault2: String = "default") {
for (i <- 0 to 1) {
val x = argWithDefault1
val y = argWithDefault2
}
}
}

object SadObject extends SadTrait
7 changes: 7 additions & 0 deletions test/files/res/t5167/t5167_2.scala
@@ -0,0 +1,7 @@
package compilerbug

class TestClass {
def repro() {
SadObject.buggyMethod[Int]()()
}
}

0 comments on commit 09bf956

Please sign in to comment.