From 089cc9fee43a84ce4b93bf3c51b22136216bd831 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 25 Nov 2012 14:19:43 -0800 Subject: Fix for SI-6712, bug in object lifting. --- test/files/pos/t6712.scala | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test/files/pos/t6712.scala (limited to 'test') diff --git a/test/files/pos/t6712.scala b/test/files/pos/t6712.scala new file mode 100644 index 0000000000..3c96eb14fb --- /dev/null +++ b/test/files/pos/t6712.scala @@ -0,0 +1,5 @@ +class H { + object O + + def foo() { object O } +} -- cgit v1.2.3 From 9aa6ded8e01179e7d55144de273b39f3a0b2d3ec Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Thu, 22 Nov 2012 14:20:18 +0100 Subject: SI-6667 Abort after any ambiguous in-scope implicit Rather than continuing on to a search of implicits of the expected type. Previously, the companion implicits were searched if the the caller was running the implicit search in silent mode, for example if searching for an implicit parameter in an application which has an expected type. After this commit, the behaviour is consistent, regardless on silent/non-silent typing. --- .../scala/tools/nsc/typechecker/Implicits.scala | 3 ++- test/files/neg/t6667.check | 13 +++++++++++ test/files/neg/t6667.scala | 10 +++++++++ test/files/neg/t6667b.check | 13 +++++++++++ test/files/neg/t6667b.scala | 25 ++++++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/files/neg/t6667.check create mode 100644 test/files/neg/t6667.scala create mode 100644 test/files/neg/t6667b.check create mode 100644 test/files/neg/t6667b.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala index 8f32f0e653..a74470a9cd 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala @@ -1399,11 +1399,12 @@ trait Implicits { val failstart = if (Statistics.canEnable) Statistics.startTimer(oftypeFailNanos) else null val succstart = if (Statistics.canEnable) Statistics.startTimer(oftypeSucceedNanos) else null + val wasAmbigious = result.isAmbiguousFailure // SI-6667, never search companions after an ambiguous error in in-scope implicits result = materializeImplicit(pt) // `materializeImplicit` does some preprocessing for `pt` // is it only meant for manifests/tags or we need to do the same for `implicitsOfExpectedType`? - if (result.isFailure) result = searchImplicit(implicitsOfExpectedType, false) + if (result.isFailure && !wasAmbigious) result = searchImplicit(implicitsOfExpectedType, false) if (result.isFailure) { context.updateBuffer(previousErrs) diff --git a/test/files/neg/t6667.check b/test/files/neg/t6667.check new file mode 100644 index 0000000000..43313fa4fe --- /dev/null +++ b/test/files/neg/t6667.check @@ -0,0 +1,13 @@ +t6667.scala:8: error: ambiguous implicit values: + both value inScope1 in object Test of type => C + and value inScope2 in object Test of type => C + match expected type C + implicitly[C]: Unit // C.companion was used; whereas the ambiguity should abort the implicit search. + ^ +t6667.scala:9: error: ambiguous implicit values: + both value inScope1 in object Test of type => C + and value inScope2 in object Test of type => C + match expected type C + implicitly[C] // ambiguity reported, rather than falling back to C.companion + ^ +two errors found diff --git a/test/files/neg/t6667.scala b/test/files/neg/t6667.scala new file mode 100644 index 0000000000..fb857ebd33 --- /dev/null +++ b/test/files/neg/t6667.scala @@ -0,0 +1,10 @@ +class C +object C { + implicit def companion = new C +} + +object Test { + implicit val inScope1, inScope2 = new C + implicitly[C]: Unit // C.companion was used; whereas the ambiguity should abort the implicit search. + implicitly[C] // ambiguity reported, rather than falling back to C.companion +} diff --git a/test/files/neg/t6667b.check b/test/files/neg/t6667b.check new file mode 100644 index 0000000000..99cea9a47c --- /dev/null +++ b/test/files/neg/t6667b.check @@ -0,0 +1,13 @@ +t6667b.scala:16: error: ambiguous implicit values: + both value a in object Test of type => Test.Box + and value b of type Test.Box + match expected type Test.Box + new Test() + ^ +t6667b.scala:19: error: ambiguous implicit values: + both value a in object Test of type => Test.Box + and value b of type Test.Box + match expected type Test.Box + new Test() + ^ +two errors found diff --git a/test/files/neg/t6667b.scala b/test/files/neg/t6667b.scala new file mode 100644 index 0000000000..4e64e1af17 --- /dev/null +++ b/test/files/neg/t6667b.scala @@ -0,0 +1,25 @@ +object Test { + abstract class Box { + val value: Int + } + + implicit val a: Box = new Box { + val value= 1 + } + + def main(args: Array[String]) { + implicit val b: Box= new Box { + val value= 2 + } + + new Object { + new Test() + } + // compare with: + new Test() + } +} + +class Test()(implicit x: Test.Box) { + println(x.value) +} -- cgit v1.2.3 From 7ee1145d4502396816da3fb0155960524de94079 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 2 Dec 2012 19:14:02 +0100 Subject: SI-6631 Handle invalid escapes in string interpolators Patch contributed by Rex Kerr. --- src/library/scala/StringContext.scala | 9 +++++---- test/files/run/t6631.scala | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/files/run/t6631.scala (limited to 'test') diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index 8be0cb1619..756077dd15 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -191,7 +191,7 @@ object StringContext { var cur = 0 var idx = 0 def output(ch: Char) = { - bldr append str.substring (start, cur) + bldr.append(str, start, cur) bldr append ch start = idx } @@ -199,14 +199,15 @@ object StringContext { cur = idx if (str(idx) == '\\') { idx += 1 + if (idx >= len) throw new InvalidEscapeException(str, cur) if ('0' <= str(idx) && str(idx) <= '7') { val leadch = str(idx) var oct = leadch - '0' idx += 1 - if ('0' <= str(idx) && str(idx) <= '7') { + if (idx < len && '0' <= str(idx) && str(idx) <= '7') { oct = oct * 8 + str(idx) - '0' idx += 1 - if (leadch <= '3' && '0' <= str(idx) && str(idx) <= '7') { + if (idx < len && leadch <= '3' && '0' <= str(idx) && str(idx) <= '7') { oct = oct * 8 + str(idx) - '0' idx += 1 } @@ -234,6 +235,6 @@ object StringContext { } } if (start == 0) str - else (bldr append str.substring(start, idx)).toString + else bldr.append(str, start, idx).toString } } diff --git a/test/files/run/t6631.scala b/test/files/run/t6631.scala new file mode 100644 index 0000000000..e472b83d50 --- /dev/null +++ b/test/files/run/t6631.scala @@ -0,0 +1,18 @@ +import reflect.ClassTag + +object Test extends App { + def intercept[T <: Throwable : ClassTag](act: => Any) = try { + act + } catch { + case x: Throwable => + val cls = implicitly[ClassTag[T]].runtimeClass + assert(cls.isInstance(x), (x.getClass, x, cls).toString) + } + assert(s"""\f\r\n\t""" == "\f\r\n\t") + + import StringContext.InvalidEscapeException + intercept[InvalidEscapeException](s"""\""") + intercept[InvalidEscapeException](s"""\x""") + intercept[InvalidEscapeException](s"\") + +} -- cgit v1.2.3 From d9928d59f9b21265f783fe781ad1d3a4ff93bb6e Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Thu, 25 Oct 2012 18:52:39 +0200 Subject: Fixes SI-6558: typecheck lazy annotation info using non-silent context. Make context for typing lazy annotations always non-silent. If lazy annotation info was created in local (silent) context, error could go unnoticed because later they would still use silent typer for typing the annotation. --- src/compiler/scala/tools/nsc/typechecker/Namers.scala | 6 +++++- test/files/neg/t6558.check | 4 ++++ test/files/neg/t6558.scala | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/files/neg/t6558.check create mode 100644 test/files/neg/t6558.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala index 36edd46f25..193c2bc76e 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala @@ -1260,7 +1260,11 @@ trait Namers extends MethodSynthesis { case defn: MemberDef => val ainfos = defn.mods.annotations filterNot (_ eq null) map { ann => // need to be lazy, #1782. beforeTyper to allow inferView in annotation args, SI-5892. - AnnotationInfo lazily beforeTyper(typer typedAnnotation ann) + AnnotationInfo lazily { + val context1 = typer.context.make(ann) + context1.setReportErrors() + beforeTyper(newTyper(context1) typedAnnotation ann) + } } if (ainfos.nonEmpty) { annotated setAnnotations ainfos diff --git a/test/files/neg/t6558.check b/test/files/neg/t6558.check new file mode 100644 index 0000000000..bd118edec8 --- /dev/null +++ b/test/files/neg/t6558.check @@ -0,0 +1,4 @@ +t6558.scala:5: error: not found: type sth + @sth + ^ +one error found diff --git a/test/files/neg/t6558.scala b/test/files/neg/t6558.scala new file mode 100644 index 0000000000..92c788f21e --- /dev/null +++ b/test/files/neg/t6558.scala @@ -0,0 +1,9 @@ +class AnnotNotFound { + def foo(a: Any) = () + + foo { + @sth + def foo = 0 + foo + } +} -- cgit v1.2.3 From c24400f13e594dffc2a3e68e161c8f36979bfd30 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 2 Dec 2012 21:26:17 +0100 Subject: SI-6558 Expand test case for annotation typos --- test/files/neg/t6558.check | 14 ++++++++++---- test/files/neg/t6558.scala | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/files/neg/t6558.check b/test/files/neg/t6558.check index bd118edec8..ea627b49ea 100644 --- a/test/files/neg/t6558.check +++ b/test/files/neg/t6558.check @@ -1,4 +1,10 @@ -t6558.scala:5: error: not found: type sth - @sth - ^ -one error found +t6558.scala:16: error: not found: type classs + @classs + ^ +t6558.scala:19: error: not found: type typeparam + class D[@typeparam T] + ^ +t6558.scala:22: error: not found: type valueparam + @valueparam x: Any + ^ +three errors found diff --git a/test/files/neg/t6558.scala b/test/files/neg/t6558.scala index 92c788f21e..cedd587c84 100644 --- a/test/files/neg/t6558.scala +++ b/test/files/neg/t6558.scala @@ -2,8 +2,23 @@ class AnnotNotFound { def foo(a: Any) = () foo { - @sth + @inargument def foo = 0 foo } + + () => { + @infunction + def foo = 0 + () + } + + @classs + class C + + class D[@typeparam T] + + class E( + @valueparam x: Any + ) } -- cgit v1.2.3 From d99b7f4e1c848bb749206f36b4bbaa17f24fa2e4 Mon Sep 17 00:00:00 2001 From: Miguel Garcia Date: Sun, 28 Oct 2012 20:08:09 +0100 Subject: SI-6547: elide box unbox pair only when primitives match --- src/compiler/scala/tools/nsc/backend/opt/ClosureElimination.scala | 6 +++--- test/files/pos/t6547.flags | 1 + test/files/pos/t6547.scala | 6 ++++++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 test/files/pos/t6547.flags create mode 100644 test/files/pos/t6547.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/backend/opt/ClosureElimination.scala b/src/compiler/scala/tools/nsc/backend/opt/ClosureElimination.scala index bb14c3dce0..23f932b5b4 100644 --- a/src/compiler/scala/tools/nsc/backend/opt/ClosureElimination.scala +++ b/src/compiler/scala/tools/nsc/backend/opt/ClosureElimination.scala @@ -147,18 +147,18 @@ abstract class ClosureElimination extends SubComponent { case _ => } - case UNBOX(_) => + case UNBOX(boxType) => info.stack match { case Deref(LocalVar(loc1)) :: _ if info.bindings isDefinedAt LocalVar(loc1) => val value = info.getBinding(loc1) value match { - case Boxed(LocalVar(loc2)) => + case Boxed(LocalVar(loc2)) if loc2.kind == boxType => bb.replaceInstruction(i, DROP(icodes.ObjectReference) :: valueToInstruction(info.getBinding(loc2)) :: Nil) debuglog("replaced " + i + " with " + info.getBinding(loc2)) case _ => () } - case Boxed(LocalVar(loc1)) :: _ => + case Boxed(LocalVar(loc1)) :: _ if loc1.kind == boxType => val loc2 = info.getAlias(loc1) bb.replaceInstruction(i, DROP(icodes.ObjectReference) :: valueToInstruction(Deref(LocalVar(loc2))) :: Nil) debuglog("replaced " + i + " with " + LocalVar(loc2)) diff --git a/test/files/pos/t6547.flags b/test/files/pos/t6547.flags new file mode 100644 index 0000000000..c9b68d70dc --- /dev/null +++ b/test/files/pos/t6547.flags @@ -0,0 +1 @@ +-optimise diff --git a/test/files/pos/t6547.scala b/test/files/pos/t6547.scala new file mode 100644 index 0000000000..53bd798219 --- /dev/null +++ b/test/files/pos/t6547.scala @@ -0,0 +1,6 @@ +trait ConfigurableDefault[@specialized V] { + def fillArray(arr: Array[V], v: V) = (arr: Any) match { + case x: Array[Int] => null + case x: Array[Long] => v.asInstanceOf[Long] + } +} -- cgit v1.2.3 From 673bc700fc32fa18941554533dfd31bf2523d45e Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 3 Dec 2012 19:12:20 +0100 Subject: Split test case to workaround incomplete error report. Reporting some errors prevents reporting others. This residual issue had been lodged as SI-6758. --- test/files/neg/t6558.check | 6 +++--- test/files/neg/t6558.scala | 3 +++ test/files/neg/t6558b.check | 7 +++++++ test/files/neg/t6558b.scala | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/files/neg/t6558b.check create mode 100644 test/files/neg/t6558b.scala (limited to 'test') diff --git a/test/files/neg/t6558.check b/test/files/neg/t6558.check index ea627b49ea..1b39ef9986 100644 --- a/test/files/neg/t6558.check +++ b/test/files/neg/t6558.check @@ -1,10 +1,10 @@ -t6558.scala:16: error: not found: type classs +t6558.scala:19: error: not found: type classs @classs ^ -t6558.scala:19: error: not found: type typeparam +t6558.scala:22: error: not found: type typeparam class D[@typeparam T] ^ -t6558.scala:22: error: not found: type valueparam +t6558.scala:25: error: not found: type valueparam @valueparam x: Any ^ three errors found diff --git a/test/files/neg/t6558.scala b/test/files/neg/t6558.scala index cedd587c84..bdc441698f 100644 --- a/test/files/neg/t6558.scala +++ b/test/files/neg/t6558.scala @@ -2,12 +2,15 @@ class AnnotNotFound { def foo(a: Any) = () foo { + // Not yet issued in the context of this file, see SI-6758 + // This error is issued in t6558b.scala @inargument def foo = 0 foo } () => { + // As per above @infunction def foo = 0 () diff --git a/test/files/neg/t6558b.check b/test/files/neg/t6558b.check new file mode 100644 index 0000000000..cfa384fc08 --- /dev/null +++ b/test/files/neg/t6558b.check @@ -0,0 +1,7 @@ +t6558b.scala:5: error: not found: type inargument + @inargument + ^ +t6558b.scala:11: error: not found: type infunction + @infunction + ^ +two errors found diff --git a/test/files/neg/t6558b.scala b/test/files/neg/t6558b.scala new file mode 100644 index 0000000000..2aa06f69cf --- /dev/null +++ b/test/files/neg/t6558b.scala @@ -0,0 +1,15 @@ +class AnnotNotFound { + def foo(a: Any) = () + + foo { + @inargument + def foo = 0 + foo + } + + () => { + @infunction + def foo = 0 + () + } +} -- cgit v1.2.3 From 90c87fc266ec45e8970f6ea0f00d375b63afd35d Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 3 Dec 2012 21:58:36 +0100 Subject: SI-6549 Improve escaping in REPL codegen. - Escape the LHS of an assign when printing results - e.g. X("").foo = bar - Escape val names - e.g. val `"` = 0` --- .../tools/nsc/interpreter/MemberHandlers.scala | 5 ++-- test/files/run/t6549.check | 32 ++++++++++++++++++++++ test/files/run/t6549.scala | 22 +++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 test/files/run/t6549.check create mode 100644 test/files/run/t6549.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala b/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala index 12fb8f1507..67519cf90c 100644 --- a/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala +++ b/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala @@ -120,7 +120,7 @@ trait MemberHandlers { if (replProps.vids) """" + " @ " + "%%8x".format(System.identityHashCode(%s)) + " """.trim.format(req fullPath name) else "" - """ + "%s%s: %s = " + %s""".format(prettyName, vidString, string2code(req typeOf name), resultString) + """ + "%s%s: %s = " + %s""".format(string2code(prettyName), vidString, string2code(req typeOf name), resultString) } } } @@ -147,8 +147,7 @@ trait MemberHandlers { override def resultExtractionCode(req: Request) = { val lhsType = string2code(req lookupTypeOf name) val res = string2code(req fullPath name) - - """ + "%s: %s = " + %s + "\n" """.format(lhs, lhsType, res) + "\n" + """ + "%s: %s = " + %s + "\n" """.format(string2code(lhs.toString), lhsType, res) + "\n" } } diff --git a/test/files/run/t6549.check b/test/files/run/t6549.check new file mode 100644 index 0000000000..bc78aac741 --- /dev/null +++ b/test/files/run/t6549.check @@ -0,0 +1,32 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> + +scala> case class `X"`(var xxx: Any) +defined class X$u0022 + +scala> val m = Map(("": Any) -> `X"`("\""), ('s: Any) -> `X"`("\"")) +m: scala.collection.immutable.Map[Any,X"] = Map("" -> X"("), 's -> X"(")) + +scala> m("") +res0: X" = X"(") + +scala> m("").xxx +res1: Any = " + +scala> m("").xxx = 0 +m("").xxx: Any = 0 + +scala> m("").xxx = "\"" +m("").xxx: Any = " + +scala> m('s).xxx = 's +m(scala.Symbol("s")).xxx: Any = 's + +scala> val `"` = 0 +": Int = 0 + +scala> + +scala> diff --git a/test/files/run/t6549.scala b/test/files/run/t6549.scala new file mode 100644 index 0000000000..7335661dc7 --- /dev/null +++ b/test/files/run/t6549.scala @@ -0,0 +1,22 @@ +import scala.tools.partest.ReplTest + +// Check that the fragments of code generated in +// in the REPL correctly escape values added to +// literal strings. +// +// Before, we saw: +// scala> m("").x = 77 +// :10: error: ')' expected but string literal found. +// + "m("").x: Int = " + `$ires8` + "\n" +object Test extends ReplTest { + def code = """ + |case class `X"`(var xxx: Any) + |val m = Map(("": Any) -> `X"`("\""), ('s: Any) -> `X"`("\"")) + |m("") + |m("").xxx + |m("").xxx = 0 + |m("").xxx = "\"" + |m('s).xxx = 's + |val `"` = 0 + """.stripMargin +} -- cgit v1.2.3 From 2aa66bec86fd464712b0d15251cc400ff9d52821 Mon Sep 17 00:00:00 2001 From: Jean-Remi Desjardins Date: Tue, 4 Dec 2012 00:37:34 -0500 Subject: SI-4664 [Make scala.util.Random Serializable] Add test case --- test/files/jvm/serialization.scala | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test') diff --git a/test/files/jvm/serialization.scala b/test/files/jvm/serialization.scala index 34b64938b4..f2c47aad77 100644 --- a/test/files/jvm/serialization.scala +++ b/test/files/jvm/serialization.scala @@ -604,6 +604,7 @@ object Test { Test7 Test8 Test9_parallel + Test10_util } } @@ -669,3 +670,17 @@ object Test9_parallel { throw e } } + +//############################################################################ +// Test classes in package scala.util + +object Test10_util { + import scala.util.Random + def rep[A](n: Int)(f: => A) { if (n > 0) { f; rep(n-1)(f) } } + + try { + val random = new Random(345) + val random2: Random = read(write(random)) + rep(5) { assert(random.nextInt == random2.nextInt) } + } +} -- cgit v1.2.3 From 8a1f85d521399be9141db1da2b0d2bd67cfb39e9 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 4 Dec 2012 00:24:42 +0100 Subject: SI-6535 Step back from the precipice of a cycle Adding any non-local parent to WrapAsScala will trigger a valid cyclic reference error. By moving the import of `Wrapper._` inside `WrapAsScala` and `WrapAsJava`, it is not in scope when typing the parents of those, and we avoid the cycle. Adds a test case to show the essense of the promiscious mutual imports that triggers this. --- src/library/scala/collection/convert/WrapAsJava.scala | 3 ++- src/library/scala/collection/convert/WrapAsScala.scala | 5 ++++- test/files/neg/t6535.check | 6 ++++++ test/files/neg/t6535.scala | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/files/neg/t6535.check create mode 100644 test/files/neg/t6535.scala (limited to 'test') diff --git a/src/library/scala/collection/convert/WrapAsJava.scala b/src/library/scala/collection/convert/WrapAsJava.scala index 9115b9e5fb..5e6126a7cf 100644 --- a/src/library/scala/collection/convert/WrapAsJava.scala +++ b/src/library/scala/collection/convert/WrapAsJava.scala @@ -10,10 +10,11 @@ package scala.collection package convert import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } -import Wrappers._ import scala.language.implicitConversions trait WrapAsJava { + import Wrappers._ + /** * Implicitly converts a Scala Iterator to a Java Iterator. * The returned Java Iterator is backed by the provided Scala diff --git a/src/library/scala/collection/convert/WrapAsScala.scala b/src/library/scala/collection/convert/WrapAsScala.scala index 5a5d204ece..ffcca62291 100644 --- a/src/library/scala/collection/convert/WrapAsScala.scala +++ b/src/library/scala/collection/convert/WrapAsScala.scala @@ -10,11 +10,13 @@ package scala.collection package convert import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } -import Wrappers._ import scala.language.implicitConversions trait LowPriorityWrapAsScala { this: WrapAsScala => + + import Wrappers._ + /** * Implicitly converts a Java ConcurrentMap to a Scala mutable ConcurrentMap. * The returned Scala ConcurrentMap is backed by the provided Java @@ -34,6 +36,7 @@ trait LowPriorityWrapAsScala { } trait WrapAsScala extends LowPriorityWrapAsScala { + import Wrappers._ /** * Implicitly converts a Java `Iterator` to a Scala `Iterator`. * diff --git a/test/files/neg/t6535.check b/test/files/neg/t6535.check new file mode 100644 index 0000000000..1225ea70db --- /dev/null +++ b/test/files/neg/t6535.check @@ -0,0 +1,6 @@ +t6535.scala:2: error: encountered unrecoverable cycle resolving import. +Note: this is often due in part to a class depending on a definition nested within its companion. +If applicable, you may wish to try moving some members into another object. + import Bs.B._ + ^ +one error found diff --git a/test/files/neg/t6535.scala b/test/files/neg/t6535.scala new file mode 100644 index 0000000000..30a750311c --- /dev/null +++ b/test/files/neg/t6535.scala @@ -0,0 +1,15 @@ +object As { + import Bs.B._ + + object A + extends scala.AnyRef // needed for the cycle; + // replacing with a locally defined closs doesn't + // hit the locked import and hence doesn't cycle. +} + +object Bs { + import As.A._ + + object B + extends scala.AnyRef // scala.Immutable, ... +} -- cgit v1.2.3 From 96e5c402a6d48de699fe48cd0eaf33d0575eaac7 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 4 Dec 2012 11:24:30 +0100 Subject: SI-5877 Support implicit classes in package objects This used to crash, as both the package and the package object had the synthetic method in `decls`, and the typer tried to add the tree to both places. Now, synthetics in the package object are excluded from the pacakge itself. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 5 ++++- test/files/pos/t5877.scala | 14 ++++++++++++++ test/files/pos/t5877b.scala | 13 +++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/t5877.scala create mode 100644 test/files/pos/t5877b.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 5714c2c109..9af72c1c00 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -2828,7 +2828,10 @@ trait Typers extends Modes with Adaptations with Tags { var moreToAdd = true while (moreToAdd) { val initElems = scope.elems - for (sym <- scope) + // SI-5877 The decls of a package include decls of the package object. But we don't want to add + // the corresponding synthetics to the package itself. + def isPackageObjectSym(sym: Symbol) = sym.owner.isPackageObjectClass && context.owner.isPackage + for (sym <- scope if !isPackageObjectSym(sym)) for (tree <- context.unit.synthetics get sym) { newStats += typedStat(tree) // might add even more synthetics to the scope context.unit.synthetics -= sym diff --git a/test/files/pos/t5877.scala b/test/files/pos/t5877.scala new file mode 100644 index 0000000000..c7827df99f --- /dev/null +++ b/test/files/pos/t5877.scala @@ -0,0 +1,14 @@ +package foo { + class Foo + + object Test { + new Foo().huzzah + } +} + +package object foo { + // Crasher: No synthetics for method PimpedFoo2: synthetics contains + implicit class PimpedFoo2(value: Foo) { + def huzzah = "" + } +} diff --git a/test/files/pos/t5877b.scala b/test/files/pos/t5877b.scala new file mode 100644 index 0000000000..6b8cbd473e --- /dev/null +++ b/test/files/pos/t5877b.scala @@ -0,0 +1,13 @@ +package foo + +class Foo + +object Test { + new Foo().huzzah +} + +object `package` { + implicit class PimpedFoo2(value: Foo) { + def huzzah = "" + } +} -- cgit v1.2.3 From 502818157671ff1d62264091b0102d05a42ff352 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 4 Dec 2012 12:35:13 +0100 Subject: tests for idempotency issues in the typechecker For more details see https://issues.scala-lang.org/browse/SI-5464. Check files are intentionally very precise, so that we can monitor how the situation changes over time. --- test/files/run/idempotency-case-classes.check | 52 ++++++++++++++++++++++ test/files/run/idempotency-case-classes.scala | 22 +++++++++ test/files/run/idempotency-extractors.check | 5 +++ test/files/run/idempotency-extractors.scala | 22 +++++++++ test/files/run/idempotency-labels.check | 15 +++++++ test/files/run/idempotency-labels.scala | 22 +++++++++ test/files/run/idempotency-lazy-vals.check | 23 ++++++++++ test/files/run/idempotency-lazy-vals.scala | 27 +++++++++++ test/files/run/idempotency-partial-functions.check | 2 + test/files/run/idempotency-partial-functions.scala | 25 +++++++++++ test/files/run/idempotency-this.check | 4 ++ test/files/run/idempotency-this.scala | 22 +++++++++ 12 files changed, 241 insertions(+) create mode 100644 test/files/run/idempotency-case-classes.check create mode 100644 test/files/run/idempotency-case-classes.scala create mode 100644 test/files/run/idempotency-extractors.check create mode 100644 test/files/run/idempotency-extractors.scala create mode 100644 test/files/run/idempotency-labels.check create mode 100644 test/files/run/idempotency-labels.scala create mode 100644 test/files/run/idempotency-lazy-vals.check create mode 100644 test/files/run/idempotency-lazy-vals.scala create mode 100644 test/files/run/idempotency-partial-functions.check create mode 100644 test/files/run/idempotency-partial-functions.scala create mode 100644 test/files/run/idempotency-this.check create mode 100644 test/files/run/idempotency-this.scala (limited to 'test') diff --git a/test/files/run/idempotency-case-classes.check b/test/files/run/idempotency-case-classes.check new file mode 100644 index 0000000000..d2fb8169cb --- /dev/null +++ b/test/files/run/idempotency-case-classes.check @@ -0,0 +1,52 @@ +C(2,3) +() +{ + case class C extends AnyRef with Product with Serializable { + private[this] val x: Int = _; + def x: Int = C.this.x; + private[this] val y: Int = _; + def y: Int = C.this.y; + def (x: Int, y: Int): C = { + C.super.(); + () + }; + def copy(x: Int = x, y: Int = y): C = new C(x, y); + def copy$default$1: Int = C.this.x; + def copy$default$2: Int = C.this.y; + override def productPrefix: String = "C"; + def productArity: Int = 2; + def productElement(x$1: Int): Any = x$1 match { + case 0 => C.this.x + case 1 => C.this.y + case _ => throw new IndexOutOfBoundsException(x$1.toString()) + }; + override def productIterator: Iterator[Any] = runtime.this.ScalaRunTime.typedProductIterator[Any](C.this); + def canEqual(x$1: Any): Boolean = x$1.$isInstanceOf[C](); + override def hashCode(): Int = { + var acc: Int = -889275714; + acc = Statics.this.mix(acc, x); + acc = Statics.this.mix(acc, y); + Statics.this.finalizeHash(acc, 2) + }; + override def toString(): String = ScalaRunTime.this._toString(C.this); + override def equals(x$1: Any): Boolean = C.this.eq(x$1.asInstanceOf[Object]).||(x$1.isInstanceOf[C].&&({ + val C$1: C = x$1.asInstanceOf[C]; + C.this.x.==(C$1.x).&&(C.this.y.==(C$1.y)).&&(C$1.canEqual(C.this)) + })) + }; + object C extends scala.runtime.AbstractFunction2[Int,Int,C] with Serializable { + def (): C.type = { + C.super.(); + () + }; + final override def toString(): String = "C"; + case def apply(x: Int, y: Int): C = new C(x, y); + case def unapply(x$0: C): Option[(Int, Int)] = if (x$0.==(null)) + scala.this.None + else + Some.apply[(Int, Int)](Tuple2.apply[Int, Int](x$0.x, x$0.y)); + private def readResolve(): Object = C + }; + scala.this.Predef.println(C.apply(2, 3)) +} +error! diff --git a/test/files/run/idempotency-case-classes.scala b/test/files/run/idempotency-case-classes.scala new file mode 100644 index 0000000000..4da8393cb6 --- /dev/null +++ b/test/files/run/idempotency-case-classes.scala @@ -0,0 +1,22 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import scala.tools.reflect.Eval + +object Test extends App { + val casee = reify { + case class C(x: Int, y: Int) + println(C(2, 3)) + } + println(casee.eval) + val tb = cm.mkToolBox() + val tcasee = tb.typeCheck(casee.tree) + println(tcasee) + val rtcasee = tb.resetAllAttrs(tcasee) + try { + println(tb.eval(rtcasee)) + } catch { + // this is the current behaviour, rather than the desired behavior; see SI-5467 + case _: ToolBoxError => println("error!") + } +} \ No newline at end of file diff --git a/test/files/run/idempotency-extractors.check b/test/files/run/idempotency-extractors.check new file mode 100644 index 0000000000..fcd50faa79 --- /dev/null +++ b/test/files/run/idempotency-extractors.check @@ -0,0 +1,5 @@ +2 +2 match { + case Test.this.Extractor.unapply() ((x @ _)) => x +} +error! diff --git a/test/files/run/idempotency-extractors.scala b/test/files/run/idempotency-extractors.scala new file mode 100644 index 0000000000..fe033295f5 --- /dev/null +++ b/test/files/run/idempotency-extractors.scala @@ -0,0 +1,22 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import scala.tools.reflect.Eval + +object Test extends App { + object Extractor { def unapply(x: Int): Option[Int] = Some(x) } + val extractor = reify { + 2 match { case Extractor(x) => x } + } + println(extractor.eval) + val tb = cm.mkToolBox() + val textractor = tb.typeCheck(extractor.tree) + println(textractor) + val rtextractor = tb.resetAllAttrs(textractor) + try { + println(tb.eval(rtextractor)) + } catch { + // this is the current behaviour, rather than the desired behavior; see SI-5465 + case _: ToolBoxError => println("error!") + } +} \ No newline at end of file diff --git a/test/files/run/idempotency-labels.check b/test/files/run/idempotency-labels.check new file mode 100644 index 0000000000..8709efeb43 --- /dev/null +++ b/test/files/run/idempotency-labels.check @@ -0,0 +1,15 @@ +2 +{ + var x: Int = 0; + while$1(){ + if (x.<(2)) + { + x = x.+(1); + while$1() + } + else + () + }; + x +} +2 diff --git a/test/files/run/idempotency-labels.scala b/test/files/run/idempotency-labels.scala new file mode 100644 index 0000000000..82d009751a --- /dev/null +++ b/test/files/run/idempotency-labels.scala @@ -0,0 +1,22 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import scala.tools.reflect.Eval + +object Test extends App { + val label = reify { + var x = 0 + while (x < 2) { x += 1 } + x + } + println(label.eval) + val tb = cm.mkToolBox() + val tlabel = tb.typeCheck(label.tree) + println(tlabel) + val rtlabel = tb.resetAllAttrs(tlabel) + try { + println(tb.eval(rtlabel)) + } catch { + case _: ToolBoxError => println("error!") + } +} \ No newline at end of file diff --git a/test/files/run/idempotency-lazy-vals.check b/test/files/run/idempotency-lazy-vals.check new file mode 100644 index 0000000000..4dcbf96184 --- /dev/null +++ b/test/files/run/idempotency-lazy-vals.check @@ -0,0 +1,23 @@ +6 +{ + class C extends AnyRef { + def (): C = { + C.super.(); + () + }; + lazy private[this] val x: Int = _; + lazy def x: Int = { + C.this.x = 2; + C.this.x + }; + lazy private[this] val y: Int = _; + implicit lazy def y: Int = { + C.this.y = 3; + C.this.y + } + }; + val c: C = new C(); + import c._; + c.x.*(scala.this.Predef.implicitly[Int](c.y)) +} +error! diff --git a/test/files/run/idempotency-lazy-vals.scala b/test/files/run/idempotency-lazy-vals.scala new file mode 100644 index 0000000000..3531f9ff4b --- /dev/null +++ b/test/files/run/idempotency-lazy-vals.scala @@ -0,0 +1,27 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import scala.tools.reflect.Eval + +object Test extends App { + val lazee = reify { + class C { + lazy val x = 2 + implicit lazy val y = 3 + } + val c = new C() + import c._ + x * implicitly[Int] + } + println(lazee.eval) + val tb = cm.mkToolBox() + val tlazee = tb.typeCheck(lazee.tree) + println(tlazee) + val rtlazee = tb.resetAllAttrs(tlazee) + try { + println(tb.eval(rtlazee)) + } catch { + // this is the current behaviour, rather than the desired behavior; see SI-5466 + case _: ToolBoxError => println("error!") + } +} \ No newline at end of file diff --git a/test/files/run/idempotency-partial-functions.check b/test/files/run/idempotency-partial-functions.check new file mode 100644 index 0000000000..5c8a411655 --- /dev/null +++ b/test/files/run/idempotency-partial-functions.check @@ -0,0 +1,2 @@ +error!! +error! diff --git a/test/files/run/idempotency-partial-functions.scala b/test/files/run/idempotency-partial-functions.scala new file mode 100644 index 0000000000..dd5f1167f1 --- /dev/null +++ b/test/files/run/idempotency-partial-functions.scala @@ -0,0 +1,25 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import scala.tools.reflect.Eval + +object Test extends App { + val partials = reify { + List((false,true)) collect { case (x,true) => x } + } + try { + println(partials.eval) + } catch { + case _: ToolBoxError => println("error!!") + } + try { + val tb = cm.mkToolBox() + val tpartials = tb.typeCheck(partials.tree) + println(tpartials) + val rtpartials = tb.resetAllAttrs(tpartials) + println(tb.eval(rtpartials)) + } catch { + // this is the current behaviour, rather than the desired behavior; see SI-6187 + case _: ToolBoxError => println("error!") + } +} \ No newline at end of file diff --git a/test/files/run/idempotency-this.check b/test/files/run/idempotency-this.check new file mode 100644 index 0000000000..08738c4565 --- /dev/null +++ b/test/files/run/idempotency-this.check @@ -0,0 +1,4 @@ +List() +immutable.this.List.apply[String]("") +Apply(TypeApply(Select(Select(This(newTypeName("immutable")), scala.collection.immutable.List), newTermName("apply")), List(TypeTree().setOriginal(Ident(newTypeName("String"))))), List(Literal(Constant("")))) +error! diff --git a/test/files/run/idempotency-this.scala b/test/files/run/idempotency-this.scala new file mode 100644 index 0000000000..5cd4226326 --- /dev/null +++ b/test/files/run/idempotency-this.scala @@ -0,0 +1,22 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.{ToolBox, ToolBoxError} +import scala.tools.reflect.Eval + +object Test extends App { + val thiss = reify { + List[String]("") + } + println(thiss.eval) + val tb = cm.mkToolBox() + val tthiss = tb.typeCheck(thiss.tree) + println(tthiss) + println(showRaw(tthiss)) + val rtthiss = tb.resetAllAttrs(tthiss) + try { + println(tb.eval(rtthiss)) + } catch { + // this is the current behaviour, rather than the desired behavior; see SI-5705 + case _: ToolBoxError => println("error!") + } +} \ No newline at end of file -- cgit v1.2.3 From 31a0aa75cf9739b2ae565d634670f6c0ce89bbc1 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Wed, 5 Dec 2012 14:16:27 +0100 Subject: SI-1672 Catches are in tail position without finally. So we can eliminate tail calls within. --- .../scala/tools/nsc/transform/TailCalls.scala | 10 +++++- test/files/neg/t1672b.check | 13 +++++++ test/files/neg/t1672b.scala | 41 ++++++++++++++++++++++ test/files/pos/t1672.scala | 10 ++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 test/files/neg/t1672b.check create mode 100644 test/files/neg/t1672b.scala create mode 100644 test/files/pos/t1672.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/transform/TailCalls.scala b/src/compiler/scala/tools/nsc/transform/TailCalls.scala index 95cb052fda..a767850cba 100644 --- a/src/compiler/scala/tools/nsc/transform/TailCalls.scala +++ b/src/compiler/scala/tools/nsc/transform/TailCalls.scala @@ -327,8 +327,16 @@ abstract class TailCalls extends Transform { transformTrees(cases).asInstanceOf[List[CaseDef]] ) + case Try(block, catches, finalizer @ EmptyTree) => + // SI-1672 Catches are in tail position when there is no finalizer + treeCopy.Try(tree, + noTailTransform(block), + transformTrees(catches).asInstanceOf[List[CaseDef]], + EmptyTree + ) + case Try(block, catches, finalizer) => - // no calls inside a try are in tail position, but keep recursing for nested functions + // no calls inside a try are in tail position if there is a finalizer, but keep recursing for nested functions treeCopy.Try(tree, noTailTransform(block), noTailTransforms(catches).asInstanceOf[List[CaseDef]], diff --git a/test/files/neg/t1672b.check b/test/files/neg/t1672b.check new file mode 100644 index 0000000000..288e47c315 --- /dev/null +++ b/test/files/neg/t1672b.check @@ -0,0 +1,13 @@ +t1672b.scala:3: error: could not optimize @tailrec annotated method bar: it contains a recursive call not in tail position + def bar : Nothing = { + ^ +t1672b.scala:14: error: could not optimize @tailrec annotated method baz: it contains a recursive call not in tail position + def baz : Nothing = { + ^ +t1672b.scala:29: error: could not optimize @tailrec annotated method boz: it contains a recursive call not in tail position + case _: Throwable => boz; ??? + ^ +t1672b.scala:34: error: could not optimize @tailrec annotated method bez: it contains a recursive call not in tail position + def bez : Nothing = { + ^ +four errors found diff --git a/test/files/neg/t1672b.scala b/test/files/neg/t1672b.scala new file mode 100644 index 0000000000..7d02ddc710 --- /dev/null +++ b/test/files/neg/t1672b.scala @@ -0,0 +1,41 @@ +object Test { + @annotation.tailrec + def bar : Nothing = { + try { + throw new RuntimeException + } catch { + case _: Throwable => bar + } finally { + bar + } + } + + @annotation.tailrec + def baz : Nothing = { + try { + throw new RuntimeException + } catch { + case _: Throwable => baz + } finally { + ??? + } + } + + @annotation.tailrec + def boz : Nothing = { + try { + throw new RuntimeException + } catch { + case _: Throwable => boz; ??? + } + } + + @annotation.tailrec + def bez : Nothing = { + try { + bez + } finally { + ??? + } + } +} diff --git a/test/files/pos/t1672.scala b/test/files/pos/t1672.scala new file mode 100644 index 0000000000..5ee6bb1759 --- /dev/null +++ b/test/files/pos/t1672.scala @@ -0,0 +1,10 @@ +object Test { + @annotation.tailrec + def bar : Nothing = { + try { + throw new RuntimeException + } catch { + case _: Throwable => bar + } + } +} -- cgit v1.2.3 From a23cc20ed5d3795584cb9ed74c0cd4bda25c2df1 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Wed, 5 Dec 2012 15:19:19 +0100 Subject: SI-5894 Don't emit static forwarders for macros. --- .../scala/tools/nsc/backend/jvm/GenJVMASM.scala | 2 +- test/files/run/t5894.scala | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/files/run/t5894.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenJVMASM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenJVMASM.scala index 35e0c3bd65..49c0fa2757 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenJVMASM.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenJVMASM.scala @@ -36,7 +36,7 @@ trait GenJVMASM { protected val ExcludedForwarderFlags = { import Flags._ // Should include DEFERRED but this breaks findMember. - ( CASE | SPECIALIZED | LIFTED | PROTECTED | STATIC | EXPANDEDNAME | BridgeAndPrivateFlags ) + ( CASE | SPECIALIZED | LIFTED | PROTECTED | STATIC | EXPANDEDNAME | BridgeAndPrivateFlags | MACRO ) } protected def isJavaEntryPoint(icls: IClass) = { diff --git a/test/files/run/t5894.scala b/test/files/run/t5894.scala new file mode 100644 index 0000000000..abeec32365 --- /dev/null +++ b/test/files/run/t5894.scala @@ -0,0 +1,17 @@ +import language.experimental.macros + +class Test + +object Test { + def foo = macro fooImpl + def fooImpl(c: reflect.macros.Context) = c.literalUnit + + def main(args: Array[String]) { + try { + val method = classOf[Test].getMethod("foo") + sys.error("Static forwarder generated for macro: " + method) + } catch { + case _: NoSuchMethodException => // okay + } + } +} -- cgit v1.2.3 From 8434922d6f0ead95a2109a5e41f4db4136449e93 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Wed, 5 Dec 2012 16:44:53 +0100 Subject: Addtional test cases for tail calls in catches. - Includes a run test to check bytecode verifies and behaves - Show this isn't possible when try is used as an expression, and a `liftedTree` local method is needed. --- test/files/neg/t1672b.check | 5 ++++- test/files/neg/t1672b.scala | 11 +++++++++++ test/files/run/t1672.scala | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/files/run/t1672.scala (limited to 'test') diff --git a/test/files/neg/t1672b.check b/test/files/neg/t1672b.check index 288e47c315..60ccf77174 100644 --- a/test/files/neg/t1672b.check +++ b/test/files/neg/t1672b.check @@ -10,4 +10,7 @@ t1672b.scala:29: error: could not optimize @tailrec annotated method boz: it con t1672b.scala:34: error: could not optimize @tailrec annotated method bez: it contains a recursive call not in tail position def bez : Nothing = { ^ -four errors found +t1672b.scala:46: error: could not optimize @tailrec annotated method bar: it contains a recursive call not in tail position + else 1 + (try { + ^ +5 errors found diff --git a/test/files/neg/t1672b.scala b/test/files/neg/t1672b.scala index 7d02ddc710..0ccdd03633 100644 --- a/test/files/neg/t1672b.scala +++ b/test/files/neg/t1672b.scala @@ -38,4 +38,15 @@ object Test { ??? } } + + // the `liftedTree` local method will prevent a tail call here. + @annotation.tailrec + def bar(i : Int) : Int = { + if (i == 0) 0 + else 1 + (try { + throw new RuntimeException + } catch { + case _: Throwable => bar(i - 1) + }) + } } diff --git a/test/files/run/t1672.scala b/test/files/run/t1672.scala new file mode 100644 index 0000000000..ee025b9031 --- /dev/null +++ b/test/files/run/t1672.scala @@ -0,0 +1,28 @@ +object Test { + @annotation.tailrec + def bar(i : Int) : Int = { + if (i == 0) 0 + else try { + throw new RuntimeException + } catch { + case _: Throwable => bar(i - 1) + } + } + + @annotation.tailrec + def nestedTry1(i : Int) : Int = { + if (i == 0) 0 + else try { + throw new RuntimeException + } catch { + case _: Throwable => + try { ??? } catch { case _: Throwable => nestedTry1(i - 1) } + } + } + + def main(args: Array[String]) { + assert(bar(2) == 0) + + assert(nestedTry1(2) == 0) + } +} -- cgit v1.2.3 From e4d1d930693ac75d8eb64c2c3c69f2fc22bec739 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 4 Dec 2012 09:55:21 -0800 Subject: Warn when generated classfiles differ only in case. We should most likely prohibit it completely, but warning is a start. --- src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala | 9 ++++++++- test/files/neg/case-collision.check | 10 ++++++++++ test/files/neg/case-collision.flags | 1 + test/files/neg/case-collision.scala | 11 +++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/files/neg/case-collision.check create mode 100644 test/files/neg/case-collision.flags create mode 100644 test/files/neg/case-collision.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala index 6eb05c4402..61f3721da1 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala @@ -160,7 +160,14 @@ abstract class GenASM extends SubComponent with BytecodeWriters { } // For predictably ordered error messages. - var sortedClasses = classes.values.toList sortBy ("" + _.symbol.fullName) + var sortedClasses = classes.values.toList sortBy (_.symbol.fullName) + + // Warn when classes will overwrite one another on case-insensitive systems. + for ((_, v1 :: v2 :: _) <- sortedClasses groupBy (_.symbol.javaClassName.toString.toLowerCase)) { + v1.cunit.warning(v1.symbol.pos, + s"Class ${v1.symbol.javaClassName} differs only in case from ${v2.symbol.javaClassName}. " + + "Such classes will overwrite one another on case-insensitive filesystems.") + } debuglog("Created new bytecode generator for " + classes.size + " classes.") val bytecodeWriter = initBytecodeWriter(sortedClasses filter isJavaEntryPoint) diff --git a/test/files/neg/case-collision.check b/test/files/neg/case-collision.check new file mode 100644 index 0000000000..4edc6f1205 --- /dev/null +++ b/test/files/neg/case-collision.check @@ -0,0 +1,10 @@ +case-collision.scala:5: error: Class foo.BIPPY differs only in case from foo.Bippy. Such classes will overwrite one another on case-insensitive filesystems. +class BIPPY + ^ +case-collision.scala:11: error: Class foo.HyRaX$ differs only in case from foo.Hyrax$. Such classes will overwrite one another on case-insensitive filesystems. +object HyRaX + ^ +case-collision.scala:8: error: Class foo.DINGO$ differs only in case from foo.Dingo$. Such classes will overwrite one another on case-insensitive filesystems. +object DINGO + ^ +three errors found diff --git a/test/files/neg/case-collision.flags b/test/files/neg/case-collision.flags new file mode 100644 index 0000000000..85d8eb2ba2 --- /dev/null +++ b/test/files/neg/case-collision.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/neg/case-collision.scala b/test/files/neg/case-collision.scala new file mode 100644 index 0000000000..241169a77a --- /dev/null +++ b/test/files/neg/case-collision.scala @@ -0,0 +1,11 @@ +package foo + +class Bippy + +class BIPPY + +object Dingo +object DINGO + +case class Hyrax() +object HyRaX -- cgit v1.2.3 From 5546a72f35e1301d38e3fd56873db7a1fc5163bb Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 21 Nov 2012 13:07:52 +0100 Subject: SI-6696 removes "helper" tree factory methods As experience shows, these methods can easily be a source of confusion for the newcomers: https://issues.scala-lang.org/browse/SI-6696. I'm only leaving the TypeTree(tp) factory, since the facility to set underlying types for type trees is not exposed in the public API, as it's inherently mutable. --- src/reflect/scala/reflect/api/Trees.scala | 44 +++++++++++++++++++--- .../Impls_Macros_1.scala | 4 +- test/files/run/macro-abort-fresh/Test_2.scala | 2 +- .../macro-declared-in-class-class/Impls_1.scala | 2 +- .../macro-declared-in-class-object/Impls_1.scala | 2 +- .../run/macro-declared-in-class/Impls_1.scala | 2 +- .../Impls_Macros_1.scala | 2 +- .../run/macro-declared-in-method/Impls_1.scala | 2 +- .../macro-declared-in-object-class/Impls_1.scala | 2 +- .../macro-declared-in-object-object/Impls_1.scala | 2 +- .../run/macro-declared-in-object/Impls_1.scala | 2 +- .../macro-declared-in-package-object/Impls_1.scala | 2 +- .../run/macro-declared-in-refinement/Impls_1.scala | 2 +- .../run/macro-declared-in-trait/Impls_1.scala | 2 +- .../run/macro-def-infer-return-type-b/Test_2.scala | 2 +- .../macro-expand-implicit-argument/Macros_1.scala | 8 ++-- .../Macros_Test_2.scala | 2 +- .../macro-impl-default-params/Impls_Macros_1.scala | 4 +- .../macro-impl-rename-context/Impls_Macros_1.scala | 4 +- .../Test_2.scala | 2 +- .../Impls_Macros_1.scala | 2 +- .../run/macro-invalidret-nontypeable/Test_2.scala | 4 +- .../run/macro-invalidusage-badret/Test_2.scala | 2 +- .../Test_2.scala | 2 +- .../Test_2.scala | 2 +- .../macro-reflective-ma-normal-mdmi/Test_2.scala | 2 +- .../Macros_Test_2.scala | 6 +-- test/files/run/macro-reify-freevars/Test_2.scala | 4 +- .../macro-reify-splice-outside-reify/Test_2.scala | 2 +- test/files/run/macro-reify-tagless-a/Test_2.scala | 4 +- .../run/toolbox_typecheck_implicitsdisabled.scala | 8 ++-- .../pending/run/macro-reify-tagless-b/Test_2.scala | 2 +- 32 files changed, 84 insertions(+), 50 deletions(-) (limited to 'test') diff --git a/src/reflect/scala/reflect/api/Trees.scala b/src/reflect/scala/reflect/api/Trees.scala index c98107f9b5..c547a1e467 100644 --- a/src/reflect/scala/reflect/api/Trees.scala +++ b/src/reflect/scala/reflect/api/Trees.scala @@ -1611,14 +1611,23 @@ trait Trees { self: Universe => * This node always occurs in the following context: * * (`new` tpt).[targs](args) + * + * For example, an AST representation of: + * + * new Example[Int](2)(3) + * + * is the following code: + * + * Apply( + * Apply( + * TypeApply( + * Select(New(TypeTree(typeOf[Example])), nme.CONSTRUCTOR) + * TypeTree(typeOf[Int])), + * List(Literal(Constant(2)))), + * List(Literal(Constant(3)))) * @group Extractors */ abstract class NewExtractor { - /** A user level `new`. - * One should always use this factory method to build a user level `new`. - * - * @param tpt a class type - */ def apply(tpt: Tree): New def unapply(new_ : New): Option[Tree] } @@ -2371,118 +2380,141 @@ trait Trees { self: Universe => /** A factory method for `ClassDef` nodes. * @group Factories */ + @deprecated("Use the canonical ClassDef constructor to create a class and then initialize its position and symbol manually", "2.10.1") def ClassDef(sym: Symbol, impl: Template): ClassDef /** A factory method for `ModuleDef` nodes. * @group Factories */ + @deprecated("Use the canonical ModuleDef constructor to create an object and then initialize its position and symbol manually", "2.10.1") def ModuleDef(sym: Symbol, impl: Template): ModuleDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical ValDef constructor to create a val and then initialize its position and symbol manually", "2.10.1") def ValDef(sym: Symbol, rhs: Tree): ValDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical ValDef constructor to create a val with an empty right-hand side and then initialize its position and symbol manually", "2.10.1") def ValDef(sym: Symbol): ValDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical DefDef constructor to create a method and then initialize its position and symbol manually", "2.10.1") def DefDef(sym: Symbol, mods: Modifiers, vparamss: List[List[ValDef]], rhs: Tree): DefDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical DefDef constructor to create a method and then initialize its position and symbol manually", "2.10.1") def DefDef(sym: Symbol, vparamss: List[List[ValDef]], rhs: Tree): DefDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical DefDef constructor to create a method and then initialize its position and symbol manually", "2.10.1") def DefDef(sym: Symbol, mods: Modifiers, rhs: Tree): DefDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical DefDef constructor to create a method and then initialize its position and symbol manually", "2.10.1") def DefDef(sym: Symbol, rhs: Tree): DefDef /** A factory method for `ValDef` nodes. * @group Factories */ + @deprecated("Use the canonical DefDef constructor to create a method and then initialize its position and symbol manually", "2.10.1") def DefDef(sym: Symbol, rhs: List[List[Symbol]] => Tree): DefDef /** A factory method for `TypeDef` nodes. * @group Factories */ + @deprecated("Use the canonical TypeDef constructor to create a type alias and then initialize its position and symbol manually", "2.10.1") def TypeDef(sym: Symbol, rhs: Tree): TypeDef /** A factory method for `TypeDef` nodes. * @group Factories */ + @deprecated("Use the canonical TypeDef constructor to create an abstract type or type parameter and then initialize its position and symbol manually", "2.10.1") def TypeDef(sym: Symbol): TypeDef /** A factory method for `LabelDef` nodes. * @group Factories */ + @deprecated("Use the canonical LabelDef constructor to create a label and then initialize its position and symbol manually", "2.10.1") def LabelDef(sym: Symbol, params: List[Symbol], rhs: Tree): LabelDef /** A factory method for `Block` nodes. * Flattens directly nested blocks. * @group Factories */ + @deprecated("Use the canonical Block constructor, explicitly specifying its expression if necessary. Flatten directly nested blocks manually if needed", "2.10.1") def Block(stats: Tree*): Block /** A factory method for `CaseDef` nodes. * @group Factories */ + @deprecated("Use the canonical CaseDef constructor passing EmptyTree for guard", "2.10.1") def CaseDef(pat: Tree, body: Tree): CaseDef /** A factory method for `Bind` nodes. * @group Factories */ + @deprecated("Use the canonical Bind constructor to create a bind and then initialize its symbol manually", "2.10.1") def Bind(sym: Symbol, body: Tree): Bind /** A factory method for `Try` nodes. * @group Factories */ + @deprecated("Use canonical CaseDef constructors to to create exception catching expressions and then wrap them in Try", "2.10.1") def Try(body: Tree, cases: (Tree, Tree)*): Try /** A factory method for `Throw` nodes. * @group Factories */ + @deprecated("Use the canonical New constructor to create an object instantiation expression and then wrap it in Throw", "2.10.1") def Throw(tpe: Type, args: Tree*): Throw /** Factory method for object creation `new tpt(args_1)...(args_n)` * A `New(t, as)` is expanded to: `(new t).(as)` * @group Factories */ + @deprecated("Use Apply(...Apply(Select(New(tpt), nme.CONSTRUCTOR), args1)...argsN) instead", "2.10.1") def New(tpt: Tree, argss: List[List[Tree]]): Tree /** 0-1 argument list new, based on a type. * @group Factories */ + @deprecated("Use New(TypeTree(tpe), args.toList) instead", "2.10.1") def New(tpe: Type, args: Tree*): Tree /** 0-1 argument list new, based on a symbol. * @group Factories */ + @deprecated("Use New(sym.toType, args) instead", "2.10.1") def New(sym: Symbol, args: Tree*): Tree /** A factory method for `Apply` nodes. * @group Factories */ + @deprecated("Use Apply(Ident(sym), args.toList) instead", "2.10.1") def Apply(sym: Symbol, args: Tree*): Tree /** 0-1 argument list new, based on a type tree. * @group Factories */ + @deprecated("Use Apply(Select(New(tpt), nme.CONSTRUCTOR), args) instead", "2.10.1") def ApplyConstructor(tpt: Tree, args: List[Tree]): Tree /** A factory method for `Super` nodes. * @group Factories */ + @deprecated("Use Super(This(sym), mix) instead", "2.10.1") def Super(sym: Symbol, mix: TypeName): Tree /** A factory method for `This` nodes. @@ -2494,6 +2526,7 @@ trait Trees { self: Universe => * The string `name` argument is assumed to represent a [[scala.reflect.api.Names#TermName `TermName`]]. * @group Factories */ + @deprecated("Use Select(tree, newTermName(name)) instead", "2.10.1") def Select(qualifier: Tree, name: String): Select /** A factory method for `Select` nodes. @@ -2504,6 +2537,7 @@ trait Trees { self: Universe => /** A factory method for `Ident` nodes. * @group Factories */ + @deprecated("Use Ident(newTermName(name)) instead", "2.10.1") def Ident(name: String): Ident /** A factory method for `Ident` nodes. diff --git a/test/files/neg/macro-invalidsig-implicit-params/Impls_Macros_1.scala b/test/files/neg/macro-invalidsig-implicit-params/Impls_Macros_1.scala index 845a168ff2..7a7293422e 100644 --- a/test/files/neg/macro-invalidsig-implicit-params/Impls_Macros_1.scala +++ b/test/files/neg/macro-invalidsig-implicit-params/Impls_Macros_1.scala @@ -5,10 +5,10 @@ object Impls { def foo_targs[T, U: c.WeakTypeTag](c: Ctx)(implicit x: c.Expr[Int]) = { import c.{prefix => prefix} import c.universe._ - val body = Block( + val body = Block(List( Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("invoking foo_targs...")))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("type of prefix is: " + prefix.staticType)))), - Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("U is: " + implicitly[c.WeakTypeTag[U]].tpe)))), + Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("U is: " + implicitly[c.WeakTypeTag[U]].tpe))))), Literal(Constant(()))) c.Expr[Unit](body) } diff --git a/test/files/run/macro-abort-fresh/Test_2.scala b/test/files/run/macro-abort-fresh/Test_2.scala index 15c498efb0..0b9986e9f6 100644 --- a/test/files/run/macro-abort-fresh/Test_2.scala +++ b/test/files/run/macro-abort-fresh/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Select(Ident("Macros"), newTermName("foo")) + val tree = Select(Ident(newTermName("Macros")), newTermName("foo")) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-class-class/Impls_1.scala b/test/files/run/macro-declared-in-class-class/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-class-class/Impls_1.scala +++ b/test/files/run/macro-declared-in-class-class/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-class-object/Impls_1.scala b/test/files/run/macro-declared-in-class-object/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-class-object/Impls_1.scala +++ b/test/files/run/macro-declared-in-class-object/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-class/Impls_1.scala b/test/files/run/macro-declared-in-class/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-class/Impls_1.scala +++ b/test/files/run/macro-declared-in-class/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-implicit-class/Impls_Macros_1.scala b/test/files/run/macro-declared-in-implicit-class/Impls_Macros_1.scala index d506de4252..837b306976 100644 --- a/test/files/run/macro-declared-in-implicit-class/Impls_Macros_1.scala +++ b/test/files/run/macro-declared-in-implicit-class/Impls_Macros_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Ident(definitions.SomeModule), List(Select(Select(prefix.tree, newTermName("x")), newTermName("toInt"))))) + val body = Block(List(printPrefix), Apply(Ident(definitions.SomeModule), List(Select(Select(prefix.tree, newTermName("x")), newTermName("toInt"))))) c.Expr[Option[Int]](body) } } diff --git a/test/files/run/macro-declared-in-method/Impls_1.scala b/test/files/run/macro-declared-in-method/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-method/Impls_1.scala +++ b/test/files/run/macro-declared-in-method/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-object-class/Impls_1.scala b/test/files/run/macro-declared-in-object-class/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-object-class/Impls_1.scala +++ b/test/files/run/macro-declared-in-object-class/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-object-object/Impls_1.scala b/test/files/run/macro-declared-in-object-object/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-object-object/Impls_1.scala +++ b/test/files/run/macro-declared-in-object-object/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-object/Impls_1.scala b/test/files/run/macro-declared-in-object/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-object/Impls_1.scala +++ b/test/files/run/macro-declared-in-object/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-package-object/Impls_1.scala b/test/files/run/macro-declared-in-package-object/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-package-object/Impls_1.scala +++ b/test/files/run/macro-declared-in-package-object/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-refinement/Impls_1.scala b/test/files/run/macro-declared-in-refinement/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-refinement/Impls_1.scala +++ b/test/files/run/macro-declared-in-refinement/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-declared-in-trait/Impls_1.scala b/test/files/run/macro-declared-in-trait/Impls_1.scala index 1f8f3cbd5a..6f06f6d3f0 100644 --- a/test/files/run/macro-declared-in-trait/Impls_1.scala +++ b/test/files/run/macro-declared-in-trait/Impls_1.scala @@ -5,7 +5,7 @@ object Impls { import c.{prefix => prefix} import c.universe._ val printPrefix = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("prefix = " + prefix)))) - val body = Block(printPrefix, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) + val body = Block(List(printPrefix), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("it works"))))) c.Expr[Unit](body) } } \ No newline at end of file diff --git a/test/files/run/macro-def-infer-return-type-b/Test_2.scala b/test/files/run/macro-def-infer-return-type-b/Test_2.scala index ef2920a432..ea0fd4bbff 100644 --- a/test/files/run/macro-def-infer-return-type-b/Test_2.scala +++ b/test/files/run/macro-def-infer-return-type-b/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) + val tree = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant(42)))) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-expand-implicit-argument/Macros_1.scala b/test/files/run/macro-expand-implicit-argument/Macros_1.scala index b1665256cd..d9fd5b8cb0 100644 --- a/test/files/run/macro-expand-implicit-argument/Macros_1.scala +++ b/test/files/run/macro-expand-implicit-argument/Macros_1.scala @@ -43,16 +43,16 @@ object Macros { val n = as.length val arr = newTermName("arr") - val create = Apply(Select(ct.tree, "newArray"), List(const(n))) + val create = Apply(Select(ct.tree, newTermName("newArray")), List(const(n))) val arrtpe = TypeTree(implicitly[c.WeakTypeTag[Array[A]]].tpe) val valdef = ValDef(Modifiers(), arr, arrtpe, create) val updates = (0 until n).map { - i => Apply(Select(Ident(arr), "update"), List(const(i), as(i).tree)) + i => Apply(Select(Ident(arr), newTermName("update")), List(const(i), as(i).tree)) } - val exprs = Seq(valdef) ++ updates ++ Seq(Ident(arr)) - val block = Block(exprs:_*) + val exprs = (Seq(valdef) ++ updates ++ Seq(Ident(arr))).toList + val block = Block(exprs.init, exprs.last) c.Expr[Array[A]](block) } diff --git a/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala b/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala index 16d2c1e6ac..c832826d64 100644 --- a/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala +++ b/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala @@ -6,7 +6,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Typed(Apply(Ident(definitions.ListModule), List(Literal(Constant(1)), Literal(Constant(2)))), Ident(tpnme.WILDCARD_STAR)))) + val tree = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Typed(Apply(Ident(definitions.ListModule), List(Literal(Constant(1)), Literal(Constant(2)))), Ident(tpnme.WILDCARD_STAR)))) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-impl-default-params/Impls_Macros_1.scala b/test/files/run/macro-impl-default-params/Impls_Macros_1.scala index db77b1923a..7c40045c0f 100644 --- a/test/files/run/macro-impl-default-params/Impls_Macros_1.scala +++ b/test/files/run/macro-impl-default-params/Impls_Macros_1.scala @@ -6,11 +6,11 @@ object Impls { import c.{prefix => prefix} import c.universe._ val U = implicitly[c.WeakTypeTag[U]] - val body = Block( + val body = Block(List( Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("invoking foo_targs...")))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("type of prefix is: " + prefix.staticType)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("type of prefix tree is: " + prefix.tree.tpe)))), - Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("U is: " + U.tpe)))), + Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("U is: " + U.tpe))))), Literal(Constant(()))) c.Expr[Unit](body) } diff --git a/test/files/run/macro-impl-rename-context/Impls_Macros_1.scala b/test/files/run/macro-impl-rename-context/Impls_Macros_1.scala index 537f1db8c6..56c23f5faf 100644 --- a/test/files/run/macro-impl-rename-context/Impls_Macros_1.scala +++ b/test/files/run/macro-impl-rename-context/Impls_Macros_1.scala @@ -3,8 +3,8 @@ import scala.reflect.macros.{Context => Ctx} object Impls { def foo(unconventionalName: Ctx)(x: unconventionalName.Expr[Int]) = { import unconventionalName.universe._ - val body = Block( - Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("invoking foo...")))), + val body = Block(List( + Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant("invoking foo..."))))), Literal(Constant(()))) unconventionalName.Expr[Unit](body) } diff --git a/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala b/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala index 15c498efb0..0b9986e9f6 100644 --- a/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala +++ b/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Select(Ident("Macros"), newTermName("foo")) + val tree = Select(Ident(newTermName("Macros")), newTermName("foo")) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-invalidret-nontypeable/Impls_Macros_1.scala b/test/files/run/macro-invalidret-nontypeable/Impls_Macros_1.scala index 405ae7024f..fb0d55208c 100644 --- a/test/files/run/macro-invalidret-nontypeable/Impls_Macros_1.scala +++ b/test/files/run/macro-invalidret-nontypeable/Impls_Macros_1.scala @@ -3,7 +3,7 @@ import scala.reflect.macros.{Context => Ctx} object Impls { def foo(c: Ctx) = { import c.universe._ - val body = Ident("IDoNotExist") + val body = Ident(newTermName("IDoNotExist")) c.Expr[Int](body) } } diff --git a/test/files/run/macro-invalidret-nontypeable/Test_2.scala b/test/files/run/macro-invalidret-nontypeable/Test_2.scala index 15c498efb0..0daee49a08 100644 --- a/test/files/run/macro-invalidret-nontypeable/Test_2.scala +++ b/test/files/run/macro-invalidret-nontypeable/Test_2.scala @@ -1,8 +1,8 @@ -object Test extends App { + object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Select(Ident("Macros"), newTermName("foo")) + val tree = Select(Ident(newTermName("Macros")), newTermName("foo")) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-invalidusage-badret/Test_2.scala b/test/files/run/macro-invalidusage-badret/Test_2.scala index f3a76f3fff..5cb0be5ddd 100644 --- a/test/files/run/macro-invalidusage-badret/Test_2.scala +++ b/test/files/run/macro-invalidusage-badret/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Typed(Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))), Ident(newTypeName("String"))) + val tree = Typed(Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant(42)))), Ident(newTypeName("String"))) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-invalidusage-partialapplication-with-tparams/Test_2.scala b/test/files/run/macro-invalidusage-partialapplication-with-tparams/Test_2.scala index c91fd7d380..e453d0b70c 100644 --- a/test/files/run/macro-invalidusage-partialapplication-with-tparams/Test_2.scala +++ b/test/files/run/macro-invalidusage-partialapplication-with-tparams/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Select(Ident("Macros"), newTermName("foo")) + val tree = Select(Ident(newTermName("Macros")), newTermName("foo")) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-invalidusage-partialapplication/Test_2.scala b/test/files/run/macro-invalidusage-partialapplication/Test_2.scala index cbfee725e2..dc48c127f4 100644 --- a/test/files/run/macro-invalidusage-partialapplication/Test_2.scala +++ b/test/files/run/macro-invalidusage-partialapplication/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(40)))) + val tree = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant(40)))) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala b/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala index 373c6a6fca..2e64c01e35 100644 --- a/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala +++ b/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala @@ -2,6 +2,6 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) + val tree = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant(42)))) println(cm.mkToolBox().eval(tree)) } diff --git a/test/files/run/macro-reflective-mamd-normal-mi/Macros_Test_2.scala b/test/files/run/macro-reflective-mamd-normal-mi/Macros_Test_2.scala index adecfcff17..70560009b1 100644 --- a/test/files/run/macro-reflective-mamd-normal-mi/Macros_Test_2.scala +++ b/test/files/run/macro-reflective-mamd-normal-mi/Macros_Test_2.scala @@ -11,10 +11,10 @@ object Test extends App { val macrobody = Select(Ident(newTermName("Impls")), newTermName("foo")) val macroparam = ValDef(NoMods, newTermName("x"), TypeTree(definitions.IntClass.toType), EmptyTree) val macrodef = DefDef(Modifiers(MACRO), newTermName("foo"), Nil, List(List(macroparam)), TypeTree(), macrobody) - val modulector = DefDef(NoMods, nme.CONSTRUCTOR, Nil, List(List()), TypeTree(), Block(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List()))) + val modulector = DefDef(NoMods, nme.CONSTRUCTOR, Nil, List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))) val module = ModuleDef(NoMods, newTermName("Macros"), Template(Nil, emptyValDef, List(modulector, macrodef))) - val macroapp = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) - val tree = Block(macrodef, module, macroapp) + val macroapp = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant(42)))) + val tree = Block(List(macrodef, module), macroapp) val toolbox = cm.mkToolBox(options = "-language:experimental.macros") println(toolbox.eval(tree)) } diff --git a/test/files/run/macro-reify-freevars/Test_2.scala b/test/files/run/macro-reify-freevars/Test_2.scala index e24758cfb4..7af9d89bdb 100644 --- a/test/files/run/macro-reify-freevars/Test_2.scala +++ b/test/files/run/macro-reify-freevars/Test_2.scala @@ -2,8 +2,8 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val q = New(AppliedTypeTree(Select(Select(Select(Ident("scala"), newTermName("collection")), newTermName("slick")), newTypeName("Queryable")), List(Ident("Int")))) - val x = ValDef(NoMods, newTermName("x"), Ident("Int"), EmptyTree) + val q = New(AppliedTypeTree(Select(Select(Select(Ident(newTermName("scala")), newTermName("collection")), newTermName("slick")), newTypeName("Queryable")), List(Ident(newTermName("Int"))))) + val x = ValDef(NoMods, newTermName("x"), Ident(newTermName("Int")), EmptyTree) val fn = Function(List(x), Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant("5"))))) val tree = Apply(Select(q, newTermName("map")), List(fn)) try cm.mkToolBox().eval(tree) diff --git a/test/files/run/macro-reify-splice-outside-reify/Test_2.scala b/test/files/run/macro-reify-splice-outside-reify/Test_2.scala index 8f96ea199d..54bd03fcd2 100644 --- a/test/files/run/macro-reify-splice-outside-reify/Test_2.scala +++ b/test/files/run/macro-reify-splice-outside-reify/Test_2.scala @@ -2,7 +2,7 @@ object Test extends App { import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox - val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) + val tree = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant(42)))) try println(cm.mkToolBox().eval(tree)) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-reify-tagless-a/Test_2.scala b/test/files/run/macro-reify-tagless-a/Test_2.scala index 1bb3945ece..584c4bdf5b 100644 --- a/test/files/run/macro-reify-tagless-a/Test_2.scala +++ b/test/files/run/macro-reify-tagless-a/Test_2.scala @@ -6,9 +6,9 @@ object Test extends App { import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox val tpt = AppliedTypeTree(Ident(definitions.ListClass), List(Ident(definitions.StringClass))) - val rhs = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant("hello world")))) + val rhs = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant("hello world")))) val list = ValDef(NoMods, newTermName("list"), tpt, rhs) - val tree = Block(list, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Ident(list.name)))) + val tree = Block(List(list), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Ident(list.name)))) try cm.mkToolBox().eval(tree) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/toolbox_typecheck_implicitsdisabled.scala b/test/files/run/toolbox_typecheck_implicitsdisabled.scala index f11f0192f3..8a3d433142 100644 --- a/test/files/run/toolbox_typecheck_implicitsdisabled.scala +++ b/test/files/run/toolbox_typecheck_implicitsdisabled.scala @@ -6,16 +6,16 @@ import scala.tools.reflect.ToolBox object Test extends App { val toolbox = cm.mkToolBox() - val tree1 = Block( - Import(Select(Ident(newTermName("scala")), newTermName("Predef")), List(ImportSelector(nme.WILDCARD, -1, null, -1))), + val tree1 = Block(List( + Import(Select(Ident(newTermName("scala")), newTermName("Predef")), List(ImportSelector(nme.WILDCARD, -1, null, -1)))), Apply(Select(Literal(Constant(1)), newTermName("$minus$greater")), List(Literal(Constant(2)))) ) val ttree1 = toolbox.typeCheck(tree1, withImplicitViewsDisabled = false) println(ttree1) try { - val tree2 = Block( - Import(Select(Ident(newTermName("scala")), newTermName("Predef")), List(ImportSelector(nme.WILDCARD, -1, null, -1))), + val tree2 = Block(List( + Import(Select(Ident(newTermName("scala")), newTermName("Predef")), List(ImportSelector(nme.WILDCARD, -1, null, -1)))), Apply(Select(Literal(Constant(1)), newTermName("$minus$greater")), List(Literal(Constant(2)))) ) val ttree2 = toolbox.typeCheck(tree2, withImplicitViewsDisabled = true) diff --git a/test/pending/run/macro-reify-tagless-b/Test_2.scala b/test/pending/run/macro-reify-tagless-b/Test_2.scala index ebd35ffe47..10487b1515 100644 --- a/test/pending/run/macro-reify-tagless-b/Test_2.scala +++ b/test/pending/run/macro-reify-tagless-b/Test_2.scala @@ -6,7 +6,7 @@ object Test extends App { import scala.reflect.runtime.{currentMirror => cm} import scala.tools.reflect.ToolBox val tpt = AppliedTypeTree(Ident(definitions.ListClass), List(Ident(definitions.StringClass))) - val rhs = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant("hello world")))) + val rhs = Apply(Select(Ident(newTermName("Macros")), newTermName("foo")), List(Literal(Constant("hello world")))) val list = ValDef(NoMods, newTermName("list"), tpt, rhs) val tree = Block(list, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Ident(list.name)))) println(cm.mkToolBox().eval(tree)) -- cgit v1.2.3 From 40063b0009d55ed527bf1625d99a168a8faa4124 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Sat, 24 Nov 2012 22:32:17 +0100 Subject: refactors handling of parent types At the moment parser does too much w.r.t handling of parent types. It checks whether a parent can have value arguments or not and more importantly, it synthesizes constructors and super calls. This approach is fundamentally incompatible with upcoming type macros. Take for example the following two snippets of code: `class C extends A(2)` `class D extends A(2) with B(3)` In the first snippet, `A` might be a type macro, therefore the super call `A.super(2)` eagerly emitted by the parser might be meaningless. In the second snippet parser will report an error despite that `B` might be a type macro which expands into a trait. Unfortunately we cannot simply augment the parser with the `isTypeMacro` check. This is because to find out whether an identifier refers to a type macro, one needs to perform a typecheck, which the parser cannot do. Therefore we need a deep change in how parent types and constructors are processed by the compiler, which is implemented in this commit. --- src/compiler/scala/tools/nsc/ast/Trees.scala | 18 +- .../scala/tools/nsc/ast/parser/Parsers.scala | 62 ++-- .../scala/tools/nsc/ast/parser/TreeBuilder.scala | 20 +- .../scala/tools/nsc/transform/Constructors.scala | 1 - .../scala/tools/nsc/transform/UnCurry.scala | 4 +- .../tools/nsc/typechecker/ContextErrors.scala | 13 +- .../scala/tools/nsc/typechecker/Namers.scala | 9 +- .../tools/nsc/typechecker/StdAttachments.scala | 22 ++ .../scala/tools/nsc/typechecker/Typers.scala | 395 ++++++++++++++------- .../scala/tools/nsc/typechecker/Unapplies.scala | 2 +- .../scala/tools/reflect/ToolBoxFactory.scala | 1 - src/reflect/scala/reflect/internal/TreeInfo.scala | 7 + src/reflect/scala/reflect/internal/Trees.scala | 3 + test/files/neg/anyval-anyref-parent.check | 2 +- test/files/neg/cyclics-import.check | 11 +- test/files/neg/names-defaults-neg.check | 2 +- test/files/neg/protected-constructors.check | 5 +- test/files/neg/t2148.check | 2 +- test/files/neg/t409.check | 4 +- test/files/neg/t5529.check | 5 +- test/files/neg/t5696.check | 2 +- test/files/neg/t667.check | 4 +- test/files/neg/t877.check | 4 +- test/files/run/t5064.check | 6 +- 24 files changed, 388 insertions(+), 216 deletions(-) (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/ast/Trees.scala b/src/compiler/scala/tools/nsc/ast/Trees.scala index 296d55fec5..b8c59da95c 100644 --- a/src/compiler/scala/tools/nsc/ast/Trees.scala +++ b/src/compiler/scala/tools/nsc/ast/Trees.scala @@ -82,7 +82,7 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => * body * } */ - def Template(parents: List[Tree], self: ValDef, constrMods: Modifiers, vparamss: List[List[ValDef]], argss: List[List[Tree]], body: List[Tree], superPos: Position): Template = { + def Template(parents: List[Tree], self: ValDef, constrMods: Modifiers, vparamss: List[List[ValDef]], body: List[Tree], superPos: Position): Template = { /* Add constructor to template */ // create parameters for as synthetic trees. @@ -117,9 +117,16 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => if (vparamss1.isEmpty || !vparamss1.head.isEmpty && vparamss1.head.head.mods.isImplicit) vparamss1 = List() :: vparamss1; val superRef: Tree = atPos(superPos)(gen.mkSuperSelect) - val superCall = (superRef /: argss) (Apply.apply) + val superCall = Apply(superRef, Nil) // we can't know in advance which of the parents will end up as a superclass + // this requires knowing which of the parents is a type macro and which is not + // and that's something that cannot be found out before typer + // (the type macros aren't in the trunk yet, but there is a plan for them to land there soon) + // this means that we don't know what will be the arguments of the super call + // therefore here we emit a dummy which gets populated when the template is named and typechecked List( - atPos(wrappingPos(superPos, lvdefs ::: argss.flatten)) ( + // TODO: previously this was `wrappingPos(superPos, lvdefs ::: argss.flatten)` + // is it going to be a problem that we can no longer include the `argss`? + atPos(wrappingPos(superPos, lvdefs)) ( DefDef(constrMods, nme.CONSTRUCTOR, List(), vparamss1, TypeTree(), Block(lvdefs ::: List(superCall), Literal(Constant()))))) } } @@ -137,11 +144,10 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => * @param constrMods the modifiers for the class constructor, i.e. as in `class C private (...)` * @param vparamss the value parameters -- if they have symbols they * should be owned by `sym` - * @param argss the supercall arguments * @param body the template statements without primary constructor * and value parameter fields. */ - def ClassDef(sym: Symbol, constrMods: Modifiers, vparamss: List[List[ValDef]], argss: List[List[Tree]], body: List[Tree], superPos: Position): ClassDef = { + def ClassDef(sym: Symbol, constrMods: Modifiers, vparamss: List[List[ValDef]], body: List[Tree], superPos: Position): ClassDef = { // "if they have symbols they should be owned by `sym`" assert( mforall(vparamss)(p => (p.symbol eq NoSymbol) || (p.symbol.owner == sym)), @@ -151,7 +157,7 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => ClassDef(sym, Template(sym.info.parents map TypeTree, if (sym.thisSym == sym || phase.erasedTypes) emptyValDef else ValDef(sym.thisSym), - constrMods, vparamss, argss, body, superPos)) + constrMods, vparamss, body, superPos)) } // --- subcomponents -------------------------------------------------- diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 074fcabec8..a929e54601 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -1562,9 +1562,9 @@ self => val nstart = in.skipToken() val npos = r2p(nstart, nstart, in.lastOffset) val tstart = in.offset - val (parents, argss, self, stats) = template(isTrait = false) + val (parents, self, stats) = template() val cpos = r2p(tstart, tstart, in.lastOffset max tstart) - makeNew(parents, self, stats, argss, npos, cpos) + makeNew(parents, self, stats, npos, cpos) case _ => syntaxErrorOrIncomplete("illegal start of simple expression", true) errorTermTree @@ -2742,20 +2742,17 @@ self => * TraitParents ::= AnnotType {with AnnotType} * }}} */ - def templateParents(isTrait: Boolean): (List[Tree], List[List[Tree]]) = { - val parents = new ListBuffer[Tree] += startAnnotType() - val argss = ( - // TODO: the insertion of ListOfNil here is where "new Foo" becomes - // indistinguishable from "new Foo()". - if (in.token == LPAREN && !isTrait) multipleArgumentExprs() - else ListOfNil - ) - - while (in.token == WITH) { - in.nextToken() - parents += startAnnotType() + def templateParents(): List[Tree] = { + val parents = new ListBuffer[Tree] + def readAppliedParent() = { + val start = in.offset + val parent = startAnnotType() + val argss = if (in.token == LPAREN) multipleArgumentExprs() else Nil + parents += atPos(start)((parent /: argss)(Apply.apply)) } - (parents.toList, argss) + readAppliedParent() + while (in.token == WITH) { in.nextToken(); readAppliedParent() } + parents.toList } /** {{{ @@ -2765,7 +2762,7 @@ self => * EarlyDef ::= Annotations Modifiers PatDef * }}} */ - def template(isTrait: Boolean): (List[Tree], List[List[Tree]], ValDef, List[Tree]) = { + def template(): (List[Tree], ValDef, List[Tree]) = { newLineOptWhenFollowedBy(LBRACE) if (in.token == LBRACE) { // @S: pre template body cannot stub like post body can! @@ -2782,16 +2779,16 @@ self => case _ => List() } in.nextToken() - val (parents, argss) = templateParents(isTrait = isTrait) - val (self1, body1) = templateBodyOpt(traitParentSeen = isTrait) - (parents, argss, self1, earlyDefs ::: body1) + val parents = templateParents() + val (self1, body1) = templateBodyOpt(parenMeansSyntaxError = false) + (parents, self1, earlyDefs ::: body1) } else { - (List(), ListOfNil, self, body) + (List(), self, body) } } else { - val (parents, argss) = templateParents(isTrait = isTrait) - val (self, body) = templateBodyOpt(traitParentSeen = isTrait) - (parents, argss, self, body) + val parents = templateParents() + val (self, body) = templateBodyOpt(parenMeansSyntaxError = false) + (parents, self, body) } } @@ -2805,15 +2802,15 @@ self => * }}} */ def templateOpt(mods: Modifiers, name: Name, constrMods: Modifiers, vparamss: List[List[ValDef]], tstart: Int): Template = { - val (parents0, argss, self, body) = ( + val (parents0, self, body) = ( if (in.token == EXTENDS || in.token == SUBTYPE && mods.isTrait) { in.nextToken() - template(isTrait = mods.isTrait) + template() } else { newLineOptWhenFollowedBy(LBRACE) - val (self, body) = templateBodyOpt(traitParentSeen = false) - (List(), ListOfNil, self, body) + val (self, body) = templateBodyOpt(parenMeansSyntaxError = mods.isTrait || name.isTermName) + (List(), self, body) } ) def anyrefParents() = { @@ -2835,7 +2832,7 @@ self => if (inScalaRootPackage && ScalaValueClassNames.contains(name)) Template(parents0, self, anyvalConstructor :: body) else - Template(anyrefParents, self, constrMods, vparamss, argss, body, o2p(tstart)) + Template(anyrefParents, self, constrMods, vparamss, body, o2p(tstart)) } } @@ -2850,14 +2847,15 @@ self => case (self, Nil) => (self, EmptyTree.asList) case result => result } - def templateBodyOpt(traitParentSeen: Boolean): (ValDef, List[Tree]) = { + def templateBodyOpt(parenMeansSyntaxError: Boolean): (ValDef, List[Tree]) = { newLineOptWhenFollowedBy(LBRACE) if (in.token == LBRACE) { templateBody(isPre = false) } else { - if (in.token == LPAREN) - syntaxError((if (traitParentSeen) "parents of traits" else "traits or objects")+ - " may not have parameters", true) + if (in.token == LPAREN) { + if (parenMeansSyntaxError) syntaxError(s"traits or objects may not have parameters", true) + else assert(false, "unexpected opening parenthesis") + } (emptyValDef, List()) } } diff --git a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala index 0ac46a18bc..9e9e81aa27 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala @@ -205,20 +205,26 @@ abstract class TreeBuilder { */ def makeAnonymousNew(stats: List[Tree]): Tree = { val stats1 = if (stats.isEmpty) List(Literal(Constant(()))) else stats - makeNew(Nil, emptyValDef, stats1, ListOfNil, NoPosition, NoPosition) + makeNew(Nil, emptyValDef, stats1, NoPosition, NoPosition) } /** Create positioned tree representing an object creation + import global._ + + /** Carries information necessary to expand the host tree. + * At times we need to store this info, because macro expansion can be delayed until its targs are inferred. + * After a macro application has been successfully expanded, this attachment is destroyed. + */ type UnaffiliatedMacroContext = scala.reflect.macros.runtime.Context type MacroContext = UnaffiliatedMacroContext { val universe: self.global.type } case class MacroRuntimeAttachment(delayed: Boolean, typerContext: Context, macroContext: Option[MacroContext]) + + /** After being synthesized by the parser, primary constructors aren't fully baked yet. + * A call to super in such constructors is just a fill-me-in-later dummy resolved later + * by `parentTypes`. This attachment coordinates `parentTypes` and `typedTemplate` and + * allows them to complete the synthesis. + */ + case class SuperCallArgsAttachment(argss: List[List[Tree]]) + + /** Extractor for `SuperCallArgsAttachment`. + * Compared with `MacroRuntimeAttachment` this attachment has different a usage pattern, + * so it really benefits from a dedicated extractor. + */ + object CarriesSuperCallArgs { + def unapply(tree: Tree): Option[List[List[Tree]]] = + tree.attachments.get[SuperCallArgsAttachment] collect { case SuperCallArgsAttachment(argss) => argss } + } } \ No newline at end of file diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 7c76eff9d8..3f5a4036aa 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1378,13 +1378,6 @@ trait Typers extends Modes with Adaptations with Tags { if (member(qual, name) != NoSymbol) qual else adaptToMember(qual, HasMember(name)) - private def typePrimaryConstrBody(clazz : Symbol, cbody: Tree, tparams: List[Symbol], enclTparams: List[Symbol], vparamss: List[List[ValDef]]): Tree = { - // XXX: see about using the class's symbol.... - enclTparams foreach (sym => context.scope.enter(sym)) - namer.enterValueParams(vparamss) - typed(cbody) - } - private def validateNoCaseAncestor(clazz: Symbol) = { if (!phase.erasedTypes) { for (ancestor <- clazz.ancestors find (_.isCase)) { @@ -1486,125 +1479,263 @@ trait Typers extends Modes with Adaptations with Tags { unit.error(tparam.pos, "type parameter of value class may not be specialized") } - def parentTypes(templ: Template): List[Tree] = - if (templ.parents.isEmpty) List(atPos(templ.pos)(TypeTree(AnyRefClass.tpe))) - else try { - val clazz = context.owner - // Normalize supertype and mixins so that supertype is always a class, not a trait. - var supertpt = typedTypeConstructor(templ.parents.head) - val firstParent = supertpt.tpe.typeSymbol - var mixins = templ.parents.tail map typedType - // If first parent is a trait, make it first mixin and add its superclass as first parent - while ((supertpt.tpe.typeSymbol ne null) && supertpt.tpe.typeSymbol.initialize.isTrait) { - val supertpt1 = typedType(supertpt) - if (!supertpt1.isErrorTyped) { - mixins = supertpt1 :: mixins - supertpt = TypeTree(supertpt1.tpe.firstParent) setPos supertpt.pos.focus - } + /** Typechecks a parent type reference. + * + * This typecheck is harder than it might look, because it should honor early + * definitions and also perform type argument inference with the help of super call + * arguments provided in `encodedtpt`. + * + * The method is called in batches (batch = 1 time per each parent type referenced), + * two batches per definition: once from namer, when entering a ClassDef or a ModuleDef + * and once from typer, when typechecking the definition. + * + * ***Arguments*** + * + * `encodedtpt` represents the parent type reference wrapped in an `Apply` node + * which indicates value arguments (i.e. type macro arguments or super constructor call arguments) + * If no value arguments are provided by the user, the `Apply` node is still + * there, but its `args` will be set to `Nil`. + * This argument is synthesized by `tools.nsc.ast.Parsers.templateParents`. + * + * `templ` is an enclosing template, which contains a primary constructor synthesized by the parser. + * Such a constructor is a DefDef which contains early initializers and maybe a super constructor call + * (I wrote "maybe" because trait constructors don't call super constructors). + * This argument is synthesized by `tools.nsc.ast.Trees.Template`. + * + * `inMixinPosition` indicates whether the reference is not the first in the + * list of parents (and therefore cannot be a class) or the opposite. + * + * ***Return value and side effects*** + * + * Returns a `TypeTree` representing a resolved parent type. + * If the typechecked parent reference implies non-nullary and non-empty argument list, + * this argument list is attached to the returned value in SuperCallArgsAttachment. + * The attachment is necessary for the subsequent typecheck to fixup a super constructor call + * in the body of the primary constructor (see `typedTemplate` for details). + * + * This method might invoke `typedPrimaryConstrBody`, hence it might cause the side effects + * described in the docs of that method. It might also attribute the Super(_, _) reference + * (if present) inside the primary constructor of `templ`. + * + * ***Example*** + * + * For the following definition: + * + * class D extends { + * val x = 2 + * val y = 4 + * } with B(x)(3) with C(y) with T + * + * this method will be called six times: + * + * (3 times from the namer) + * typedParentType(Apply(Apply(Ident(B), List(Ident(x))), List(3)), templ, inMixinPosition = false) + * typedParentType(Apply(Ident(C), List(Ident(y))), templ, inMixinPosition = true) + * typedParentType(Apply(Ident(T), List()), templ, inMixinPosition = true) + * + * (3 times from the typer) + * + */ + private def typedParentType(encodedtpt: Tree, templ: Template, inMixinPosition: Boolean): Tree = { + val app = treeInfo.dissectApplied(encodedtpt) + val (treeInfo.Applied(core, targs, argss), decodedtpt) = (app, app.callee) + val argssAreTrivial = argss == Nil || argss == ListOfNil + + // we cannot avoid cyclic references with `initialize` here, because when type macros arrive, + // we'll have to check the probe for isTypeMacro anyways. + // therefore I think it's reasonable to trade a more specific "inherits itself" error + // for a generic, yet understandable "cyclic reference" error + var probe = typedTypeConstructor(core.duplicate).tpe.typeSymbol + if (probe == null) probe = NoSymbol + probe.initialize + + if (probe.isTrait || inMixinPosition) { + if (!argssAreTrivial) { + if (probe.isTrait) ConstrArgsInParentWhichIsTraitError(encodedtpt, probe) + else () // a class in a mixin position - this warrants an error in `validateParentClasses` + // therefore here we do nothing, e.g. don't check that the # of ctor arguments + // matches the # of ctor parameters or stuff like that } - if (supertpt.tpe.typeSymbol == AnyClass && firstParent.isTrait) - supertpt.tpe = AnyRefClass.tpe - - // Determine - // - supertparams: Missing type parameters from supertype - // - supertpe: Given supertype, polymorphic in supertparams - val supertparams = if (supertpt.hasSymbol) supertpt.symbol.typeParams else List() - var supertpe = supertpt.tpe - if (!supertparams.isEmpty) - supertpe = PolyType(supertparams, appliedType(supertpe, supertparams map (_.tpeHK))) - - // A method to replace a super reference by a New in a supercall - def transformSuperCall(scall: Tree): Tree = (scall: @unchecked) match { - case Apply(fn, args) => - treeCopy.Apply(scall, transformSuperCall(fn), args map (_.duplicate)) - case Select(Super(_, _), nme.CONSTRUCTOR) => - treeCopy.Select( - scall, - atPos(supertpt.pos.focus)(New(TypeTree(supertpe)) setType supertpe), - nme.CONSTRUCTOR) + typedType(decodedtpt) + } else { + var supertpt = typedTypeConstructor(decodedtpt) + val supertparams = if (supertpt.hasSymbol) supertpt.symbol.typeParams else Nil + if (supertparams.nonEmpty) { + typedPrimaryConstrBody(templ) { superRef => + val supertpe = PolyType(supertparams, appliedType(supertpt.tpe, supertparams map (_.tpeHK))) + val supercall = New(supertpe, mmap(argss)(_.duplicate)) + val treeInfo.Applied(Select(ctor, nme.CONSTRUCTOR), _, _) = supercall + ctor setType supertpe // this is an essential hack, otherwise it will occasionally fail to typecheck + atPos(supertpt.pos.focus)(supercall) + } match { + case EmptyTree => MissingTypeArgumentsParentTpeError(supertpt) + case tpt => supertpt = TypeTree(tpt.tpe) setPos supertpt.pos.focus + } } + // this is the place where we tell the typer what argss should be used for the super call + // if argss are nullary or empty, then (see the docs for `typedPrimaryConstrBody`) + // the super call dummy is already good enough, so we don't need to do anything + if (argssAreTrivial) supertpt else supertpt updateAttachment SuperCallArgsAttachment(argss) + } + } - treeInfo.firstConstructor(templ.body) match { - case constr @ DefDef(_, _, _, vparamss, _, cbody @ Block(cstats, cunit)) => - // Convert constructor body to block in environment and typecheck it - val (preSuperStats, superCall) = { - val (stats, rest) = cstats span (x => !treeInfo.isSuperConstrCall(x)) - (stats map (_.duplicate), if (rest.isEmpty) EmptyTree else rest.head.duplicate) - } - val cstats1 = if (superCall == EmptyTree) preSuperStats else preSuperStats :+ superCall - val cbody1 = treeCopy.Block(cbody, preSuperStats, superCall match { - case Apply(_, _) if supertparams.nonEmpty => transformSuperCall(superCall) - case _ => cunit.duplicate - }) - val outercontext = context.outer - - assert(clazz != NoSymbol, templ) - val cscope = outercontext.makeNewScope(constr, outercontext.owner) - val cbody2 = newTyper(cscope) // called both during completion AND typing. - .typePrimaryConstrBody(clazz, - cbody1, supertparams, clazz.unsafeTypeParams, vparamss map (_.map(_.duplicate))) - - superCall match { - case Apply(_, _) => - val sarg = treeInfo.firstArgument(superCall) - if (sarg != EmptyTree && supertpe.typeSymbol != firstParent) - ConstrArgsInTraitParentTpeError(sarg, firstParent) - if (!supertparams.isEmpty) - supertpt = TypeTree(cbody2.tpe) setPos supertpt.pos.focus - case _ => - if (!supertparams.isEmpty) - MissingTypeArgumentsParentTpeError(supertpt) - } + /** Typechecks the mishmash of trees that happen to be stuffed into the primary constructor of a given template. + * Before commencing the typecheck applies `superCallTransform` to a super call (if the latter exists). + * The transform can return `EmptyTree`, in which case the super call is replaced with a literal unit. + * + * ***Return value and side effects*** + * + * If a super call is present in the primary constructor and is not erased by the transform, returns it typechecked. + * Otherwise (e.g. if the primary constructor is missing or the super call isn't there) returns `EmptyTree`. + * + * As a side effect, this method attributes the underlying fields of early vals. + * Early vals aren't typechecked anywhere else, so it's essential to call `typedPrimaryConstrBody` + * at least once per definition. It'd be great to disentangle this logic at some point. + * + * ***Example*** + * + * For the following definition: + * + * class D extends { + * val x = 2 + * val y = 4 + * } with B(x)(3) with C(y) with T + * + * the primary constructor of `templ` will be: + * + * Block(List( + * ValDef(NoMods, x, TypeTree(), 2) + * ValDef(NoMods, y, TypeTree(), 4) + * Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR)), List()), + * Literal(Constant(()))) + * + * Note the Select(Super(_, _), nme.CONSTRUCTOR) part. This is the representation of + * a fill-me-in-later supercall dummy. The argss are Nil, which encodes the fact + * that supercall argss are unknown during parsing and need to be transplanted from one of the parent types. + * Read more about why the argss are unknown in `tools.nsc.ast.Trees.Template`. + * + * The entire Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR)), List()) is a dummy, + * and it's the one and only possible representation that can be emitted by parser. + * + * Despite of being unwieldy, this tree is quite convenient because: + * * It works as is for the case when no processing is required (empty ctor args for the superclass) + * * Stripping off the Apply produces a core that only needs rewrapping with applications of actual argss. + * + * For some time I was thinking of using just Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR)), + * but that one required wrapping even if the superclass doesn't take any argss. + * + * Another option would be to introduce a singleton tree akin to `emptyValDef` and use it as a dummy. + * Unfortunately this won't work out of the box, because the Super part is supposed to get attributed + * during `typedPrimaryConstrBody`. + * + * We could introduce another attachment for that or change SuperCallArgsAttachment + * to accommodate for the attributed Super, and then using the attached info to adjust the primary constructor + * during typedTemplate. However, given the scope of necessary changes (beyond a few lines) and the fact that, + * according to Martin, the whole thing is to be rewritten soon, I'd say we don't do the follow-up refactoring. + */ + private def typedPrimaryConstrBody(templ: Template)(superCallTransform: Tree => Tree): Tree = + treeInfo.firstConstructor(templ.body) match { + case ctor @ DefDef(_, _, _, vparamss, _, cbody @ Block(cstats, cunit)) => + val (preSuperStats, superCall) = { + val (stats, rest) = cstats span (x => !treeInfo.isSuperConstrCall(x)) + (stats map (_.duplicate), if (rest.isEmpty) EmptyTree else rest.head.duplicate) + } + val superCall1 = (superCall match { + case Apply(superRef @ Select(Super(_, _), nme.CONSTRUCTOR), Nil) => superCallTransform(superRef) + case EmptyTree => EmptyTree + }) orElse cunit + val cbody1 = treeCopy.Block(cbody, preSuperStats, superCall1) + + val clazz = context.owner + assert(clazz != NoSymbol, templ) + val cscope = context.outer.makeNewScope(ctor, context.outer.owner) + val cbody2 = { // called both during completion AND typing. + val typer1 = newTyper(cscope) + // XXX: see about using the class's symbol.... + clazz.unsafeTypeParams foreach (sym => typer1.context.scope.enter(sym)) + typer1.namer.enterValueParams(vparamss map (_.map(_.duplicate))) + typer1.typed(cbody1) + } - val preSuperVals = treeInfo.preSuperFields(templ.body) - if (preSuperVals.isEmpty && preSuperStats.nonEmpty) - debugwarn("Wanted to zip empty presuper val list with " + preSuperStats) - else - map2(preSuperStats, preSuperVals)((ldef, gdef) => gdef.tpt.tpe = ldef.symbol.tpe) + val preSuperVals = treeInfo.preSuperFields(templ.body) + if (preSuperVals.isEmpty && preSuperStats.nonEmpty) + debugwarn("Wanted to zip empty presuper val list with " + preSuperStats) + else + map2(preSuperStats, preSuperVals)((ldef, gdef) => gdef.tpt.tpe = ldef.symbol.tpe) - case _ => - if (!supertparams.isEmpty) - MissingTypeArgumentsParentTpeError(supertpt) - } -/* experimental: early types as type arguments - val hasEarlyTypes = templ.body exists (treeInfo.isEarlyTypeDef) - val earlyMap = new EarlyMap(clazz) - List.mapConserve(supertpt :: mixins){ tpt => - val tpt1 = checkNoEscaping.privates(clazz, tpt) - if (hasEarlyTypes) tpt1 else tpt1 setType earlyMap(tpt1.tpe) + if (superCall1 == cunit) EmptyTree else cbody2 + case _ => + EmptyTree + } + + /** Makes sure that the first type tree in the list of parent types is always a class. + * If the first parent is a trait, prepend its supertype to the list until it's a class. + */ + private def normalizeFirstParent(parents: List[Tree]): List[Tree] = parents match { + case first :: rest if treeInfo.isTraitRef(first) => + def explode(supertpt: Tree, acc: List[Tree]): List[Tree] = { + if (treeInfo.isTraitRef(supertpt)) { + val supertpt1 = typedType(supertpt) + if (!supertpt1.isErrorTyped) { + val supersupertpt = TypeTree(supertpt1.tpe.firstParent) setPos supertpt.pos.focus + return explode(supersupertpt, supertpt1 :: acc) + } + } + if (supertpt.tpe.typeSymbol == AnyClass) supertpt.tpe = AnyRefClass.tpe + supertpt :: acc } -*/ + explode(first, Nil) ++ rest + case _ => parents + } - //Console.println("parents("+clazz") = "+supertpt :: mixins);//DEBUG + /** Certain parents are added in the parser before it is known whether + * that class also declared them as parents. For instance, this is an + * error unless we take corrective action here: + * + * case class Foo() extends Serializable + * + * So we strip the duplicates before typer. + */ + private def fixDuplicateSyntheticParents(parents: List[Tree]): List[Tree] = parents match { + case Nil => Nil + case x :: xs => + val sym = x.symbol + x :: fixDuplicateSyntheticParents( + if (isPossibleSyntheticParent(sym)) xs filterNot (_.symbol == sym) + else xs + ) + } - // Certain parents are added in the parser before it is known whether - // that class also declared them as parents. For instance, this is an - // error unless we take corrective action here: - // - // case class Foo() extends Serializable - // - // So we strip the duplicates before typer. - def fixDuplicates(remaining: List[Tree]): List[Tree] = remaining match { - case Nil => Nil - case x :: xs => - val sym = x.symbol - x :: fixDuplicates( - if (isPossibleSyntheticParent(sym)) xs filterNot (_.symbol == sym) - else xs - ) + def parentTypes(templ: Template): List[Tree] = templ.parents match { + case Nil => List(atPos(templ.pos)(TypeTree(AnyRefClass.tpe))) + case first :: rest => + try { + val supertpts = fixDuplicateSyntheticParents(normalizeFirstParent( + typedParentType(first, templ, inMixinPosition = false) +: + (rest map (typedParentType(_, templ, inMixinPosition = true))))) + + // if that is required to infer the targs of a super call + // typedParentType calls typedPrimaryConstrBody to do the inferring typecheck + // as a side effect, that typecheck also assigns types to the fields underlying early vals + // however if inference is not required, the typecheck doesn't happen + // and therefore early fields have their type trees not assigned + // here we detect this situation and take preventive measures + if (treeInfo.hasUntypedPreSuperFields(templ.body)) + typedPrimaryConstrBody(templ)(superRef => EmptyTree) + + supertpts mapConserve (tpt => checkNoEscaping.privates(context.owner, tpt)) + } catch { + case ex: TypeError => + // fallback in case of cyclic errors + // @H none of the tests enter here but I couldn't rule it out + // upd. @E when a definitions inherits itself, we end up here + // because `typedParentType` triggers `initialize` for parent types symbols + log("Type error calculating parents in template " + templ) + log("Error: " + ex) + ParentTypesError(templ, ex) + List(TypeTree(AnyRefClass.tpe)) } - - fixDuplicates(supertpt :: mixins) mapConserve (tpt => checkNoEscaping.privates(clazz, tpt)) - } - catch { - case ex: TypeError => - // fallback in case of cyclic errors - // @H none of the tests enter here but I couldn't rule it out - log("Type error calculating parents in template " + templ) - log("Error: " + ex) - ParentTypesError(templ, ex) - List(TypeTree(AnyRefClass.tpe)) - } + } /**

Check that

*
    @@ -1844,7 +1975,8 @@ trait Typers extends Modes with Adaptations with Tags { // the following is necessary for templates generated later assert(clazz.info.decls != EmptyScope, clazz) enterSyms(context.outer.make(templ, clazz, clazz.info.decls), templ.body) - validateParentClasses(parents1, selfType) + if (!templ.isErrorTyped) // if `parentTypes` has invalidated the template, don't validate it anymore + validateParentClasses(parents1, selfType) if (clazz.isCase) validateNoCaseAncestor(clazz) @@ -1854,9 +1986,28 @@ trait Typers extends Modes with Adaptations with Tags { if (!phase.erasedTypes && !clazz.info.resultType.isError) // @S: prevent crash for duplicated type members checkFinitary(clazz.info.resultType.asInstanceOf[ClassInfoType]) - val body = - if (isPastTyper || reporter.hasErrors) templ.body - else templ.body flatMap rewrappingWrapperTrees(namer.addDerivedTrees(Typer.this, _)) + val body = { + val body = + if (isPastTyper || reporter.hasErrors) templ.body + else templ.body flatMap rewrappingWrapperTrees(namer.addDerivedTrees(Typer.this, _)) + parents1.head match { + case CarriesSuperCallArgs(argss) => + if (clazz.isTrait) { + ConstrArgsInParentOfTraitError(parents1.head, clazz) + body + } else { + val primaryCtor = treeInfo.firstConstructor(templ.body) + val primaryCtor1 = (deriveDefDef(primaryCtor) { + case block @ Block(earlyVals :+ Apply(superRef, Nil), unit) => + val pos = wrappingPos(parents1.head.pos, argss.flatten) + val superCall = atPos(pos)((superRef /: argss)(Apply.apply)) + Block(earlyVals :+ superCall, unit) setPos pos + }) setPos pos + body map { case `primaryCtor` => primaryCtor1; case stat => stat } + } + case _ => body + } + } val body1 = typedStats(body, templ.symbol) @@ -2603,7 +2754,7 @@ trait Typers extends Modes with Adaptations with Tags { if (members.head eq EmptyTree) setError(tree) else { val typedBlock = typedPos(tree.pos, mode, pt) { - Block(ClassDef(anonClass, NoMods, ListOfNil, ListOfNil, members, tree.pos.focus), atPos(tree.pos.focus)(New(anonClass.tpe))) + Block(ClassDef(anonClass, NoMods, ListOfNil, members, tree.pos.focus), atPos(tree.pos.focus)(New(anonClass.tpe))) } // Don't leak implementation details into the type, see SI-6575 if (isPartial && !typedBlock.isErrorTyped) diff --git a/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala b/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala index bf44b65406..a34d7389bf 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala @@ -126,7 +126,7 @@ trait Unapplies extends ast.TreeDSL ModuleDef( Modifiers(cdef.mods.flags & AccessFlags | SYNTHETIC, cdef.mods.privateWithin), cdef.name.toTermName, - Template(parents, emptyValDef, NoMods, Nil, ListOfNil, body, cdef.impl.pos.focus)) + Template(parents, emptyValDef, NoMods, Nil, body, cdef.impl.pos.focus)) } private val caseMods = Modifiers(SYNTHETIC | CASE) diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala index 95135b84e0..0125f1b189 100644 --- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala +++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala @@ -230,7 +230,6 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => emptyValDef, NoMods, List(), - List(List()), List(methdef), NoPosition)) trace("wrapped: ")(showAttributed(moduledef, true, true, settings.Yshowsymkinds.value)) diff --git a/src/reflect/scala/reflect/internal/TreeInfo.scala b/src/reflect/scala/reflect/internal/TreeInfo.scala index 7423a2ff57..8908036442 100644 --- a/src/reflect/scala/reflect/internal/TreeInfo.scala +++ b/src/reflect/scala/reflect/internal/TreeInfo.scala @@ -330,6 +330,9 @@ abstract class TreeInfo { def preSuperFields(stats: List[Tree]): List[ValDef] = stats collect { case vd: ValDef if isEarlyValDef(vd) => vd } + def hasUntypedPreSuperFields(stats: List[Tree]): Boolean = + preSuperFields(stats) exists (_.tpt.isEmpty) + def isEarlyDef(tree: Tree) = tree match { case TypeDef(mods, _, _, _) => mods hasFlag PRESUPER case ValDef(mods, _, _, _) => mods hasFlag PRESUPER @@ -494,6 +497,10 @@ abstract class TreeInfo { def isSynthCaseSymbol(sym: Symbol) = sym hasAllFlags SYNTH_CASE_FLAGS def hasSynthCaseSymbol(t: Tree) = t.symbol != null && isSynthCaseSymbol(t.symbol) + def isTraitRef(tree: Tree): Boolean = { + val sym = if (tree.tpe != null) tree.tpe.typeSymbol else null + ((sym ne null) && sym.initialize.isTrait) + } /** Applications in Scala can have one of the following shapes: * diff --git a/src/reflect/scala/reflect/internal/Trees.scala b/src/reflect/scala/reflect/internal/Trees.scala index 6df4b75a88..0087bb93e7 100644 --- a/src/reflect/scala/reflect/internal/Trees.scala +++ b/src/reflect/scala/reflect/internal/Trees.scala @@ -1034,6 +1034,9 @@ trait Trees extends api.Trees { self: SymbolTable => def New(tpe: Type, args: Tree*): Tree = ApplyConstructor(TypeTree(tpe), args.toList) + def New(tpe: Type, argss: List[List[Tree]]): Tree = + New(TypeTree(tpe), argss) + def New(sym: Symbol, args: Tree*): Tree = New(sym.tpe, args: _*) diff --git a/test/files/neg/anyval-anyref-parent.check b/test/files/neg/anyval-anyref-parent.check index fe20e5de81..8c2aa36583 100644 --- a/test/files/neg/anyval-anyref-parent.check +++ b/test/files/neg/anyval-anyref-parent.check @@ -3,7 +3,7 @@ trait Foo2 extends AnyVal // fail ^ anyval-anyref-parent.scala:5: error: Any does not have a constructor class Bar1 extends Any // fail - ^ + ^ anyval-anyref-parent.scala:6: error: value class needs to have exactly one public val parameter class Bar2(x: Int) extends AnyVal // fail ^ diff --git a/test/files/neg/cyclics-import.check b/test/files/neg/cyclics-import.check index ef355fab0a..be09fca374 100644 --- a/test/files/neg/cyclics-import.check +++ b/test/files/neg/cyclics-import.check @@ -3,13 +3,4 @@ Note: this is often due in part to a class depending on a definition nested with If applicable, you may wish to try moving some members into another object. import User.UserStatus._ ^ -cyclics-import.scala:12: error: not found: type Value - type UserStatus = Value - ^ -cyclics-import.scala:14: error: not found: value Value - val Active = Value("1") - ^ -cyclics-import.scala:15: error: not found: value Value - val Disabled = Value("2") - ^ -four errors found +one error found diff --git a/test/files/neg/names-defaults-neg.check b/test/files/neg/names-defaults-neg.check index f3c45a6aa0..6f9dc7d127 100644 --- a/test/files/neg/names-defaults-neg.check +++ b/test/files/neg/names-defaults-neg.check @@ -100,7 +100,7 @@ Error occurred in an application involving default arguments. ^ names-defaults-neg.scala:86: error: module extending its companion class cannot use default constructor arguments object C extends C() - ^ + ^ names-defaults-neg.scala:90: error: deprecated parameter name x has to be distinct from any other parameter name (deprecated or not). def deprNam1(x: Int, @deprecatedName('x) y: String) = 0 ^ diff --git a/test/files/neg/protected-constructors.check b/test/files/neg/protected-constructors.check index f137158ed6..e295917050 100644 --- a/test/files/neg/protected-constructors.check +++ b/test/files/neg/protected-constructors.check @@ -19,7 +19,4 @@ protected-constructors.scala:15: error: class Foo3 in object Ding cannot be acce object Ding in package dingus where target is defined class Bar3 extends Ding.Foo3("abc") ^ -protected-constructors.scala:15: error: too many arguments for constructor Object: ()Object - class Bar3 extends Ding.Foo3("abc") - ^ -5 errors found +four errors found diff --git a/test/files/neg/t2148.check b/test/files/neg/t2148.check index 5113b48e51..27b5dce507 100644 --- a/test/files/neg/t2148.check +++ b/test/files/neg/t2148.check @@ -1,4 +1,4 @@ -t2148.scala:9: error: type A is not a stable prefix +t2148.scala:9: error: A is not a legal prefix for a constructor val b = new A with A#A1 ^ one error found diff --git a/test/files/neg/t409.check b/test/files/neg/t409.check index 433d64d25d..0edc0d03cd 100644 --- a/test/files/neg/t409.check +++ b/test/files/neg/t409.check @@ -1,4 +1,4 @@ -t409.scala:6: error: traits or objects may not have parameters +t409.scala:6: error: class Case1 needs to be a trait to be mixed in class Toto extends Expr with Case1(12); - ^ + ^ one error found diff --git a/test/files/neg/t5529.check b/test/files/neg/t5529.check index 5d2175fa79..da3f84e1ec 100644 --- a/test/files/neg/t5529.check +++ b/test/files/neg/t5529.check @@ -4,7 +4,4 @@ t5529.scala:12: error: File is already defined as class File t5529.scala:10: error: class type required but test.Test.File found sealed class Dir extends File { } ^ -t5529.scala:10: error: test.Test.File does not have a constructor - sealed class Dir extends File { } - ^ -three errors found +two errors found diff --git a/test/files/neg/t5696.check b/test/files/neg/t5696.check index 72b7781fc4..e0fb61b839 100644 --- a/test/files/neg/t5696.check +++ b/test/files/neg/t5696.check @@ -15,5 +15,5 @@ t5696.scala:38: error: too many argument lists for constructor invocation ^ t5696.scala:46: error: too many argument lists for constructor invocation object x extends G(1)(2) {} - ^ + ^ 6 errors found diff --git a/test/files/neg/t667.check b/test/files/neg/t667.check index d4367bc87b..e68c6dea00 100644 --- a/test/files/neg/t667.check +++ b/test/files/neg/t667.check @@ -1,4 +1,4 @@ -t667.scala:8: error: class Ni inherits itself +t667.scala:8: error: illegal cyclic reference involving class Ni class Ni extends super.Ni with Ni; - ^ + ^ one error found diff --git a/test/files/neg/t877.check b/test/files/neg/t877.check index 5f25bd439c..c3d4ab6584 100644 --- a/test/files/neg/t877.check +++ b/test/files/neg/t877.check @@ -1,7 +1,7 @@ t877.scala:3: error: Invalid literal number trait Foo extends A(22A, Bug!) {} ^ -t877.scala:3: error: parents of traits may not have parameters +t877.scala:3: error: ')' expected but eof found. trait Foo extends A(22A, Bug!) {} - ^ + ^ two errors found diff --git a/test/files/run/t5064.check b/test/files/run/t5064.check index 077006abd9..61ccfd16e7 100644 --- a/test/files/run/t5064.check +++ b/test/files/run/t5064.check @@ -1,6 +1,6 @@ -[12] T5064.super.() -[12] T5064.super. -[12] this +[53] T5064.super.() +[53] T5064.super. +[53] this [16:23] immutable.this.List.apply(scala.this.Predef.wrapIntArray(Array[Int]{1})) [16:20] immutable.this.List.apply <16:20> immutable.this.List -- cgit v1.2.3 From 0ebf72b9498108e67c2133c6522c436af50a18e8 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Sun, 25 Nov 2012 22:00:11 +0100 Subject: introduces global.pendingSuperCall Similarly to global.emptyValDef, which is a dummy that stands for an empty self-type, this commit introduces global.pendingSuperCall, which stands for a yet-to-be-filled-in call to a superclass constructor. pendingSuperCall is emitted by Parsers.template, treated specially by Typers.typedParentType and replaced with a real superclass ctor call by Typers.typedTemplate. To avoid copy/paste, this commit also factors out and unifies dumminess of EmptyTree, emptyValDef and pendingSuperCall - they all don't have a position and actively refuse to gain one, same story for tpe. --- .../scala/reflect/reify/codegen/GenTrees.scala | 4 +- .../scala/reflect/reify/codegen/GenUtils.scala | 3 - src/compiler/scala/tools/nsc/ast/Positions.scala | 2 +- src/compiler/scala/tools/nsc/ast/Trees.scala | 23 +++-- .../tools/nsc/typechecker/StdAttachments.scala | 12 ++- .../scala/tools/nsc/typechecker/Typers.scala | 103 ++++++++------------- src/reflect/scala/reflect/api/BuildUtils.scala | 2 - src/reflect/scala/reflect/api/Trees.scala | 9 ++ .../scala/reflect/internal/BuildUtils.scala | 2 - src/reflect/scala/reflect/internal/Importers.scala | 2 + src/reflect/scala/reflect/internal/Positions.scala | 2 +- src/reflect/scala/reflect/internal/Printers.scala | 4 +- src/reflect/scala/reflect/internal/StdNames.scala | 1 + src/reflect/scala/reflect/internal/Trees.scala | 20 ++-- test/files/run/t5603.check | 4 +- 15 files changed, 95 insertions(+), 98 deletions(-) (limited to 'test') diff --git a/src/compiler/scala/reflect/reify/codegen/GenTrees.scala b/src/compiler/scala/reflect/reify/codegen/GenTrees.scala index bdcc7383b0..d6bafb6759 100644 --- a/src/compiler/scala/reflect/reify/codegen/GenTrees.scala +++ b/src/compiler/scala/reflect/reify/codegen/GenTrees.scala @@ -45,7 +45,9 @@ trait GenTrees { case global.EmptyTree => reifyMirrorObject(EmptyTree) case global.emptyValDef => - mirrorBuildSelect(nme.emptyValDef) + mirrorSelect(nme.emptyValDef) + case global.pendingSuperCall => + mirrorSelect(nme.pendingSuperCall) case FreeDef(_, _, _, _, _) => reifyNestedFreeDef(tree) case FreeRef(_, _) => diff --git a/src/compiler/scala/reflect/reify/codegen/GenUtils.scala b/src/compiler/scala/reflect/reify/codegen/GenUtils.scala index 49877b4286..21db93d8f5 100644 --- a/src/compiler/scala/reflect/reify/codegen/GenUtils.scala +++ b/src/compiler/scala/reflect/reify/codegen/GenUtils.scala @@ -34,9 +34,6 @@ trait GenUtils { def mirrorSelect(name: String): Tree = termPath(nme.UNIVERSE_PREFIX + name) - def mirrorBuildSelect(name: String): Tree = - termPath(nme.UNIVERSE_BUILD_PREFIX + name) - def mirrorMirrorSelect(name: String): Tree = termPath(nme.MIRROR_PREFIX + name) diff --git a/src/compiler/scala/tools/nsc/ast/Positions.scala b/src/compiler/scala/tools/nsc/ast/Positions.scala index d8fb632f73..0503c5fb10 100644 --- a/src/compiler/scala/tools/nsc/ast/Positions.scala +++ b/src/compiler/scala/tools/nsc/ast/Positions.scala @@ -20,7 +20,7 @@ trait Positions extends scala.reflect.internal.Positions { // When we prune due to encountering a position, traverse the // pruned children so we can warn about those lacking positions. t.children foreach { c => - if ((c eq EmptyTree) || (c eq emptyValDef)) () + if (c.isDummy) () else if (c.pos == NoPosition) { reporter.warning(t.pos, " Positioned tree has unpositioned child in phase " + globalPhase) inform("parent: " + treeSymStatus(t)) diff --git a/src/compiler/scala/tools/nsc/ast/Trees.scala b/src/compiler/scala/tools/nsc/ast/Trees.scala index b8c59da95c..e848fa223b 100644 --- a/src/compiler/scala/tools/nsc/ast/Trees.scala +++ b/src/compiler/scala/tools/nsc/ast/Trees.scala @@ -65,6 +65,13 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => // --- factory methods ---------------------------------------------------------- + /** Factory method for a primary constructor super call `super.(args_1)...(args_n)` + */ + def PrimarySuperCall(argss: List[List[Tree]]): Tree = argss match { + case Nil => Apply(gen.mkSuperSelect, Nil) + case xs :: rest => rest.foldLeft(Apply(gen.mkSuperSelect, xs): Tree)(Apply.apply) + } + /** Generates a template with constructor corresponding to * * constrmods (vparams1_) ... (vparams_n) preSuper { presupers } @@ -117,12 +124,12 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => if (vparamss1.isEmpty || !vparamss1.head.isEmpty && vparamss1.head.head.mods.isImplicit) vparamss1 = List() :: vparamss1; val superRef: Tree = atPos(superPos)(gen.mkSuperSelect) - val superCall = Apply(superRef, Nil) // we can't know in advance which of the parents will end up as a superclass - // this requires knowing which of the parents is a type macro and which is not - // and that's something that cannot be found out before typer - // (the type macros aren't in the trunk yet, but there is a plan for them to land there soon) - // this means that we don't know what will be the arguments of the super call - // therefore here we emit a dummy which gets populated when the template is named and typechecked + val superCall = pendingSuperCall // we can't know in advance which of the parents will end up as a superclass + // this requires knowing which of the parents is a type macro and which is not + // and that's something that cannot be found out before typer + // (the type macros aren't in the trunk yet, but there is a plan for them to land there soon) + // this means that we don't know what will be the arguments of the super call + // therefore here we emit a dummy which gets populated when the template is named and typechecked List( // TODO: previously this was `wrappingPos(superPos, lvdefs ::: argss.flatten)` // is it going to be a problem that we can no longer include the `argss`? @@ -330,6 +337,8 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => else super.transform { tree match { + case tree if tree.isDummy => + tree case tpt: TypeTree => if (tpt.original != null) transform(tpt.original) @@ -343,8 +352,6 @@ trait Trees extends scala.reflect.internal.Trees { self: Global => transform(fn) case This(_) if tree.symbol != null && tree.symbol.isPackageClass => tree - case EmptyTree => - tree case _ => val dupl = tree.duplicate if (tree.hasSymbol && (!localOnly || (locals contains tree.symbol)) && !(keepLabels && tree.symbol.isLabel)) diff --git a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala index fa2913bee3..0a1d3bfa7a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala +++ b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala @@ -21,12 +21,14 @@ trait StdAttachments { */ case class SuperCallArgsAttachment(argss: List[List[Tree]]) - /** Extractor for `SuperCallArgsAttachment`. + /** Convenience method for `SuperCallArgsAttachment`. * Compared with `MacroRuntimeAttachment` this attachment has different a usage pattern, * so it really benefits from a dedicated extractor. */ - object CarriesSuperCallArgs { - def unapply(tree: Tree): Option[List[List[Tree]]] = - tree.attachments.get[SuperCallArgsAttachment] collect { case SuperCallArgsAttachment(argss) => argss } - } + def superCallArgs(tree: Tree): Option[List[List[Tree]]] = + tree.attachments.get[SuperCallArgsAttachment] collect { case SuperCallArgsAttachment(argss) => argss } + + /** Determines whether the given tree has an associated SuperCallArgsAttachment. + */ + def hasSuperArgs(tree: Tree): Boolean = superCallArgs(tree).nonEmpty } \ No newline at end of file diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 3f5a4036aa..96432f49a7 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -52,8 +52,10 @@ trait Typers extends Modes with Adaptations with Tags { object UnTyper extends Traverser { override def traverse(tree: Tree) = { - if (tree != EmptyTree) tree.tpe = null - if (tree.hasSymbol) tree.symbol = NoSymbol + if (!tree.isDummy) { + tree.tpe = null + if (tree.hasSymbol) tree.symbol = NoSymbol + } super.traverse(tree) } } @@ -1561,7 +1563,7 @@ trait Typers extends Modes with Adaptations with Tags { var supertpt = typedTypeConstructor(decodedtpt) val supertparams = if (supertpt.hasSymbol) supertpt.symbol.typeParams else Nil if (supertparams.nonEmpty) { - typedPrimaryConstrBody(templ) { superRef => + typedPrimaryConstrBody(templ) { val supertpe = PolyType(supertparams, appliedType(supertpt.tpe, supertparams map (_.tpeHK))) val supercall = New(supertpe, mmap(argss)(_.duplicate)) val treeInfo.Applied(Select(ctor, nme.CONSTRUCTOR), _, _) = supercall @@ -1580,8 +1582,8 @@ trait Typers extends Modes with Adaptations with Tags { } /** Typechecks the mishmash of trees that happen to be stuffed into the primary constructor of a given template. - * Before commencing the typecheck applies `superCallTransform` to a super call (if the latter exists). - * The transform can return `EmptyTree`, in which case the super call is replaced with a literal unit. + * Before commencing the typecheck, replaces the `pendingSuperCall` dummy with the result of `actualSuperCall`. + * `actualSuperCall` can return `EmptyTree`, in which case the dummy is replaced with a literal unit. * * ***Return value and side effects*** * @@ -1606,34 +1608,14 @@ trait Typers extends Modes with Adaptations with Tags { * Block(List( * ValDef(NoMods, x, TypeTree(), 2) * ValDef(NoMods, y, TypeTree(), 4) - * Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR)), List()), + * global.pendingSuperCall, * Literal(Constant(()))) * - * Note the Select(Super(_, _), nme.CONSTRUCTOR) part. This is the representation of - * a fill-me-in-later supercall dummy. The argss are Nil, which encodes the fact - * that supercall argss are unknown during parsing and need to be transplanted from one of the parent types. - * Read more about why the argss are unknown in `tools.nsc.ast.Trees.Template`. - * - * The entire Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR)), List()) is a dummy, - * and it's the one and only possible representation that can be emitted by parser. - * - * Despite of being unwieldy, this tree is quite convenient because: - * * It works as is for the case when no processing is required (empty ctor args for the superclass) - * * Stripping off the Apply produces a core that only needs rewrapping with applications of actual argss. - * - * For some time I was thinking of using just Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR)), - * but that one required wrapping even if the superclass doesn't take any argss. - * - * Another option would be to introduce a singleton tree akin to `emptyValDef` and use it as a dummy. - * Unfortunately this won't work out of the box, because the Super part is supposed to get attributed - * during `typedPrimaryConstrBody`. - * - * We could introduce another attachment for that or change SuperCallArgsAttachment - * to accommodate for the attributed Super, and then using the attached info to adjust the primary constructor - * during typedTemplate. However, given the scope of necessary changes (beyond a few lines) and the fact that, - * according to Martin, the whole thing is to be rewritten soon, I'd say we don't do the follow-up refactoring. + * Note the `pendingSuperCall` part. This is the representation of a fill-me-in-later supercall dummy, + * which encodes the fact that supercall argss are unknown during parsing and need to be transplanted + * from one of the parent types. Read more about why the argss are unknown in `tools.nsc.ast.Trees.Template`. */ - private def typedPrimaryConstrBody(templ: Template)(superCallTransform: Tree => Tree): Tree = + private def typedPrimaryConstrBody(templ: Template)(actualSuperCall: => Tree): Tree = treeInfo.firstConstructor(templ.body) match { case ctor @ DefDef(_, _, _, vparamss, _, cbody @ Block(cstats, cunit)) => val (preSuperStats, superCall) = { @@ -1641,7 +1623,7 @@ trait Typers extends Modes with Adaptations with Tags { (stats map (_.duplicate), if (rest.isEmpty) EmptyTree else rest.head.duplicate) } val superCall1 = (superCall match { - case Apply(superRef @ Select(Super(_, _), nme.CONSTRUCTOR), Nil) => superCallTransform(superRef) + case global.pendingSuperCall => actualSuperCall case EmptyTree => EmptyTree }) orElse cunit val cbody1 = treeCopy.Block(cbody, preSuperStats, superCall1) @@ -1721,7 +1703,7 @@ trait Typers extends Modes with Adaptations with Tags { // and therefore early fields have their type trees not assigned // here we detect this situation and take preventive measures if (treeInfo.hasUntypedPreSuperFields(templ.body)) - typedPrimaryConstrBody(templ)(superRef => EmptyTree) + typedPrimaryConstrBody(templ)(EmptyTree) supertpts mapConserve (tpt => checkNoEscaping.privates(context.owner, tpt)) } catch { @@ -1979,6 +1961,8 @@ trait Typers extends Modes with Adaptations with Tags { validateParentClasses(parents1, selfType) if (clazz.isCase) validateNoCaseAncestor(clazz) + if (clazz.isTrait && hasSuperArgs(parents1.head)) + ConstrArgsInParentOfTraitError(parents1.head, clazz) if ((clazz isSubClass ClassfileAnnotationClass) && !clazz.owner.isPackageClass) unit.error(clazz.pos, "inner classes cannot be classfile annotations") @@ -1990,23 +1974,16 @@ trait Typers extends Modes with Adaptations with Tags { val body = if (isPastTyper || reporter.hasErrors) templ.body else templ.body flatMap rewrappingWrapperTrees(namer.addDerivedTrees(Typer.this, _)) - parents1.head match { - case CarriesSuperCallArgs(argss) => - if (clazz.isTrait) { - ConstrArgsInParentOfTraitError(parents1.head, clazz) - body - } else { - val primaryCtor = treeInfo.firstConstructor(templ.body) - val primaryCtor1 = (deriveDefDef(primaryCtor) { - case block @ Block(earlyVals :+ Apply(superRef, Nil), unit) => - val pos = wrappingPos(parents1.head.pos, argss.flatten) - val superCall = atPos(pos)((superRef /: argss)(Apply.apply)) - Block(earlyVals :+ superCall, unit) setPos pos - }) setPos pos - body map { case `primaryCtor` => primaryCtor1; case stat => stat } - } - case _ => body + val primaryCtor = treeInfo.firstConstructor(body) + val primaryCtor1 = primaryCtor match { + case DefDef(_, _, _, _, _, Block(earlyVals :+ global.pendingSuperCall, unit)) => + val argss = superCallArgs(parents1.head) getOrElse Nil + val pos = wrappingPos(parents1.head.pos, argss.flatten) + val superCall = atPos(pos)(PrimarySuperCall(argss)) + deriveDefDef(primaryCtor)(block => Block(earlyVals :+ superCall, unit) setPos pos) setPos pos + case _ => primaryCtor } + body mapConserve { case `primaryCtor` => primaryCtor1; case stat => stat } } val body1 = typedStats(body, templ.symbol) @@ -5752,21 +5729,21 @@ trait Typers extends Modes with Adaptations with Tags { // enough to see those. See #3938 ConstructorPrefixError(tree, restpe) } else { - //@M fix for #2208 - // if there are no type arguments, normalization does not bypass any checks, so perform it to get rid of AnyRef - if (result.tpe.typeArgs.isEmpty) { - // minimal check: if(result.tpe.typeSymbolDirect eq AnyRefClass) { - // must expand the fake AnyRef type alias, because bootstrapping (init in Definitions) is not - // designed to deal with the cycles in the scala package (ScalaObject extends - // AnyRef, but the AnyRef type alias is entered after the scala package is - // loaded and completed, so that ScalaObject is unpickled while AnyRef is not - // yet defined ) - // !!! TODO - revisit now that ScalaObject is gone. - result setType(restpe) - } else { // must not normalize: type application must be (bounds-)checked (during RefChecks), see #2208 - // during uncurry (after refchecks), all types are normalized - result - } + //@M fix for #2208 + // if there are no type arguments, normalization does not bypass any checks, so perform it to get rid of AnyRef + if (result.tpe.typeArgs.isEmpty) { + // minimal check: if(result.tpe.typeSymbolDirect eq AnyRefClass) { + // must expand the fake AnyRef type alias, because bootstrapping (init in Definitions) is not + // designed to deal with the cycles in the scala package (ScalaObject extends + // AnyRef, but the AnyRef type alias is entered after the scala package is + // loaded and completed, so that ScalaObject is unpickled while AnyRef is not + // yet defined ) + // !!! TODO - revisit now that ScalaObject is gone. + result setType(restpe) + } else { // must not normalize: type application must be (bounds-)checked (during RefChecks), see #2208 + // during uncurry (after refchecks), all types are normalized + result + } } } diff --git a/src/reflect/scala/reflect/api/BuildUtils.scala b/src/reflect/scala/reflect/api/BuildUtils.scala index 0c8e81a220..8f256aa1f5 100644 --- a/src/reflect/scala/reflect/api/BuildUtils.scala +++ b/src/reflect/scala/reflect/api/BuildUtils.scala @@ -59,8 +59,6 @@ private[reflect] trait BuildUtils { self: Universe => def flagsFromBits(bits: Long): FlagSet - def emptyValDef: ValDef - def This(sym: Symbol): Tree def Select(qualifier: Tree, sym: Symbol): Select diff --git a/src/reflect/scala/reflect/api/Trees.scala b/src/reflect/scala/reflect/api/Trees.scala index c98107f9b5..9a65ddaa96 100644 --- a/src/reflect/scala/reflect/api/Trees.scala +++ b/src/reflect/scala/reflect/api/Trees.scala @@ -2366,6 +2366,15 @@ trait Trees { self: Universe => */ val emptyValDef: ValDef + /** An empty superclass constructor call corresponding to: + * super.() + * This is used as a placeholder in the primary constructor body in class templates + * to denote the insertion point of a call to superclass constructor after the typechecker + * figures out the superclass of a given template. + * @group Trees + */ + val pendingSuperCall: Apply + // ---------------------- factories ---------------------------------------------- /** A factory method for `ClassDef` nodes. diff --git a/src/reflect/scala/reflect/internal/BuildUtils.scala b/src/reflect/scala/reflect/internal/BuildUtils.scala index 9f41f0336e..b1b0c5b60b 100644 --- a/src/reflect/scala/reflect/internal/BuildUtils.scala +++ b/src/reflect/scala/reflect/internal/BuildUtils.scala @@ -47,8 +47,6 @@ trait BuildUtils { self: SymbolTable => def flagsFromBits(bits: Long): FlagSet = bits - def emptyValDef: ValDef = self.emptyValDef - def This(sym: Symbol): Tree = self.This(sym) def Select(qualifier: Tree, sym: Symbol): Select = self.Select(qualifier, sym) diff --git a/src/reflect/scala/reflect/internal/Importers.scala b/src/reflect/scala/reflect/internal/Importers.scala index 43902c1930..2f2b02975c 100644 --- a/src/reflect/scala/reflect/internal/Importers.scala +++ b/src/reflect/scala/reflect/internal/Importers.scala @@ -334,6 +334,8 @@ trait Importers extends api.Importers { self: SymbolTable => new ModuleDef(importModifiers(mods), importName(name).toTermName, importTemplate(impl)) case from.emptyValDef => emptyValDef + case from.pendingSuperCall => + pendingSuperCall case from.ValDef(mods, name, tpt, rhs) => new ValDef(importModifiers(mods), importName(name).toTermName, importTree(tpt), importTree(rhs)) case from.DefDef(mods, name, tparams, vparamss, tpt, rhs) => diff --git a/src/reflect/scala/reflect/internal/Positions.scala b/src/reflect/scala/reflect/internal/Positions.scala index faa161d6b1..b65df0e70b 100644 --- a/src/reflect/scala/reflect/internal/Positions.scala +++ b/src/reflect/scala/reflect/internal/Positions.scala @@ -38,7 +38,7 @@ trait Positions extends api.Positions { self: SymbolTable => protected class DefaultPosAssigner extends PosAssigner { var pos: Position = _ override def traverse(t: Tree) { - if (t eq EmptyTree) () + if (t.isDummy) () else if (t.pos == NoPosition) { t.setPos(pos) super.traverse(t) // TODO: bug? shouldn't the traverse be outside of the if? diff --git a/src/reflect/scala/reflect/internal/Printers.scala b/src/reflect/scala/reflect/internal/Printers.scala index 80d247c0ea..6f834803c4 100644 --- a/src/reflect/scala/reflect/internal/Printers.scala +++ b/src/reflect/scala/reflect/internal/Printers.scala @@ -542,8 +542,10 @@ trait Printers extends api.Printers { self: SymbolTable => print(")") case EmptyTree => print("EmptyTree") - case emptyValDef: AnyRef if emptyValDef eq self.emptyValDef => + case self.emptyValDef => print("emptyValDef") + case self.pendingSuperCall => + print("pendingSuperCall") case tree: Tree => val hasSymbol = tree.hasSymbol && tree.symbol != NoSymbol val isError = hasSymbol && tree.symbol.name.toString == nme.ERROR.toString diff --git a/src/reflect/scala/reflect/internal/StdNames.scala b/src/reflect/scala/reflect/internal/StdNames.scala index 5e7f5777b2..c870d8972d 100644 --- a/src/reflect/scala/reflect/internal/StdNames.scala +++ b/src/reflect/scala/reflect/internal/StdNames.scala @@ -730,6 +730,7 @@ trait StdNames { val null_ : NameType = "null" val ofDim: NameType = "ofDim" val origin: NameType = "origin" + val pendingSuperCall: NameType = "pendingSuperCall" val prefix : NameType = "prefix" val productArity: NameType = "productArity" val productElement: NameType = "productElement" diff --git a/src/reflect/scala/reflect/internal/Trees.scala b/src/reflect/scala/reflect/internal/Trees.scala index 0087bb93e7..047298cf82 100644 --- a/src/reflect/scala/reflect/internal/Trees.scala +++ b/src/reflect/scala/reflect/internal/Trees.scala @@ -36,6 +36,7 @@ trait Trees extends api.Trees { self: SymbolTable => def isDef = false def isEmpty = false + def isDummy = false /** The canonical way to test if a Tree represents a term. */ @@ -228,14 +229,6 @@ trait Trees extends api.Trees { self: SymbolTable => override def isDef = true } - case object EmptyTree extends TermTree { - val asList = List(this) - super.tpe_=(NoType) - override def tpe_=(t: Type) = - if (t != NoType) throw new UnsupportedOperationException("tpe_=("+t+") inapplicable for ") - override def isEmpty = true - } - abstract class MemberDef extends DefTree with MemberDefApi { def mods: Modifiers def keyword: String = this match { @@ -599,6 +592,7 @@ trait Trees extends api.Trees { self: SymbolTable => case _: ApplyToImplicitArgs => new ApplyToImplicitArgs(fun, args) case _: ApplyImplicitView => new ApplyImplicitView(fun, args) // TODO: ApplyConstructor ??? + case self.pendingSuperCall => self.pendingSuperCall case _ => new Apply(fun, args) }).copyAttrs(tree) def ApplyDynamic(tree: Tree, qual: Tree, args: List[Tree]) = @@ -961,12 +955,20 @@ trait Trees extends api.Trees { self: SymbolTable => def ValDef(sym: Symbol): ValDef = ValDef(sym, EmptyTree) - object emptyValDef extends ValDef(Modifiers(PRIVATE), nme.WILDCARD, TypeTree(NoType), EmptyTree) { + trait DummyTree extends Tree { override def isEmpty = true + override def isDummy = true super.setPos(NoPosition) override def setPos(pos: Position) = { assert(false); this } + super.setType(NoType) + override def tpe_=(t: Type) = + if (t != NoType) throw new UnsupportedOperationException("tpe_=("+t+") inapplicable for "+self.toString) } + case object EmptyTree extends TermTree with DummyTree { val asList = List(this) } + object emptyValDef extends ValDef(Modifiers(PRIVATE), nme.WILDCARD, TypeTree(NoType), EmptyTree) with DummyTree + object pendingSuperCall extends Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List()) with DummyTree + def DefDef(sym: Symbol, mods: Modifiers, vparamss: List[List[ValDef]], rhs: Tree): DefDef = atPos(sym.pos) { assert(sym != NoSymbol) diff --git a/test/files/run/t5603.check b/test/files/run/t5603.check index 5127d3c1c7..3b2eb55313 100644 --- a/test/files/run/t5603.check +++ b/test/files/run/t5603.check @@ -12,7 +12,7 @@ [95:101] private[this] val i: [98:101]Int = _; <119:139>def ([95]i: [98]Int) = <119:139>{ <119:139>val nameElse = <134:139>"Bob"; - [94][94][94]super.(); + [NoPosition][NoPosition][NoPosition]super.(); [94]() }; [168:184]val name = [179:184]"avc"; @@ -20,7 +20,7 @@ }; [215:241]object Test extends [227:241][235:238]App { [227]def () = [227]{ - [227][227][227]super.(); + [NoPosition][NoPosition][NoPosition]super.(); [227]() }; [NoPosition] -- cgit v1.2.3 From d69912293410dd6f3cb205d70b9de8d1bd10ded9 Mon Sep 17 00:00:00 2001 From: James Iry Date: Thu, 6 Dec 2012 21:46:14 -0800 Subject: SI-5789 Removes assertion about implclass flag in Mixin.scala The assertion that the class being mixed from should be an implclass seems reasonable, but the flag isn't always set. In order to stop the bleeding this fix turns the assertion into a debug warning. Issue SI-6782 will track figuring out the root cause of the missing flag. --- src/compiler/scala/tools/nsc/transform/Mixin.scala | 3 +-- test/files/run/t5789.check | 1 + test/files/run/t5789.scala | 29 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/files/run/t5789.check create mode 100644 test/files/run/t5789.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/transform/Mixin.scala b/src/compiler/scala/tools/nsc/transform/Mixin.scala index 64bb98e2c5..e59a70869f 100644 --- a/src/compiler/scala/tools/nsc/transform/Mixin.scala +++ b/src/compiler/scala/tools/nsc/transform/Mixin.scala @@ -269,7 +269,7 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL { /** Mix in members of implementation class mixinClass into class clazz */ def mixinImplClassMembers(mixinClass: Symbol, mixinInterface: Symbol) { - assert(mixinClass.isImplClass, "Not an impl class:" + + if (!mixinClass.isImplClass) debugwarn ("Impl class flag is not set " + ((mixinClass.debugLocationString, mixinInterface.debugLocationString))) for (member <- mixinClass.info.decls ; if isForwarded(member)) { @@ -360,7 +360,6 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL { // first complete the superclass with mixed in members addMixedinMembers(clazz.superClass, unit) - //Console.println("adding members of " + clazz.info.baseClasses.tail.takeWhile(superclazz !=) + " to " + clazz);//DEBUG for (mc <- clazz.mixinClasses ; if mc hasFlag lateINTERFACE) { // @SEAN: adding trait tracking so we don't have to recompile transitive closures unit.depends += mc diff --git a/test/files/run/t5789.check b/test/files/run/t5789.check new file mode 100644 index 0000000000..3c459e9a54 --- /dev/null +++ b/test/files/run/t5789.check @@ -0,0 +1 @@ +completed successfully diff --git a/test/files/run/t5789.scala b/test/files/run/t5789.scala new file mode 100644 index 0000000000..4169e34959 --- /dev/null +++ b/test/files/run/t5789.scala @@ -0,0 +1,29 @@ + +import scala.tools.nsc._ +import interpreter.ILoop + +object Test { + + def main(args : Array[String]) { + + val code = """ + val n = 2 + () => n + """ + + val s = new Settings() + s.optimise.value = false + s.debug.value = true + s.log.value = List("all") + val lines = ILoop.runForTranscript(code + "\n" + code, s).lines.toList + + + if (lines exists (_ contains "Abandoning crashed session")) { + lines foreach println + println("crashed!") + } else { + println("completed successfully") + } + } +} + -- cgit v1.2.3 From 850128db455830e38f550d25406bb0a6dddde3b5 Mon Sep 17 00:00:00 2001 From: James Iry Date: Fri, 7 Dec 2012 11:12:17 -0800 Subject: SI-5789 Checks in the right version of the test In my other commit I had a version of the test that didn't actually reproduce the problem because it didn't set the optimize flag --- test/files/run/t5789.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'test') diff --git a/test/files/run/t5789.scala b/test/files/run/t5789.scala index 4169e34959..bbb0401941 100644 --- a/test/files/run/t5789.scala +++ b/test/files/run/t5789.scala @@ -12,9 +12,7 @@ object Test { """ val s = new Settings() - s.optimise.value = false - s.debug.value = true - s.log.value = List("all") + s.optimise.value = true val lines = ILoop.runForTranscript(code + "\n" + code, s).lines.toList -- cgit v1.2.3 From 5f2b7c4c36f96f955573c4ac322b8ca6d632abc1 Mon Sep 17 00:00:00 2001 From: James Iry Date: Fri, 7 Dec 2012 15:46:04 -0800 Subject: SI-5789 Use the ReplTest framework in the test Don't check for the crashed message, just dump the output from the REPL. Use the ReplTest framework to the make the test clean --- test/files/run/t5789.check | 15 ++++++++++++++- test/files/run/t5789.scala | 27 +++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'test') diff --git a/test/files/run/t5789.check b/test/files/run/t5789.check index 3c459e9a54..ea8d4966b1 100644 --- a/test/files/run/t5789.check +++ b/test/files/run/t5789.check @@ -1 +1,14 @@ -completed successfully +Type in expressions to have them evaluated. +Type :help for more information. + +scala> + +scala> val n = 2 +n: Int = 2 + +scala> () => n +res0: () => Int = + +scala> + +scala> diff --git a/test/files/run/t5789.scala b/test/files/run/t5789.scala index bbb0401941..461f6a4aae 100644 --- a/test/files/run/t5789.scala +++ b/test/files/run/t5789.scala @@ -1,27 +1,14 @@ import scala.tools.nsc._ import interpreter.ILoop +import scala.tools.partest.ReplTest -object Test { - def main(args : Array[String]) { - - val code = """ - val n = 2 - () => n - """ - - val s = new Settings() - s.optimise.value = true - val lines = ILoop.runForTranscript(code + "\n" + code, s).lines.toList - - - if (lines exists (_ contains "Abandoning crashed session")) { - lines foreach println - println("crashed!") - } else { - println("completed successfully") - } - } +object Test extends ReplTest { + override def extraSettings = "-Yinline" + def code = """ + val n = 2 + () => n + """ } -- cgit v1.2.3 From d526f8bd74a3a6b878dda77bf19beb60dbc28f81 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 8 Dec 2012 09:30:42 +0100 Subject: SI-6690 Release reference to last dequeued element. Avoids leaks in MutableList and its more better known child, mutable.Queue, when the last element is dequeued or when we take the tail of a one element collection. Refactors copy/pasted code between the two implementations of tail. --- .../scala/collection/mutable/MutableList.scala | 10 ++-- src/library/scala/collection/mutable/Queue.scala | 28 +++++----- test/files/run/t6690.scala | 62 ++++++++++++++++++++++ 3 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 test/files/run/t6690.scala (limited to 'test') diff --git a/src/library/scala/collection/mutable/MutableList.scala b/src/library/scala/collection/mutable/MutableList.scala index 183de5b727..4bbe604cd3 100644 --- a/src/library/scala/collection/mutable/MutableList.scala +++ b/src/library/scala/collection/mutable/MutableList.scala @@ -56,12 +56,16 @@ extends AbstractSeq[A] /** Returns the rest of this list */ override def tail: MutableList[A] = { - require(nonEmpty, "tail of empty list") val tl = new MutableList[A] + tailImpl(tl) + tl + } + + protected final def tailImpl(tl: MutableList[A]) { + require(nonEmpty, "tail of empty list") tl.first0 = first0.tail - tl.last0 = last0 tl.len = len - 1 - tl + tl.last0 = if (tl.len == 0) new LinkedList[A]() else last0 } /** Prepends a single element to this list. This operation takes constant diff --git a/src/library/scala/collection/mutable/Queue.scala b/src/library/scala/collection/mutable/Queue.scala index 5ea012eb9f..8956a1105e 100644 --- a/src/library/scala/collection/mutable/Queue.scala +++ b/src/library/scala/collection/mutable/Queue.scala @@ -66,7 +66,7 @@ extends MutableList[A] else { val res = first0.elem first0 = first0.next - len -= 1 + decrementLength() res } @@ -82,11 +82,11 @@ extends MutableList[A] else if (p(first0.elem)) { val res: Option[A] = Some(first0.elem) first0 = first0.next - len -= 1 + decrementLength() res } else { val optElem = removeFromList(p) - if (optElem != None) len -= 1 + if (optElem != None) decrementLength() optElem } @@ -119,7 +119,7 @@ extends MutableList[A] while ((first0.nonEmpty) && p(first0.elem)) { res += first0.elem first0 = first0.next - len -= 1 + decrementLength() } if (first0.isEmpty) res else removeAllFromList(p, res) @@ -130,10 +130,10 @@ extends MutableList[A] var leftlst = first0 while (leftlst.next.nonEmpty) { if (p(leftlst.next.elem)) { - res += leftlst.next.elem - if (leftlst.next eq last0) last0 = leftlst - leftlst.next = leftlst.next.next - len -= 1 + res += leftlst.next.elem + if (leftlst.next eq last0) last0 = leftlst + leftlst.next = leftlst.next.next + decrementLength() } else leftlst = leftlst.next } res @@ -154,7 +154,7 @@ extends MutableList[A] else { val res: Option[LinkedList[A]] = Some(cell.next) cell.next = cell.next.next - len -= 1 + decrementLength() res } } @@ -170,11 +170,8 @@ extends MutableList[A] // TODO - Don't override this just for new to create appropriate type.... override def tail: Queue[A] = { - require(nonEmpty, "tail of empty list") val tl = new Queue[A] - tl.first0 = first0.tail - tl.last0 = last0 - tl.len = len - 1 + tailImpl(tl) tl } @@ -183,6 +180,11 @@ extends MutableList[A] bf ++= seq bf.result } + + private[this] def decrementLength() { + len -= 1 + if (len == 0) last0 = new LinkedList[A]() + } } diff --git a/test/files/run/t6690.scala b/test/files/run/t6690.scala new file mode 100644 index 0000000000..43ede967a0 --- /dev/null +++ b/test/files/run/t6690.scala @@ -0,0 +1,62 @@ +import scala.collection.mutable + +object Test extends App { + def last0(ml: mutable.MutableList[Int]) = + ml.asInstanceOf[{def last0: mutable.LinkedList[Int]}].last0 + + def first0(ml: mutable.MutableList[Int]) = + ml.asInstanceOf[{def first0: mutable.LinkedList[Int]}].first0 + + val f = mutable.Queue[Int]() + def check(desc: String) { + assert(f.length == 0, s"$desc: non empty: $f") + assert(last0(f).isEmpty, s"$desc: last0 leak: ${last0(f)}") + assert(first0(f).isEmpty, s"$desc: first0 leak: ${last0(f)}") + } + + f.enqueue(1) + f.dequeue() + check("dequeue 1") + + f.enqueue(1) + f.enqueue(2) + f.dequeue() + assert(last0(f).toList == List(2), last0(f)) + f.dequeue() + check("dequeue 2") + + f.enqueue(1) + f.dequeueAll(_ => false) + f.dequeueAll(_ => true) + check("dequeueAll") + + f.enqueue(1) + f.dequeueFirst(_ => true) + check("dequeueFirst") + + { + f.enqueue(1) + val tail = f.tail + assert(last0(tail).isEmpty, last0(tail)) + assert(first0(tail).isEmpty, first0(tail)) + } + + { + val ml = mutable.MutableList[Int]() + 1 +=: ml + val tail = ml.tail + assert(last0(tail).isEmpty, last0(tail)) + assert(first0(tail).isEmpty, first0(tail)) + } + + { + val ml = mutable.MutableList[Int]() + 1 +=: ml + ml += 2 + val tail = ml.tail + assert(last0(tail).toList == List(2), last0(tail)) + assert(first0(tail) == last0(tail).toList, first0(tail)) + assert(last0(tail.tail).toList == Nil, last0(tail.tail).toList) + assert(first0(tail.tail) == Nil, first0(tail.tail)) + } +} -- cgit v1.2.3 From bd04b2cb681328a0e35c4f77b58ec4a623e047c7 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 8 Dec 2012 12:04:00 +0100 Subject: SI-6614 Test case for fixed ArrayStack misconduct. Not sure when it was fixed, but in 2.9.2 things were messed up: scala> (for (i <- 0 to 10) yield { val in = ArrayStack.tabulate(i)(_.toString); (in, (in filter (_ => true)) == in) }).mkString("\n") res14: String = (ArrayStack(),true) (ArrayStack(0),true) (ArrayStack(0, 1),true) (ArrayStack(1, 2, null),false) (ArrayStack(0, 1, 2, 3),true) (ArrayStack(3, 4, null, null, null),false) (ArrayStack(2, 3, 4, 5, null, null),false) (ArrayStack(1, 2, 3, 4, 5, 6, null),false) (ArrayStack(0, 1, 2, 3, 4, 5, 6, 7),true) (ArrayStack(7, 8, null, null, null, null, null, null, null),false) (ArrayStack(6, 7, 8, 9, null, null, null, null, null, null),false) --- test/files/run/t6614.check | 11 +++++++++++ test/files/run/t6614.scala | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/files/run/t6614.check create mode 100644 test/files/run/t6614.scala (limited to 'test') diff --git a/test/files/run/t6614.check b/test/files/run/t6614.check new file mode 100644 index 0000000000..2e80ebda8b --- /dev/null +++ b/test/files/run/t6614.check @@ -0,0 +1,11 @@ +(ArrayStack(),true) +(ArrayStack(0),true) +(ArrayStack(0, 1),true) +(ArrayStack(0, 1, 2),true) +(ArrayStack(0, 1, 2, 3),true) +(ArrayStack(0, 1, 2, 3, 4),true) +(ArrayStack(0, 1, 2, 3, 4, 5),true) +(ArrayStack(0, 1, 2, 3, 4, 5, 6),true) +(ArrayStack(0, 1, 2, 3, 4, 5, 6, 7),true) +(ArrayStack(0, 1, 2, 3, 4, 5, 6, 7, 8),true) +(ArrayStack(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),true) diff --git a/test/files/run/t6614.scala b/test/files/run/t6614.scala new file mode 100644 index 0000000000..3ad9f36fc4 --- /dev/null +++ b/test/files/run/t6614.scala @@ -0,0 +1,8 @@ +object Test extends App { + import scala.collection.mutable.ArrayStack + + println((for (i <- 0 to 10) yield { + val in = ArrayStack.tabulate(i)(_.toString) + (in, (in filter (_ => true)) == in) + }).mkString("\n")) +} -- cgit v1.2.3 From e249f2eeb8451c1b754711294ed954c8de7775fb Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 10 Dec 2012 12:46:13 +0100 Subject: SI-4922 Show default in Scaladoc for generic methods. We must account for cloned symbols. --- .../scala/tools/nsc/doc/model/ModelFactory.scala | 11 +++++++- test/scaladoc/run/t4922.check | 1 + test/scaladoc/run/t4922.scala | 32 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/scaladoc/run/t4922.check create mode 100644 test/scaladoc/run/t4922.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala b/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala index 3ae1210ebf..8ced574676 100644 --- a/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala @@ -950,7 +950,16 @@ class ModelFactory(val global: Global, val settings: doc.Settings) { // units.filter should return only one element (currentRun.units filter (_.source.file == aSym.sourceFile)).toList match { case List(unit) => - (unit.body find (_.symbol == aSym)) match { + // SI-4922 `sym == aSym` is insufficent if `aSym` is a clone of symbol + // of the parameter in the tree, as can happen with type parametric methods. + def isCorrespondingParam(sym: Symbol) = ( + sym != null && + sym != NoSymbol && + sym.owner == aSym.owner && + sym.name == aSym.name && + sym.isParamWithDefault + ) + (unit.body find (t => isCorrespondingParam(t.symbol))) match { case Some(ValDef(_,_,_,rhs)) => makeTree(rhs) case _ => None } diff --git a/test/scaladoc/run/t4922.check b/test/scaladoc/run/t4922.check new file mode 100644 index 0000000000..619c56180b --- /dev/null +++ b/test/scaladoc/run/t4922.check @@ -0,0 +1 @@ +Done. diff --git a/test/scaladoc/run/t4922.scala b/test/scaladoc/run/t4922.scala new file mode 100644 index 0000000000..bce87ac980 --- /dev/null +++ b/test/scaladoc/run/t4922.scala @@ -0,0 +1,32 @@ +import scala.tools.nsc.doc.model._ +import scala.tools.partest.ScaladocModelTest + +object Test extends ScaladocModelTest { + + // Test code + override def code = """ + // This the default values should be displayed + + object Test { + def f (a: Any = "".isEmpty) = () + def g[A](b: A = null) = () + } + """ + + // no need for special settings + def scaladocSettings = "" + + def testModel(rootPackage: Package) = { + // get the quick access implicit defs in scope (_package(s), _class(es), _trait(s), object(s) _method(s), _value(s)) + import access._ + + val Test = rootPackage._object("Test") + val f = Test._method("f") + val g = Test._method("g") + + def assertEqual(s1: String, s2: String) = assert(s1 == s2, s1 + " == " + s2) + + assertEqual(f.valueParams(0)(0).defaultValue.get.expression, "\"\".isEmpty") + assertEqual(g.valueParams(0)(0).defaultValue.get.expression, "null") + } +} \ No newline at end of file -- cgit v1.2.3 From 286abfc4cf6ed41c1622f4964aae321b0b9b6c6d Mon Sep 17 00:00:00 2001 From: Andriy Polishchuk Date: Thu, 6 Dec 2012 11:59:05 +0200 Subject: SI-5841 reification of renamed imports Reification of renamed imports is done by catching Selects with name != their tree.symbol.name, replacing this name with tree.symbol.name, and then doing reifyProduct in case of renamed terms and reifyBoundType (inner) in case of renamed types. --- .../scala/reflect/reify/codegen/GenTrees.scala | 7 +++++++ .../scala/reflect/reify/utils/Extractors.scala | 2 ++ test/files/run/reify_renamed_term_basic.check | 1 + test/files/run/reify_renamed_term_basic.scala | 20 ++++++++++++++++++ .../run/reify_renamed_term_local_to_reifee.check | 1 + .../run/reify_renamed_term_local_to_reifee.scala | 20 ++++++++++++++++++ .../run/reify_renamed_term_overloaded_method.check | 1 + .../run/reify_renamed_term_overloaded_method.scala | 17 +++++++++++++++ test/files/run/reify_renamed_term_si5841.check | 1 + test/files/run/reify_renamed_term_si5841.scala | 7 +++++++ test/files/run/reify_renamed_type_basic.check | 1 + test/files/run/reify_renamed_type_basic.scala | 16 +++++++++++++++ .../run/reify_renamed_type_local_to_reifee.check | 1 + .../run/reify_renamed_type_local_to_reifee.scala | 24 ++++++++++++++++++++++ test/files/run/reify_renamed_type_spliceable.check | 1 + test/files/run/reify_renamed_type_spliceable.scala | 21 +++++++++++++++++++ 16 files changed, 141 insertions(+) create mode 100644 test/files/run/reify_renamed_term_basic.check create mode 100644 test/files/run/reify_renamed_term_basic.scala create mode 100644 test/files/run/reify_renamed_term_local_to_reifee.check create mode 100644 test/files/run/reify_renamed_term_local_to_reifee.scala create mode 100644 test/files/run/reify_renamed_term_overloaded_method.check create mode 100644 test/files/run/reify_renamed_term_overloaded_method.scala create mode 100644 test/files/run/reify_renamed_term_si5841.check create mode 100644 test/files/run/reify_renamed_term_si5841.scala create mode 100644 test/files/run/reify_renamed_type_basic.check create mode 100644 test/files/run/reify_renamed_type_basic.scala create mode 100644 test/files/run/reify_renamed_type_local_to_reifee.check create mode 100644 test/files/run/reify_renamed_type_local_to_reifee.scala create mode 100644 test/files/run/reify_renamed_type_spliceable.check create mode 100644 test/files/run/reify_renamed_type_spliceable.scala (limited to 'test') diff --git a/src/compiler/scala/reflect/reify/codegen/GenTrees.scala b/src/compiler/scala/reflect/reify/codegen/GenTrees.scala index 918aedce51..f489476e96 100644 --- a/src/compiler/scala/reflect/reify/codegen/GenTrees.scala +++ b/src/compiler/scala/reflect/reify/codegen/GenTrees.scala @@ -147,6 +147,11 @@ trait GenTrees { } case tree @ Ident(_) if tree.symbol.isLocalToReifee => mirrorCall(nme.Ident, reify(tree.name)) + case Select(qual, name) => + if (tree.symbol != NoSymbol && tree.symbol.name != name) + reifyProduct(Select(qual, tree.symbol.name)) + else + reifyProduct(tree) case _ => throw new Error("internal error: %s (%s, %s) is not supported".format(tree, tree.productPrefix, tree.getClass)) } @@ -196,6 +201,8 @@ trait GenTrees { } tree match { + case Select(qual, name) if (name != tree.symbol.name) => + reifyBoundType(Select(qual, tree.symbol.name)) case Select(_, _) => reifyBoundType(tree) case SelectFromTypeTree(_, _) => diff --git a/src/compiler/scala/reflect/reify/utils/Extractors.scala b/src/compiler/scala/reflect/reify/utils/Extractors.scala index b60d15c1d4..ebbcb95481 100644 --- a/src/compiler/scala/reflect/reify/utils/Extractors.scala +++ b/src/compiler/scala/reflect/reify/utils/Extractors.scala @@ -251,6 +251,8 @@ trait Extractors { object BoundTerm { def unapply(tree: Tree): Option[Tree] = tree match { + case Select(_, name) if name.isTermName => + Some(tree) case Ident(name) if name.isTermName => Some(tree) case This(_) => diff --git a/test/files/run/reify_renamed_term_basic.check b/test/files/run/reify_renamed_term_basic.check new file mode 100644 index 0000000000..e78f94fffd --- /dev/null +++ b/test/files/run/reify_renamed_term_basic.check @@ -0,0 +1 @@ +((),(),()) diff --git a/test/files/run/reify_renamed_term_basic.scala b/test/files/run/reify_renamed_term_basic.scala new file mode 100644 index 0000000000..cd76def395 --- /dev/null +++ b/test/files/run/reify_renamed_term_basic.scala @@ -0,0 +1,20 @@ +import scala.reflect.runtime.universe._ +import scala.tools.reflect.Eval + +object A { + object B { + val c = () + } +} + +object Test extends App { + import A.{B => X} + import A.B.{c => y} + import X.{c => z} + + val expr = reify ( + X.c, y, z + ) + + println(expr.eval) +} \ No newline at end of file diff --git a/test/files/run/reify_renamed_term_local_to_reifee.check b/test/files/run/reify_renamed_term_local_to_reifee.check new file mode 100644 index 0000000000..e78f94fffd --- /dev/null +++ b/test/files/run/reify_renamed_term_local_to_reifee.check @@ -0,0 +1 @@ +((),(),()) diff --git a/test/files/run/reify_renamed_term_local_to_reifee.scala b/test/files/run/reify_renamed_term_local_to_reifee.scala new file mode 100644 index 0000000000..1860316a5b --- /dev/null +++ b/test/files/run/reify_renamed_term_local_to_reifee.scala @@ -0,0 +1,20 @@ +import scala.reflect.runtime.universe._ +import scala.tools.reflect.Eval + +object A { + object B { + val c = () + } +} + +object Test extends App { + val expr = reify { + import A.{B => X} + import A.B.{c => y} + import X.{c => z} + + (X.c, y, z) + } + + println(expr.eval) +} \ No newline at end of file diff --git a/test/files/run/reify_renamed_term_overloaded_method.check b/test/files/run/reify_renamed_term_overloaded_method.check new file mode 100644 index 0000000000..48082f72f0 --- /dev/null +++ b/test/files/run/reify_renamed_term_overloaded_method.check @@ -0,0 +1 @@ +12 diff --git a/test/files/run/reify_renamed_term_overloaded_method.scala b/test/files/run/reify_renamed_term_overloaded_method.scala new file mode 100644 index 0000000000..3ef442d203 --- /dev/null +++ b/test/files/run/reify_renamed_term_overloaded_method.scala @@ -0,0 +1,17 @@ +import scala.reflect.runtime.universe._ +import scala.tools.reflect.Eval + +object O { + def show(i: Int) = i.toString + def show(s: String) = s +} + +object Test extends App { + import O.{show => s} + + val expr = reify { + s("1") + s(2) + } + + println(expr.eval) +} \ No newline at end of file diff --git a/test/files/run/reify_renamed_term_si5841.check b/test/files/run/reify_renamed_term_si5841.check new file mode 100644 index 0000000000..6031277b76 --- /dev/null +++ b/test/files/run/reify_renamed_term_si5841.check @@ -0,0 +1 @@ +class scala.reflect.runtime.JavaUniverse diff --git a/test/files/run/reify_renamed_term_si5841.scala b/test/files/run/reify_renamed_term_si5841.scala new file mode 100644 index 0000000000..ef18d650bf --- /dev/null +++ b/test/files/run/reify_renamed_term_si5841.scala @@ -0,0 +1,7 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{universe => ru} +import scala.tools.reflect.Eval + +object Test extends App { + println(reify{ru}.eval.getClass) +} \ No newline at end of file diff --git a/test/files/run/reify_renamed_type_basic.check b/test/files/run/reify_renamed_type_basic.check new file mode 100644 index 0000000000..6a452c185a --- /dev/null +++ b/test/files/run/reify_renamed_type_basic.check @@ -0,0 +1 @@ +() diff --git a/test/files/run/reify_renamed_type_basic.scala b/test/files/run/reify_renamed_type_basic.scala new file mode 100644 index 0000000000..23729e5c54 --- /dev/null +++ b/test/files/run/reify_renamed_type_basic.scala @@ -0,0 +1,16 @@ +import scala.reflect.runtime.universe._ +import scala.tools.reflect.Eval + +object O { + type A = Unit +} + +object Test extends App { + import O.{A => X} + + def expr = reify { + val a: X = () + } + + println(expr.eval) +} \ No newline at end of file diff --git a/test/files/run/reify_renamed_type_local_to_reifee.check b/test/files/run/reify_renamed_type_local_to_reifee.check new file mode 100644 index 0000000000..6a452c185a --- /dev/null +++ b/test/files/run/reify_renamed_type_local_to_reifee.check @@ -0,0 +1 @@ +() diff --git a/test/files/run/reify_renamed_type_local_to_reifee.scala b/test/files/run/reify_renamed_type_local_to_reifee.scala new file mode 100644 index 0000000000..ed1bad239e --- /dev/null +++ b/test/files/run/reify_renamed_type_local_to_reifee.scala @@ -0,0 +1,24 @@ +import scala.reflect.runtime.universe._ +import scala.tools.reflect.Eval + +object O { + type A = Unit +} + +object Test extends App { + val expr = reify { + import O.{A => X} + + val a: X = () + + object P { + type B = Unit + } + + import P.{B => Y} + + val b: Y = () + } + + println(expr.eval) +} \ No newline at end of file diff --git a/test/files/run/reify_renamed_type_spliceable.check b/test/files/run/reify_renamed_type_spliceable.check new file mode 100644 index 0000000000..6a452c185a --- /dev/null +++ b/test/files/run/reify_renamed_type_spliceable.check @@ -0,0 +1 @@ +() diff --git a/test/files/run/reify_renamed_type_spliceable.scala b/test/files/run/reify_renamed_type_spliceable.scala new file mode 100644 index 0000000000..9c2cff5199 --- /dev/null +++ b/test/files/run/reify_renamed_type_spliceable.scala @@ -0,0 +1,21 @@ +import scala.reflect.runtime.universe._ +import scala.tools.reflect.Eval + +abstract class C { + type T >: Null +} + +object Test extends App { + def foo(c: C) = { + import c.{T => U} + reify { + val x: U = null + } + } + + val expr = foo(new C { + type T = AnyRef + }) + + println(expr.eval) +} \ No newline at end of file -- cgit v1.2.3