summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2012-05-26 22:49:12 +0200
committerJason Zaugg <jzaugg@gmail.com>2012-05-27 12:18:44 +0200
commitc82bc67737a31f2a639172e677cafdefe8fdbf4e (patch)
tree6da5069e6c92b72e36d5118cb79424f511d1fe2b
parent688c558b0257636f8f46240616b8b3d448847a47 (diff)
downloadscala-c82bc67737a31f2a639172e677cafdefe8fdbf4e.tar.gz
scala-c82bc67737a31f2a639172e677cafdefe8fdbf4e.tar.bz2
scala-c82bc67737a31f2a639172e677cafdefe8fdbf4e.zip
Fix a NSDNHAO in extension methods.
A bridge method, created when we override a method from a superclass and refine the return type, was appearing as an overloaded alternative. (`erasure` doesn't create new scopes, so the bridges it builds are visible at earlier phases.) The problem was masked when compiling with specialization, which *does* create a new scope, shielding the code in question from the artefacts of erasure. To fix the problem, we filter out bridge methods from the overloaded alternatives returned by `.decl`, as would happen internally in `.member`.
-rw-r--r--src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala15
-rw-r--r--test/files/pos/value-class-override-no-spec.flags1
-rw-r--r--test/files/pos/value-class-override-no-spec.scala9
-rw-r--r--test/files/pos/value-class-override-spec.scala9
4 files changed, 32 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala b/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala
index 007457ef7b..8556cc9ddc 100644
--- a/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala
+++ b/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala
@@ -44,8 +44,18 @@ abstract class ExtensionMethods extends Transform with TypingTransformers {
* in `extensionMethod` if the first name has the wrong type. We thereby gain a level of insensitivity
* of how overloaded types are ordered between phases and picklings.
*/
- private def extensionNames(imeth: Symbol): Stream[Name] =
- imeth.owner.info.decl(imeth.name).tpe match {
+ private def extensionNames(imeth: Symbol): Stream[Name] = {
+ val decl = imeth.owner.info.decl(imeth.name)
+
+ // Bridge generation is done at phase `erasure`, but new scopes are only generated
+ // for the phase after that. So bridges are visible in earlier phases.
+ //
+ // `info.member(imeth.name)` filters these out, but we need to use `decl`
+ // to restrict ourselves to members defined in the current class, so we
+ // must do the filtering here.
+ val declTypeNoBridge = decl.filter(sym => !sym.isBridge).tpe
+
+ declTypeNoBridge match {
case OverloadedType(_, alts) =>
val index = alts indexOf imeth
assert(index >= 0, alts+" does not contain "+imeth)
@@ -55,6 +65,7 @@ abstract class ExtensionMethods extends Transform with TypingTransformers {
assert(tpe != NoType, imeth.name+" not found in "+imeth.owner+"'s decls: "+imeth.owner.info.decls)
Stream(newTermName("extension$"+imeth.name))
}
+ }
/** Return the extension method that corresponds to given instance method `meth`.
*/
diff --git a/test/files/pos/value-class-override-no-spec.flags b/test/files/pos/value-class-override-no-spec.flags
new file mode 100644
index 0000000000..a7e64e4f0c
--- /dev/null
+++ b/test/files/pos/value-class-override-no-spec.flags
@@ -0,0 +1 @@
+-no-specialization \ No newline at end of file
diff --git a/test/files/pos/value-class-override-no-spec.scala b/test/files/pos/value-class-override-no-spec.scala
new file mode 100644
index 0000000000..79de5d9305
--- /dev/null
+++ b/test/files/pos/value-class-override-no-spec.scala
@@ -0,0 +1,9 @@
+// There are two versions of this tests: one with and one without specialization.
+// The bug was only exposed *without* specialization.
+trait T extends Any {
+ def x: Any
+}
+
+final class StringOps(val repr0: String) extends AnyVal with T {
+ def x = ()
+}
diff --git a/test/files/pos/value-class-override-spec.scala b/test/files/pos/value-class-override-spec.scala
new file mode 100644
index 0000000000..79de5d9305
--- /dev/null
+++ b/test/files/pos/value-class-override-spec.scala
@@ -0,0 +1,9 @@
+// There are two versions of this tests: one with and one without specialization.
+// The bug was only exposed *without* specialization.
+trait T extends Any {
+ def x: Any
+}
+
+final class StringOps(val repr0: String) extends AnyVal with T {
+ def x = ()
+}