From eccdddcc73dc532885abaee500e1b35953723d5a Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 27 Mar 2006 13:34:36 +0000 Subject: 1. 2. Cleaned up search of implicit names to make it conformant to the spec. 3. Made Sean's SOURCE CHANGE messages dependent on option -debug. --- test/files/neg/faculty.check | 4 ++ test/files/neg/faculty.scala | 5 ++ test/files/neg/imp2.check | 7 +++ test/files/neg/imp2.scala | 19 +++++++ test/files/neg/viewtest.check | 7 +++ test/files/neg/viewtest.scala | 116 +++++++++++++++++++++++++++++++++++++++++ test/files/pos/imp2.scala | 5 ++ test/files/pos/viewtest2.scala | 2 - 8 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 test/files/neg/faculty.check create mode 100755 test/files/neg/faculty.scala create mode 100644 test/files/neg/imp2.check create mode 100755 test/files/neg/imp2.scala create mode 100644 test/files/neg/viewtest.check create mode 100755 test/files/neg/viewtest.scala create mode 100755 test/files/pos/imp2.scala (limited to 'test') diff --git a/test/files/neg/faculty.check b/test/files/neg/faculty.check new file mode 100644 index 0000000000..1a1c65f853 --- /dev/null +++ b/test/files/neg/faculty.check @@ -0,0 +1,4 @@ +faculty.scala:3 error: recursive method faculty needs result type + def faculty(x: int) = if (x == 0) 1 else x * faculty(x - 1) + ^ +one error found diff --git a/test/files/neg/faculty.scala b/test/files/neg/faculty.scala new file mode 100755 index 0000000000..464033ef30 --- /dev/null +++ b/test/files/neg/faculty.scala @@ -0,0 +1,5 @@ +object Test { + + def faculty(x: int) = if (x == 0) 1 else x * faculty(x - 1) + +} diff --git a/test/files/neg/imp2.check b/test/files/neg/imp2.check new file mode 100644 index 0000000000..2daca73bf3 --- /dev/null +++ b/test/files/neg/imp2.check @@ -0,0 +1,7 @@ +imp2.scala:18 error: reference to f is ambiguous; +it is imported twice in the same scope by +import b.{_} +and import a.{_} + val x = f + ^ +one error found diff --git a/test/files/neg/imp2.scala b/test/files/neg/imp2.scala new file mode 100755 index 0000000000..20b3df5d91 --- /dev/null +++ b/test/files/neg/imp2.scala @@ -0,0 +1,19 @@ +abstract class C { + val f: int +} + +object A extends C { + val f = 1 +} + +object B extends C { + val f = 2 +} + +object Test { + val a: C = A; + val b: C = B; + import a._ + import b._ + val x = f +} diff --git a/test/files/neg/viewtest.check b/test/files/neg/viewtest.check new file mode 100644 index 0000000000..f7920de368 --- /dev/null +++ b/test/files/neg/viewtest.check @@ -0,0 +1,7 @@ +viewtest.scala:104 error: ambiguous implicit value: + both method view4 in object O of type [a](a)a + and method identity in object Predef of type [a](a)a + match expected type (test.Str) => test.Ordered[test.Str] + t = t insert Str(s) + ^ +one error found diff --git a/test/files/neg/viewtest.scala b/test/files/neg/viewtest.scala new file mode 100755 index 0000000000..deb6480983 --- /dev/null +++ b/test/files/neg/viewtest.scala @@ -0,0 +1,116 @@ +package test; + +/** A trait for totally ordered data. + */ +trait Ordered[+a] { + + /** Result of comparing `this' with operand `that'. + * returns `x' where + * x < 0 iff this < that + * x == 0 iff this == that + * x > 0 iff this > that + */ + def compareTo [b >: a <% Ordered[b]](that: b): int; + + def < [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) < 0; + + def > [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) > 0; + + def <= [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) <= 0; + + def >= [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) >= 0; +} + + +object O { + + implicit def view1(x: String): Ordered[String] = new Ordered[String] { + def compareTo [b >: String <% Ordered[b]](y: b): int = y match { + case y1: String => x compareTo y1; + case _ => -(y compareTo x) + } + } + implicit def view2(x: char): Ordered[char] = new Ordered[char] { + def compareTo [b >: char <% Ordered[b]](y: b): int = y match { + case y1: char => x - y1; + case _ => -(y compareTo x) + } + } + + implicit def view3[a <% Ordered[a]](x: List[a]): Ordered[List[a]] = + new Ordered[List[a]] { + def compareTo [b >: List[a] <% Ordered[b]](y: b): int = y match { + case y1: List[a] => compareLists(x, y1); + case _ => -(y compareTo x) + } + private def compareLists(xs: List[a], ys: List[a]): int = { + if (xs.isEmpty && ys.isEmpty) 0 + else if (xs.isEmpty) -1 + else if (ys.isEmpty) 1 + else { + val s = xs.head compareTo ys.head; + if (s != 0) s + else compareLists(xs.tail, ys.tail) + } + } + } + implicit def view4[a](x: a): a = x; +} + +trait Tree[+a <% Ordered[a]] { + def insert[b >: a <% Ordered[b]](x: b): Tree[b]; + def elements: List[a] +} + +object Empty extends Tree[All] { + def insert[b >: All <% Ordered[b]](x: b): Tree[b] = new Node(x, Empty, Empty); + def elements: List[All] = List(); +} + +class Node[a <% Ordered[a]](elem: a, l: Tree[a], r: Tree[a]) extends Tree[a] { + def insert[b >: a <% Ordered[b]](x: b): Tree[b] = + if (x == elem) this + else if (x < elem) new Node(elem, l insert x, r) + else new Node(elem, l, r insert x); + def elements: List[a] = + l.elements ::: List(elem) ::: r.elements +} + +case class Str(elem: String) extends Ordered[Str] { + def compareTo[b >: Str <% Ordered[b]](that: b): int = that match { + case that1: Str => this.elem compareTo that1.elem + case _ => -(that compareTo this) + } +} + +object Test { + import O._; + + private def toCharList(s: String): List[Char] = + if (s.length() == 0) List() + else s.charAt(0) :: toCharList(s.substring(1)); + + def main(args: Array[String]) = { + { + var t: Tree[String] = Empty; + for (val s <- args) { + t = t insert s + } + System.out.println(t.elements) + } + { + var t: Tree[Str] = Empty; + for (val s <- args) { + t = t insert Str(s) + } + System.out.println(t.elements) + } + { + var t: Tree[List[char]] = Empty; + for (val s <- args) { + t = t insert toCharList(s) + } + System.out.println(t.elements) + } + } +} diff --git a/test/files/pos/imp2.scala b/test/files/pos/imp2.scala new file mode 100755 index 0000000000..5460c60015 --- /dev/null +++ b/test/files/pos/imp2.scala @@ -0,0 +1,5 @@ +object Test { + import collection.mutable._ + import collection.mutable._ + val x = new HashMap +} diff --git a/test/files/pos/viewtest2.scala b/test/files/pos/viewtest2.scala index 1958696c1f..51df563633 100644 --- a/test/files/pos/viewtest2.scala +++ b/test/files/pos/viewtest2.scala @@ -54,8 +54,6 @@ object O { } } } - - implicit def view4[a](x: a): a = x; } trait Tree[+a <% Ordered[a]] { -- cgit v1.2.3