From ca9c8efac5694d6dbfe1d0393c2e7485a01c3ef5 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 26 Mar 2013 10:45:17 +0100 Subject: SI-6793 Don't use super param accessors if inaccessible. "Alias replacement" has been with us since 2005 (13c59adf9). Given: package a { class C1(val v0: String) class C2(v1: String) extends a.C1(v1) { v1 } } The reference to `v1` is rewritten as `C2.super.v0()`, and no field is generated in `C2`. (Oddly, this optimization doesn't seem to kick in if these classes are in the empty package. That's probably a distinct bug.) However, this rewriting is done without consideration of the accessibility of `v0` from `C2`. This commit disables this optimization if there if `v0` is not accessible. --- test/files/run/t6793.scala | 9 +++++++++ test/files/run/t6793b.scala | 11 +++++++++++ test/files/run/t6793c.scala | 11 +++++++++++ 3 files changed, 31 insertions(+) create mode 100644 test/files/run/t6793.scala create mode 100644 test/files/run/t6793b.scala create mode 100644 test/files/run/t6793c.scala (limited to 'test/files/run') diff --git a/test/files/run/t6793.scala b/test/files/run/t6793.scala new file mode 100644 index 0000000000..0b1f1619af --- /dev/null +++ b/test/files/run/t6793.scala @@ -0,0 +1,9 @@ +package a { class C1(private[a] val v0: String) } +package b { class C2(v1: String) extends a.C1(v1) { def foo = v1 } } + +object Test extends App { + new b.C2("x") + + val c2Fields = classOf[b.C2].getDeclaredFields + assert(c2Fields.size == 1, c2Fields.map(_.getName).toList) +} diff --git a/test/files/run/t6793b.scala b/test/files/run/t6793b.scala new file mode 100644 index 0000000000..cb3f2fb2fa --- /dev/null +++ b/test/files/run/t6793b.scala @@ -0,0 +1,11 @@ +package a { + class C1(val v0: String) + class C2(v1: String) extends a.C1(v1) { def foo = v1 } +} + +object Test extends App { + new a.C2("x") + + val c2Fields = classOf[a.C2].getDeclaredFields + assert(c2Fields.isEmpty, c2Fields.map(_.getName).mkString(", ")) +} diff --git a/test/files/run/t6793c.scala b/test/files/run/t6793c.scala new file mode 100644 index 0000000000..e28c7c81a1 --- /dev/null +++ b/test/files/run/t6793c.scala @@ -0,0 +1,11 @@ +package a { + class C1(private[a] val v0: String) + class C2(v1: String) extends a.C1(v1) { def foo = v1 } +} + +object Test extends App { + new a.C2("x").foo + + val c2Fields = classOf[a.C2].getDeclaredFields + assert(c2Fields.isEmpty, c2Fields.map(_.getName).toList) +} -- cgit v1.2.3 From 5f9bc0570f0e2fc4a80eee592bf2eb3eaddf1390 Mon Sep 17 00:00:00 2001 From: Kato Kazuyoshi Date: Wed, 20 Mar 2013 04:10:07 +0900 Subject: SI-6715 Shouldn't return "" from TermNames.originalName --- src/reflect/scala/reflect/internal/StdNames.scala | 2 +- test/files/run/t6715.scala | 15 +++++++++++++++ test/scaladoc/run/SI-6715.check | 1 + test/scaladoc/run/SI-6715.scala | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/files/run/t6715.scala create mode 100644 test/scaladoc/run/SI-6715.check create mode 100644 test/scaladoc/run/SI-6715.scala (limited to 'test/files/run') diff --git a/src/reflect/scala/reflect/internal/StdNames.scala b/src/reflect/scala/reflect/internal/StdNames.scala index d97eaabf72..de7af4340d 100644 --- a/src/reflect/scala/reflect/internal/StdNames.scala +++ b/src/reflect/scala/reflect/internal/StdNames.scala @@ -382,7 +382,7 @@ trait StdNames { * Otherwise `name` itself. */ def originalName(name: Name): Name = name.toString lastIndexOf "$$" match { - case -1 => name + case -1 | 0 => name case idx0 => // Sketchville - We've found $$ but if it's part of $$$ or $$$$ // or something we need to keep the bonus dollars, so e.g. foo$$$outer diff --git a/test/files/run/t6715.scala b/test/files/run/t6715.scala new file mode 100644 index 0000000000..07ff34218a --- /dev/null +++ b/test/files/run/t6715.scala @@ -0,0 +1,15 @@ +import scala.reflect.runtime.universe._ + +class A { + def $$ = 1 + def $times = 1 +} + +object Test { + def main(args: Array[String]): Unit = { + val memberSet: Set[String] = typeOf[A].members.map{ _.toString }.toSet + assert(memberSet contains "method *") + assert(memberSet contains "method $$") + assert(! (memberSet contains "method")) + } +} diff --git a/test/scaladoc/run/SI-6715.check b/test/scaladoc/run/SI-6715.check new file mode 100644 index 0000000000..619c56180b --- /dev/null +++ b/test/scaladoc/run/SI-6715.check @@ -0,0 +1 @@ +Done. diff --git a/test/scaladoc/run/SI-6715.scala b/test/scaladoc/run/SI-6715.scala new file mode 100644 index 0000000000..92d3376234 --- /dev/null +++ b/test/scaladoc/run/SI-6715.scala @@ -0,0 +1,15 @@ +import scala.tools.nsc.doc.model._ +import scala.tools.partest.ScaladocModelTest + +object Test extends ScaladocModelTest { + def scaladocSettings = "" + + override def code = "object A { def $$ = 123 }" + + def testModel(rootPackage: Package) = { + import access._ + + val method = rootPackage._object("A")._method("$$") + assert(method != null) + } +} -- cgit v1.2.3 From c2534bf61b61b76cebaad09ba303234193709b60 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Fri, 22 Feb 2013 15:53:29 +0100 Subject: SI-6900 Fix tailrec for dependent method types Uncurry's info transformer could generate a MethodType with cloned parameter symbols. This type was used for the LabelDef generated in the TailCalls phase. But, the RHS of the method still contains types that refer to the original parmameter symbol. Spurious type errors ensued. I've spent a good chunk of time pursuing a more principled fix, in which we keep the symbols in the tree in sync with those in the MethodType. You can relive the procession of false dawns: https://github.com/scala/scala/pull/2248 Ultimately that scheme was derailed by a mismatch between the type parameter `T` and the skolem `T&` in the example below. trait Endo[A] { def apply(a: => A): A } class Test { def foo[T] = new Endo[(T, Unit)] { def apply(v1: => (T, Unit)) = v1 // no bridge created } } Interestingly, by removing the caching in SingleType, I got past that problem. But I didn't characterize it further. This commit sets asides the noble goal of operating in the world of types, and sledgehammers past the crash by casting the arguments to and the result of the label jump generated in TailCalls. --- .../scala/tools/nsc/transform/TailCalls.scala | 14 +++++++-- test/files/run/t6900.scala | 36 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/files/run/t6900.scala (limited to 'test/files/run') diff --git a/src/compiler/scala/tools/nsc/transform/TailCalls.scala b/src/compiler/scala/tools/nsc/transform/TailCalls.scala index a767850cba..938499261e 100644 --- a/src/compiler/scala/tools/nsc/transform/TailCalls.scala +++ b/src/compiler/scala/tools/nsc/transform/TailCalls.scala @@ -220,7 +220,10 @@ abstract class TailCalls extends Transform { debuglog("Rewriting tail recursive call: " + fun.pos.lineContent.trim) accessed += ctx.label - typedPos(fun.pos)(Apply(Ident(ctx.label), noTailTransform(recv) :: transformArgs)) + typedPos(fun.pos) { + val args = mapWithIndex(transformArgs)((arg, i) => mkAttributedCastHack(arg, ctx.label.info.params(i + 1).tpe)) + Apply(Ident(ctx.label), noTailTransform(recv) :: args) + } } if (!ctx.isEligible) fail("it is neither private nor final so can be overridden") @@ -280,7 +283,7 @@ abstract class TailCalls extends Transform { typedPos(tree.pos)(Block( List(ValDef(newThis, This(currentClass))), - LabelDef(newCtx.label, newThis :: vpSyms, newRHS) + LabelDef(newCtx.label, newThis :: vpSyms, mkAttributedCastHack(newRHS, newCtx.label.tpe.resultType)) )) } else { @@ -377,6 +380,13 @@ abstract class TailCalls extends Transform { super.transform(tree) } } + + // Workaround for SI-6900. Uncurry installs an InfoTransformer and a tree Transformer. + // These leave us with conflicting view on method signatures; the parameter symbols in + // the MethodType can be clones of the ones originally found on the parameter ValDef, and + // consequently appearing in the typechecked RHS of the method. + private def mkAttributedCastHack(tree: Tree, tpe: Type) = + gen.mkAttributedCast(tree, tpe) } // collect the LabelDefs (generated by the pattern matcher) in a DefDef that are in tail position diff --git a/test/files/run/t6900.scala b/test/files/run/t6900.scala new file mode 100644 index 0000000000..a29d388129 --- /dev/null +++ b/test/files/run/t6900.scala @@ -0,0 +1,36 @@ +import annotation.tailrec + +trait Universe { + type T <: AnyRef +} + +final class Bug { + var i = 1 + def stop() = { i -= 1; i < 0 } + // the alias bypasses the fast path in erasures InfoTransformer + // predicated on `TypeMap.noChangeToSymbols` + type Alias = Any + + @tailrec + // So we get two symbols for `universe`, the original on the ValDef + // and a clone in the MethodType of `f`. + def f(universe: Universe, l: Alias): universe.T = { + if (stop()) null.asInstanceOf[universe.T] else f(universe, null) + } + + @tailrec + def g(universe: Universe)(l: Alias): universe.T = { + if (stop()) null.asInstanceOf[universe.T] else g(universe)(l) + } + + @tailrec + def h(universe: Universe)(l: List[universe.T]): List[universe.T] = { + if (stop()) Nil else h(universe)(l) + } +} + +object Test extends App { + assert(new Bug().f(null, null) == null) + assert(new Bug().g(null)(null) == null) + assert(new Bug().h(null)(null) == Nil) +} \ No newline at end of file -- cgit v1.2.3