summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2013-05-31 00:22:50 -0700
committerJason Zaugg <jzaugg@gmail.com>2014-02-09 22:28:23 +0100
commitf8d80ea26a03a39fd1d0160a2fad69752712f574 (patch)
tree66495ad03477362642d315d5bbcb2bf1ee155dce /tools
parent3306967c561e3ea34306419d5351b4e20673cc8b (diff)
downloadscala-f8d80ea26a03a39fd1d0160a2fad69752712f574.tar.gz
scala-f8d80ea26a03a39fd1d0160a2fad69752712f574.tar.bz2
scala-f8d80ea26a03a39fd1d0160a2fad69752712f574.zip
SI-3452 Correct Java generic signatures for mixins, static forwarders
[Parts of this patch and some of the commentary are from @paulp] This took me so long to figure out I can't even tell you. Partly because there were two different bugs, one which only arose for trait forwarders and one for mirror class forwarders, and every time I'd make one set of tests work another set would start failing. The runtime failures associated with these bugs were fairly well hidden because you usually have to go through java to encounter them: scala doesn't pay that much attention to generic signatures, so they can be wrong and scala might still generate correct code. But java is not so lucky. Bug #1) During mixin composition, classes which extend traits receive forwarders to the implementations. An attempt was made to give these the correct info (in method "cloneBeforeErasure") but it was prone to giving the wrong answer, because: the key attribute which the forwarder must capture is what the underlying method will erase to *where the implementation is*, not how it appears to the class which contains it. That means the signature of the forwarder must be no more precise than the signature of the inherited implementation unless additional measures will be taken. This subtle difference will put on an unsubtle show for you in test run/t3452.scala. trait C[T] trait Search[M] { def search(input: M): C[Int] = null } object StringSearch extends Search[String] { } StringSearch.search("test"); // java // java.lang.NoSuchMethodError: StringSearch.search(Ljava/lang/String;)LC; The principled thing to do here would be to create a pair of methods in the host class: a mixin forwarder with the erased signature `(String)C[Int]`, and a bridge method with the same erased signature as the trait interface facet. But, this turns out to be pretty hard to retrofit onto the current setup of Mixin and Erasure, mostly due to the fact that mixin happens after erasure which has already taken care of bridging. For a future, release, we should try to move all bridging after mixin, and pursue this approach. But for now, what can we do about `LinkageError`s for Java clients? This commit simply checks if the pre-erasure method signature that we generate for the trait forward erases identically to that of the interface method. If so, we can be precise. If not, we emit the erased signature as the generic signature. Bug #2) The same principle is at work, at a different location. During genjvm, objects without declared companion classes are given static forwarders in the corresponding class, e.g. object Foo { def bar = 5 } which creates these classes (taking minor liberties): class Foo$ { static val MODULE$ = new Foo$ ; def bar = 5 } class Foo { static def bar = Foo$.MODULE$.bar } In generating these, genjvm circumvented the usual process whereby one creates a symbol and gives it an info, preferring to target the bytecode directly. However generic signatures are calculated from symbol info (in this case reusing the info from the module class.) Lacking even the attempt which was being made in mixin to "clone before erasure", we would have runtime failures of this kind: abstract class Foo { type T def f(x: T): List[T] = List() } object Bar extends Foo { type T = String } Bar.f(""); // java // java.lang.NoSuchMethodError: Bar.f(Ljava/lang/String;)Lscala/collection/immutable/List; Before/after this commit: < signature f (Ljava/lang/String;)Lscala/collection/immutable/List<Ljava/lang/String;>; --- > signature f (Ljava/lang/Object;)Lscala/collection/immutable/List<Ljava/lang/Object;>; This takes the warning count for compiling collections under `-Ycheck:jvm` from 1521 to 26.
Diffstat (limited to 'tools')
-rw-r--r--tools/compare-java-sigs56
1 files changed, 56 insertions, 0 deletions
diff --git a/tools/compare-java-sigs b/tools/compare-java-sigs
new file mode 100644
index 0000000000..99ab775437
--- /dev/null
+++ b/tools/compare-java-sigs
@@ -0,0 +1,56 @@
+#!/bin/sh
+#
+# Compare javac -Xprint (i.e. see signatures from java point of view)
+# for the given classes.
+#
+# Sample:
+#
+# % SCALA_HOME=/scala/inst/29 SCALA_BUILD=/scala/inst/3 tools/compare-java-sigs 'scala.Predef$'
+#
+# Comparing javac -Xprint for scala.Predef$ based on '/scala/inst/29' and '/scala/inst/3'
+# 3c3
+# < public final class Predef$ extends scala.LowPriorityImplicits implements scala.ScalaObject {
+# ---
+# > public final class Predef$ extends scala.LowPriorityImplicits {
+# 7d6
+# < private final scala.SpecializableCompanion AnyRef;
+# 21,22d19
+# < public scala.SpecializableCompanion AnyRef();
+# <
+# 68a66,67
+# > public scala.runtime.Nothing$ $qmark$qmark$qmark();
+# >
+# 225c224,226
+# < public scala.collection.immutable.StringOps augmentString(java.lang.String x);
+# ---
+# > public scala.runtime.StringFormat any2stringfmt(java.lang.Object x);
+# >
+# > public java.lang.String augmentString(java.lang.String x);
+# 227c228
+# < public java.lang.String unaugmentString(scala.collection.immutable.StringOps x);
+# ---
+# > public java.lang.String unaugmentString(java.lang.String x);
+#
+
+set -e
+
+[[ $# -gt 0 ]] || {
+ echo "Usage: $(basename $0) <class> <class> ..."
+ echo ""
+ echo "# Example usage"
+ echo "SCALA_HOME=/scala/inst/29 SCALA_BUILD=/scala/inst/3 \\"
+ echo " $(basename $0) scala.Function1 scala.runtime.AbstractFunction1"
+ exit 0
+}
+
+home1=$(cd ${SCALA_HOME:-/scala/inst/3} && pwd)
+home2=$(cd ${SCALA_BUILD:-$(dirname $BASH_SOURCE)/../build/pack} && pwd)
+
+echo "Comparing javac -Xprint for $@ based on '$home1' and '$home2'"
+tmpdir=$(mktemp -dt $(basename $BASH_SOURCE))
+
+cd $tmpdir
+javac -Xprint -cp $home1:$home1/lib/'*' "$@" > before.txt
+javac -Xprint -cp $home2:$home2/lib/'*' "$@" > after.txt
+
+diff before.txt after.txt && echo "No differences in javac -Xprint output."