summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksandar Prokopec <axel22@gmail.com>2012-08-20 22:10:57 +0200
committerAleksandar Prokopec <axel22@gmail.com>2012-08-21 15:04:43 +0200
commit9733f56c876c6097383bd4103bd0a7be4373742e (patch)
tree516fc81e6d675d106f5b7a851f039c661c56f109
parent2b93ace3518ce48f145d3afb8d4e1abe9fcb26aa (diff)
downloadscala-9733f56c876c6097383bd4103bd0a7be4373742e.tar.gz
scala-9733f56c876c6097383bd4103bd0a7be4373742e.tar.bz2
scala-9733f56c876c6097383bd4103bd0a7be4373742e.zip
Fixes SI-4996.
This bug is a result of a subtle interplay of the stackable modifications mechanism and specialization. Prior to this commit, using `abstract override` with specialization was broken in the sense that specialization did not create a specialized version of the super accessor. Observe the following code: trait A[@specialized(Int) T] { def foo(t: T) } trait B extends A[Int] { def foo(t: Int) { println("B.foo") } } trait M extends B { abstract override def foo(t: Int) { super.foo(t) println("M.foo") } } object C extends B with M During the `superaccessors` phase, the following stub is generated in `M`: private <artifact> <superaccessor> def super$foo(t: Int) Note that `foo` is a method that will later need to be specialized. During the `specialize` phase, `A.foo` gets a *special overload* `A.foo$mcI$sp`, which is a bridge to `A.foo`. `B` extends `A$mcI$sp` (previously `A[Int]`), so `B.foo` gets a *special override* `B.foo$mcI$sp`, which contains the implementation. `B.foo` is overridden to become a bridge to `B.foo$mcI$sp`. `M` extends `B`, so `M.foo` gets a special override `M.foo$mcI$sp`, and `M.foo` itself is turned into a bridge to `M.foo$mcI$sp`, just as was the case with `B`. This is where the first problem arises - `M.foo$mcI$sp` does not get an `ABSOVERRIDE` flag after being created. This commit fixes that. As mentioned earlier, `M` has a super accessor `super$foo`. A super accessor (naturally) does not override anything, thus, according to the standing specialization criteria it does not need a special override (see `needsSpecialOverride`). Its type does not contain any specialized type parameters, thus, it is not eligible to obtain a special overload. So, our `super$foo` stays the way it is, subsequently being renamed to `M$$super$foo`. Later, during `mixin`, it is implemented in `C` as a forwarder to `B$class.foo` (Not `B.foo`, because the implementation classes are generated in `mixin`). Lets see what we have now (I omit the `$this` parameter for methods in implementation classes `B$class` and `M$class`): class B$class def foo(t: Int) = ------------\ <-\ def foo$mcI$sp(t: Int) = | | | | trait M$class | | def foo(t: Int) = -------\ | | | | | def foo$mcI$sp(t: Int) = <-/<-\ | | { | | | /---- M$$super$foo(t) | | | | ... | | | | } | | | | | | | | object C | | | | def foo$mcI$sp(t: Int) = -----/ <-/ | \-> def M$$super$foo(t: Int) = | { | ------------------------------------/ Now, call `C.foo`. This call is rewritten to `C.foo$mcI$sp`, which is a bridge to `M$class.foo$mcI$sp`. Follow the lines on the diagram to enter an endless loop. Boom! Stack overflow. The culprit is the super accessor `C.M$$super$foo`, which should have forwarded to the special override `B$class.foo$mcI$sp`, instead of to `B$class.foo`. So, we have 2 options: 1) Make `C.M$$super$foo` forward to the proper special override, where the implementation actually is. 2) During specialization, create a specialized version of `M$$super$foo` called `M$$super$foo$mcI$sp`, and set its alias to the special overload `foo$mcI$sp`. Later, during `mixin`, this super accessor will be appropriately resolved in concrete classes. Option 1) involves cluttering `mixin` with specialization logic. Furthermore, the specialization already does create specialized versions of super accessors when the super accessor type contains specialized type parameters (in other words, it generates a special overload). Thus, 2) seems an ideal solution. We cannot deduct if a super accessor should get a special override directly, but we can see if its alias needs a special override. If it does, then we generate a special override for the super accessor. This commit introduces the following changes: 1) The `ABSOVERRIDE` flag is now retained, as mentioned earlier. 2) A super accessor gets a special override if its alias needs a special override. 3) The super calls in the methods bodies are rewritten to their specialized variants if they exist. 4) Newly generated special overrides and special overloads are now entered into the declaration list of the owner during specialization. Strangely, this was not done before, but it is necessary for `mixin` to detect the generated special overload/override which is an alias for the super accessor.
-rw-r--r--src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala41
-rw-r--r--test/files/run/t4996.check4
-rw-r--r--test/files/run/t4996.scala47
3 files changed, 86 insertions, 6 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala
index 3366244bc6..d2c3744a1c 100644
--- a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala
+++ b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala
@@ -218,6 +218,11 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
def target = t
}
+ /** Symbol is a special overload of the super accessor. */
+ case class SpecialSuperAccessor(t: Symbol) extends SpecializedInfo {
+ def target = t
+ }
+
/** Symbol is a specialized accessor for the `target` field. */
case class SpecializedAccessor(target: Symbol) extends SpecializedInfo {
override def isAccessor = true
@@ -865,6 +870,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
}
val specMember = subst(outerEnv)(specializedOverload(owner, sym, spec))
+ owner.info.decls.enter(specMember)
typeEnv(specMember) = typeEnv(sym) ++ outerEnv ++ spec
wasSpecializedForTypeVars(specMember) ++= spec collect { case (s, tp) if s.tpe == tp => s }
@@ -894,10 +900,11 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
}
/** Return the specialized overload of `m`, in the given environment. */
- private def specializedOverload(owner: Symbol, sym: Symbol, env: TypeEnv): Symbol = {
+ private def specializedOverload(owner: Symbol, sym: Symbol, env: TypeEnv, nameSymbol: Symbol = NoSymbol): Symbol = {
val newFlags = (sym.flags | SPECIALIZED) & ~(DEFERRED | CASEACCESSOR)
// this method properly duplicates the symbol's info
- ( sym.cloneSymbol(owner, newFlags, specializedName(sym, env))
+ val specname = specializedName(nameSymbol orElse sym, env)
+ ( sym.cloneSymbol(owner, newFlags, specname)
modifyInfo (info => subst(env, info.asSeenFrom(owner.thisType, sym.owner)))
)
}
@@ -957,14 +964,32 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
}
(clazz.info.decls flatMap { overriding =>
needsSpecialOverride(overriding) match {
- case (NoSymbol, _) => None
+ case (NoSymbol, _) =>
+ if (overriding.isSuperAccessor) {
+ val alias = overriding.alias
+ debuglog("checking special overload for super accessor: %s, alias for %s".format(overriding.fullName, alias.fullName))
+ needsSpecialOverride(alias) match {
+ case nope @ (NoSymbol, _) => None
+ case (overridden, env) =>
+ val om = specializedOverload(clazz, overriding, env, overridden)
+ om.setName(nme.superName(om.name))
+ om.asInstanceOf[TermSymbol].setAlias(info(alias).target)
+ om.owner.info.decls.enter(om)
+ info(om) = SpecialSuperAccessor(om)
+ om.makeNotPrivate(om.owner)
+ overloads(overriding) ::= Overload(om, env)
+ Some(om)
+ }
+ } else None
case (overridden, env) =>
val om = specializedOverload(clazz, overridden, env)
+ clazz.info.decls.enter(om)
debuglog("specialized overload %s for %s in %s: %s".format(om, overriding.name.decode, pp(env), om.info))
+ if (overriding.isAbstractOverride) om.setFlag(ABSOVERRIDE)
typeEnv(om) = env
addConcreteSpecMethod(overriding)
info(om) = (
- if (overriding.isDeferred) { // abstract override
+ if (overriding.isDeferred) { // abstract override
debuglog("abstract override " + overriding.fullName + " with specialized " + om.fullName)
Forward(overriding)
}
@@ -1287,7 +1312,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
if (sym.isPrivate) debuglog(
"seeing private member %s, currentClass: %s, owner: %s, isAccessible: %b, isLocalName: %b".format(
sym, currentClass, sym.owner.enclClass, isAccessible(sym), nme.isLocalName(sym.name))
- )
+ )
if (shouldMakePublic(sym) && !isAccessible(sym)) {
debuglog("changing private flag of " + sym)
sym.makeNotPrivate(sym.owner)
@@ -1548,7 +1573,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
case SpecialOverride(target) =>
assert(body.isDefinedAt(target), "sym: " + symbol.fullName + " target: " + target.fullName)
//debuglog("moving implementation, body of target " + target + ": " + body(target))
- debuglog("%s is param accessor? %b".format(ddef.symbol, ddef.symbol.isParamAccessor))
+ log("%s is param accessor? %b".format(ddef.symbol, ddef.symbol.isParamAccessor))
// we have an rhs, specialize it
val tree1 = addBody(ddef, target)
(new ChangeOwnerTraverser(target, tree1.symbol))(tree1.rhs)
@@ -1596,6 +1621,10 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
case Abstract(targ) =>
debuglog("abstract: " + targ)
localTyper.typed(deriveDefDef(tree)(rhs => rhs))
+
+ case SpecialSuperAccessor(targ) =>
+ debuglog("special super accessor: " + targ + " for " + tree)
+ localTyper.typed(deriveDefDef(tree)(rhs => rhs))
}
case ValDef(_, _, _, _) if symbol.hasFlag(SPECIALIZED) && !symbol.isParamAccessor =>
diff --git a/test/files/run/t4996.check b/test/files/run/t4996.check
new file mode 100644
index 0000000000..8d45b413c9
--- /dev/null
+++ b/test/files/run/t4996.check
@@ -0,0 +1,4 @@
+B.foo
+M.foo
+B.foo
+M.foo \ No newline at end of file
diff --git a/test/files/run/t4996.scala b/test/files/run/t4996.scala
new file mode 100644
index 0000000000..8e7636aaac
--- /dev/null
+++ b/test/files/run/t4996.scala
@@ -0,0 +1,47 @@
+
+
+
+
+
+
+trait A[@specialized(Int) T] {
+ def foo(t: T)
+}
+
+
+trait B extends A[Int] {
+ def foo(t: Int) {
+ println("B.foo")
+ }
+}
+
+
+trait M extends B {
+ abstract override def foo(t: Int) {
+ super.foo(t)
+ println("M.foo")
+ }
+}
+
+
+object C extends B with M
+
+
+object D extends B {
+ override def foo(t: Int) {
+ super.foo(t)
+ println("M.foo")
+ }
+}
+
+
+object Test {
+
+ def main(args: Array[String]) {
+ D.foo(42) // OK, prints B.foo M.foo
+ C.foo(42) // was StackOverflowError
+ }
+
+}
+
+