From 0faeeba25efcae711eee75ba0bcd9682b892ee0e Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 17 Aug 2016 17:24:09 +0200 Subject: Add passing tests --- tests/pending/pos/spec-asseenfrom.scala | 29 ----- tests/pending/pos/spec-constr-new.scala | 9 -- tests/pending/pos/spec-doubledef-new.scala | 30 ----- tests/pending/pos/spec-doubledef-old.scala | 28 ----- tests/pending/pos/spec-foo.scala | 4 - tests/pending/pos/spec-groups.scala | 65 ---------- tests/pending/pos/spec-partialmap.scala | 17 --- tests/pending/pos/spec-traits.scala | 64 ---------- tests/pending/pos/tcpoly_seq.scala | 175 --------------------------- tests/pending/pos/tcpoly_seq_typealias.scala | 143 ---------------------- tests/pos/spec-asseenfrom.scala | 29 +++++ tests/pos/spec-constr-new.scala | 9 ++ tests/pos/spec-doubledef-new.scala | 30 +++++ tests/pos/spec-doubledef-old.scala | 28 +++++ tests/pos/spec-foo.scala | 4 + tests/pos/spec-groups.scala | 65 ++++++++++ tests/pos/spec-partialmap.scala | 17 +++ tests/pos/spec-traits.scala | 64 ++++++++++ tests/pos/tcpoly_seq.scala | 175 +++++++++++++++++++++++++++ tests/pos/tcpoly_seq_typealias.scala | 143 ++++++++++++++++++++++ 20 files changed, 564 insertions(+), 564 deletions(-) delete mode 100644 tests/pending/pos/spec-asseenfrom.scala delete mode 100644 tests/pending/pos/spec-constr-new.scala delete mode 100644 tests/pending/pos/spec-doubledef-new.scala delete mode 100644 tests/pending/pos/spec-doubledef-old.scala delete mode 100644 tests/pending/pos/spec-foo.scala delete mode 100644 tests/pending/pos/spec-groups.scala delete mode 100644 tests/pending/pos/spec-partialmap.scala delete mode 100644 tests/pending/pos/spec-traits.scala delete mode 100644 tests/pending/pos/tcpoly_seq.scala delete mode 100644 tests/pending/pos/tcpoly_seq_typealias.scala create mode 100644 tests/pos/spec-asseenfrom.scala create mode 100644 tests/pos/spec-constr-new.scala create mode 100644 tests/pos/spec-doubledef-new.scala create mode 100644 tests/pos/spec-doubledef-old.scala create mode 100644 tests/pos/spec-foo.scala create mode 100644 tests/pos/spec-groups.scala create mode 100644 tests/pos/spec-partialmap.scala create mode 100644 tests/pos/spec-traits.scala create mode 100644 tests/pos/tcpoly_seq.scala create mode 100644 tests/pos/tcpoly_seq_typealias.scala diff --git a/tests/pending/pos/spec-asseenfrom.scala b/tests/pending/pos/spec-asseenfrom.scala deleted file mode 100644 index ede579170..000000000 --- a/tests/pending/pos/spec-asseenfrom.scala +++ /dev/null @@ -1,29 +0,0 @@ -class Automaton[@specialized(Double) W,State] { - - def finalWeight(s: State): W = sys.error("todo"); - - def allStates: Set[State] = sys.error("toodo"); - - /** - * Returns a map from states to its final weight. may expand all nodes. - */ - def finalStateWeights = Map.empty ++ allStates.map { s => (s,finalWeight(s)) } - - // This works fine: - /* - def finalStateWeights() = { - val it = allStates.iterator; - while(it.hasNext) { - finalWeight(it.next); - } - } - */ - -} - -abstract class Automaton2[@specialized T1, T2] { - def finalWeight(s: T2): T1 - def allStates: Set[T2] - - def f = allStates map finalWeight -} diff --git a/tests/pending/pos/spec-constr-new.scala b/tests/pending/pos/spec-constr-new.scala deleted file mode 100644 index c6acc862a..000000000 --- a/tests/pending/pos/spec-constr-new.scala +++ /dev/null @@ -1,9 +0,0 @@ -import scala.reflect.{ClassTag, classTag} - -class SparseArray2[@specialized(Int) T:ClassTag](val maxSize: Int, initialLength:Int = 3) { - private var data = new Array[T](initialLength); - private var index = new Array[Int](initialLength); - - // comment out to compile correctly - data.length + 3; -} diff --git a/tests/pending/pos/spec-doubledef-new.scala b/tests/pending/pos/spec-doubledef-new.scala deleted file mode 100644 index de438d6e9..000000000 --- a/tests/pending/pos/spec-doubledef-new.scala +++ /dev/null @@ -1,30 +0,0 @@ -import scala.reflect.runtime.universe._ - -object Test { - def fn[@specialized T, @specialized U](t : T => Int, u : U => Int) : T = - null.asInstanceOf[T] -} - -trait A[@specialized(Int) T] { - var value: T - def getWith[@specialized(Int) Z](f: T => Z) = f(value) -} - -class C extends A[Int] { - var value = 10 - override def getWith[@specialized(Int) Z](f: Int => Z) = f(value) -} - -abstract class B[T, @specialized(scala.Int) U : TypeTag, @specialized(scala.Int) V <% Ordered[V]] { - val u: U - val v: V - - def f(t: T, v2: V): Tuple2[U, V] = { - val m: Array[U] = null - if (m.isEmpty) { - (u, v) - } else { - (u, v2) - } - } -} diff --git a/tests/pending/pos/spec-doubledef-old.scala b/tests/pending/pos/spec-doubledef-old.scala deleted file mode 100644 index bde259e4f..000000000 --- a/tests/pending/pos/spec-doubledef-old.scala +++ /dev/null @@ -1,28 +0,0 @@ -object Test { - def fn[@specialized T, @specialized U](t : T => Int, u : U => Int) : T = - null.asInstanceOf[T] -} - -trait A[@specialized(Int) T] { - var value: T - def getWith[@specialized(Int) Z](f: T => Z) = f(value) -} - -class C extends A[Int] { - var value = 10 - override def getWith[@specialized(Int) Z](f: Int => Z) = f(value) -} - -abstract class B[T, @specialized(scala.Int) U : Manifest, @specialized(scala.Int) V <% Ordered[V]] { - val u: U - val v: V - - def f(t: T, v2: V): Tuple2[U, V] = { - val m: Array[U] = null - if (m.isEmpty) { - (u, v) - } else { - (u, v2) - } - } -} diff --git a/tests/pending/pos/spec-foo.scala b/tests/pending/pos/spec-foo.scala deleted file mode 100644 index aabe0d51e..000000000 --- a/tests/pending/pos/spec-foo.scala +++ /dev/null @@ -1,4 +0,0 @@ -class Foo { - val xs = List(1, 2) - 1 :: xs -} diff --git a/tests/pending/pos/spec-groups.scala b/tests/pending/pos/spec-groups.scala deleted file mode 100644 index 9b6359a98..000000000 --- a/tests/pending/pos/spec-groups.scala +++ /dev/null @@ -1,65 +0,0 @@ -import Specializable._ - -class A[@specialized(Primitives) T](x: T) { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} -class B[@specialized(Everything) T] { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} -class C[@specialized(Bits32AndUp) T] { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} -class D[@specialized(Integral) T] { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} -class E[@specialized(AllNumeric) T] { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} -class F[@specialized(BestOfBreed) T] { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} -class G[@specialized(Byte, Double, AnyRef) T] { - def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) - def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) - def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) - def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) - def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) - def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) - def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) -} diff --git a/tests/pending/pos/spec-partialmap.scala b/tests/pending/pos/spec-partialmap.scala deleted file mode 100644 index 09684e024..000000000 --- a/tests/pending/pos/spec-partialmap.scala +++ /dev/null @@ -1,17 +0,0 @@ - -// ticket #3378, overloaded specialized variants -import scala.collection.{Traversable,TraversableLike}; -import scala.collection.generic.CanBuildFrom; - -trait PartialMap[@specialized A,@specialized B] -extends PartialFunction[A,B] with Iterable[(A,B)] { - - // commenting out this declaration gives a different exception. - /** Getter for all values for which the given key function returns true. */ - def apply(f : (A => Boolean)) : Iterator[B] = - for ((k,v) <- iterator; if f(k)) yield v; - - // if this is commented, it compiles fine: - def apply[This <: Traversable[A], That](keys : TraversableLike[A,This]) - (implicit bf: CanBuildFrom[This, B, That]) : That = keys.map(apply); -} diff --git a/tests/pending/pos/spec-traits.scala b/tests/pending/pos/spec-traits.scala deleted file mode 100644 index c8c8000d8..000000000 --- a/tests/pending/pos/spec-traits.scala +++ /dev/null @@ -1,64 +0,0 @@ -trait A[@specialized(Int) T] { def foo: T } -class B extends A[Int] { val foo = 10 } -class C extends B - -// issue 3309 -class Lazy { - def test[U](block: => U): Unit = { block } - - test { lazy val x = 1 } -} - -// issue 3307 -class Bug3307 { - def f[Z](block: String => Z): Unit = { - block("abc") - } - - ({ () => - f { implicit x => println(x) } })() -} - -// issue 3301 - trait T[X] - -class Bug3301 { - def t[A]: T[A] = sys.error("stub") - - () => { - type X = Int - - def foo[X] = t[X] - () - } -} -// issue 3299 -object Failure { - def thunk(): Unit = { - for (i <- 1 to 2) { - val Array(a, b) = Array(1,2) - () - } - } -} - -// issue 3296 - -object AA -{ - def f(block: => Unit): Unit = {} - - object BB - { - f { - object CC - - () - } - } - - def foo[T](x: T) = { object A; false } -} - -// issue 3325 -object O { def f[@specialized T]: Unit = { for(k <- Nil: List[T]) { } } } diff --git a/tests/pending/pos/tcpoly_seq.scala b/tests/pending/pos/tcpoly_seq.scala deleted file mode 100644 index e8711d1c4..000000000 --- a/tests/pending/pos/tcpoly_seq.scala +++ /dev/null @@ -1,175 +0,0 @@ -package examples.tcpoly.collection; - -trait HOSeq { - // an internal interface that encapsulates the accumulation of elements (of type elT) to produce - // a structure of type coll[elT] -- different kinds of collections should provide different implicit - // values implementing this interface, in order to provide more performant ways of building that structure - trait Accumulator[+coll[x], elT] { - def += (el: elT): Unit - def result: coll[elT] - } - - - // Iterable abstracts over the type of its structure as well as its elements (see PolyP's Bifunctor) - // m[x] is intentionally unbounded: fold can then be defined nicely - // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type - // is an invariant position -- should probably rule that out? - trait Iterable[+m[+x], +t] { - //def unit[a](orig: a): m[a] - def iterator: Iterator[t] - - // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t - def accumulator[t]: Accumulator[m, t] - - def filter(p: t => Boolean): m[t] = { - val buf = accumulator[t] - val elems = iterator - while (elems.hasNext) { val x = elems.next; if (p(x)) buf += x } - buf.result - } - - def map[s](f: t => s): m[s] = { - val buf = accumulator[s] - val elems = iterator - while (elems.hasNext) buf += f(elems.next) - buf.result - } - - // flatMap is a more specialized map, it only works if the mapped function produces Iterable values, - // which are then added to the result one by one - // the compiler should be able to find the right accumulator (implicit buf) to build the result - // to get concat, resColl = SingletonIterable, f = unit for SingletonIterable - def flatMap[resColl[+x] <: Iterable[resColl, x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = { - // TODO: would a viewbound for resColl[x] be better? - // -- 2nd-order type params are not yet in scope in view bound - val elems = iterator - while (elems.hasNext) { - val elemss: Iterator[s] = f(elems.next).iterator - while (elemss.hasNext) buf += elemss.next - } - buf.result - } - } - - final class ListBuffer[A] { - private var start: List[A] = Nil - private var last: ::[A] = _ - private var exported: Boolean = false - - /** Appends a single element to this buffer. - * - * @param x the element to append. - */ - def += (x: A): Unit = { - if (exported) copy - if (start.isEmpty) { - last = new HOSeq.this.:: (x, Nil) - start = last - } else { - val last1 = last - last = new HOSeq.this.:: (x, null) // hack: ::'s tail will actually be last - //last1.tl = last - } - } - - /** Converts this buffer to a list - */ - def toList: List[A] = { - exported = !start.isEmpty - start - } - - /** Clears the buffer contents. - */ - def clear: Unit = { - start = Nil - exported = false - } - - /** Copy contents of this buffer */ - private def copy: Unit = { - var cursor = start - val limit = last.tail - clear - while (cursor ne limit) { - this += cursor.head - cursor = cursor.tail - } - } - } - - implicit def listAccumulator[elT]: Accumulator[List, elT] = new Accumulator[List, elT] { - private[this] val buff = new ListBuffer[elT] - def += (el: elT): Unit = buff += el - def result: List[elT] = buff.toList - } - - trait List[+t] extends Iterable[List, t] { - def head: t - def tail: List[t] - def isEmpty: Boolean - def iterator: Iterator[t] = new Iterator[t] { - var these = List.this - def hasNext: Boolean = !these.isEmpty - def next: t = - if (!hasNext) - throw new NoSuchElementException("next on empty Iterator") - else { - val result = these.head; these = these.tail; result - } - } - // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t - def accumulator[t]: Accumulator[List, t] = listAccumulator[t] - } - - // TODO: the var tl approach does not seem to work because subtyping isn't fully working yet - final case class ::[+b](hd: b, private val tl: List[b]) extends List[b] { - def head = hd - def tail = if (tl==null) this else tl // hack - override def isEmpty: Boolean = false - } - - case object Nil extends List[Nothing] { - def isEmpty = true - def head: Nothing = - throw new NoSuchElementException("head of empty list") - def tail: List[Nothing] = - throw new NoSuchElementException("tail of empty list") - } -} - - - -// misc signatures collected from mailing list / library code: - /*override def flatMap[B](f: A => Iterable[B]): Set[B] - final override def flatMap[b](f: Any => Iterable[b]): Array[b] - def flatMap[b](f: a => Parser[b]) = new Parser[b] - override def flatMap[b](f: a => Iterable[b]): List[b] - - - MapResult[K] <: Seq[K] - FilterResult <: Seq[T] - Concat <: Seq[T] - Subseq <: Seq[T] - - - def map[K](f: T=>K): MapResult[K] - def filter(f: T=>Boolean): FilterResult - def subseq(from: Int, to: Int): Subseq - def flatMap[S <: Seq[K], K](f: T => S): S#Concat // legal? - def concat(others: Seq[T]): Concat - */ - -/*trait Iterator[t] { - // @post hasAdvanced implies hasNext - // model def hasAdvanced: Boolean - - def hasNext: Boolean // pure - - // @pre hasAdvanced - def current: t // pure - - // @pre hasNext - // @post hasAdvanced - def advance: Unit -}*/ diff --git a/tests/pending/pos/tcpoly_seq_typealias.scala b/tests/pending/pos/tcpoly_seq_typealias.scala deleted file mode 100644 index b758ecd99..000000000 --- a/tests/pending/pos/tcpoly_seq_typealias.scala +++ /dev/null @@ -1,143 +0,0 @@ -package examples.tcpoly.collection - -trait HOSeq { - // an internal interface that encapsulates the accumulation of elements (of type elT) to produce - // a structure of type coll[elT] -- different kinds of collections should provide different implicit - // values implementing this interface, in order to provide more performant ways of building that structure - trait Accumulator[+coll[x], elT] { - def += (el: elT): Unit - def result: coll[elT] - } - - - // Iterable abstracts over the type of its structure as well as its elements (see PolyP's Bifunctor) - // m[x] is intentionally unbounded: fold can then be defined nicely - // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type - // is an invariant position -- should probably rule that out? - trait Iterable[+t] { - type m[+x] - - //def unit[a](orig: a): m[a] - def iterator: Iterator[t] - - // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t - def accumulator[t]: Accumulator[m, t] - - def filter(p: t => Boolean): m[t] = { - val buf = accumulator[t] - val elems = iterator - while (elems.hasNext) { val x = elems.next; if (p(x)) buf += x } - buf.result - } - - def map[s](f: t => s): m[s] = { - val buf = accumulator[s] - val elems = iterator - while (elems.hasNext) buf += f(elems.next) - buf.result - } - - // flatMap is a more specialized map, it only works if the mapped function produces Iterable values, - // which are then added to the result one by one - // the compiler should be able to find the right accumulator (implicit buf) to build the result - // to get concat, resColl = SingletonIterable, f = unit for SingletonIterable - def flatMap[resColl[+x] <: Iterable[x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = { - // TODO: would a viewbound for resColl[x] be better? - // -- 2nd-order type params are not yet in scope in view bound - val elems = iterator - while (elems.hasNext) { - val elemss: Iterator[s] = f(elems.next).iterator - while (elemss.hasNext) buf += elemss.next - } - buf.result - } - } - - final class ListBuffer[A] { - private var start: List[A] = Nil - private var last: ::[A] = _ - private var exported: Boolean = false - - /** Appends a single element to this buffer. - * - * @param x the element to append. - */ - def += (x: A): Unit = { - if (exported) copy - if (start.isEmpty) { - last = new HOSeq.this.:: (x, Nil) - start = last - } else { - val last1 = last - last = new HOSeq.this.:: (x, null) // hack: ::'s tail will actually be last - //last1.tl = last - } - } - - /** Converts this buffer to a list - */ - def toList: List[A] = { - exported = !start.isEmpty - start - } - - /** Clears the buffer contents. - */ - def clear: Unit = { - start = Nil - exported = false - } - - /** Copy contents of this buffer */ - private def copy: Unit = { - var cursor = start - val limit = last.tail - clear - while (cursor ne limit) { - this += cursor.head - cursor = cursor.tail - } - } - } - - implicit def listAccumulator[elT]: Accumulator[List, elT] = new Accumulator[List, elT] { - private[this] val buff = new ListBuffer[elT] - def += (el: elT): Unit = buff += el - def result: List[elT] = buff.toList - } - - trait List[+t] extends Iterable[t] { - type m[+x] = List[x] - - def head: t - def tail: List[t] - def isEmpty: Boolean - def iterator: Iterator[t] = new Iterator[t] { - var these = List.this - def hasNext: Boolean = !these.isEmpty - def next: t = - if (!hasNext) - throw new NoSuchElementException("next on empty Iterator") - else { - val result = these.head; these = these.tail; result - } - } - // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t - def accumulator[t]: Accumulator[List, t] = listAccumulator[t] - } - - // TODO: the var tl approach does not seem to work because subtyping isn't fully working yet - final case class ::[+b](hd: b, private val tl: List[b]) extends List[b] { - def head = hd - def tail = if (tl==null) this else tl // hack - override def isEmpty: Boolean = false - } - - case object Nil extends List[Nothing] { - def isEmpty = true - def head: Nothing = - throw new NoSuchElementException("head of empty list") - def tail: List[Nothing] = - throw new NoSuchElementException("tail of empty list") - } -} diff --git a/tests/pos/spec-asseenfrom.scala b/tests/pos/spec-asseenfrom.scala new file mode 100644 index 000000000..ede579170 --- /dev/null +++ b/tests/pos/spec-asseenfrom.scala @@ -0,0 +1,29 @@ +class Automaton[@specialized(Double) W,State] { + + def finalWeight(s: State): W = sys.error("todo"); + + def allStates: Set[State] = sys.error("toodo"); + + /** + * Returns a map from states to its final weight. may expand all nodes. + */ + def finalStateWeights = Map.empty ++ allStates.map { s => (s,finalWeight(s)) } + + // This works fine: + /* + def finalStateWeights() = { + val it = allStates.iterator; + while(it.hasNext) { + finalWeight(it.next); + } + } + */ + +} + +abstract class Automaton2[@specialized T1, T2] { + def finalWeight(s: T2): T1 + def allStates: Set[T2] + + def f = allStates map finalWeight +} diff --git a/tests/pos/spec-constr-new.scala b/tests/pos/spec-constr-new.scala new file mode 100644 index 000000000..c6acc862a --- /dev/null +++ b/tests/pos/spec-constr-new.scala @@ -0,0 +1,9 @@ +import scala.reflect.{ClassTag, classTag} + +class SparseArray2[@specialized(Int) T:ClassTag](val maxSize: Int, initialLength:Int = 3) { + private var data = new Array[T](initialLength); + private var index = new Array[Int](initialLength); + + // comment out to compile correctly + data.length + 3; +} diff --git a/tests/pos/spec-doubledef-new.scala b/tests/pos/spec-doubledef-new.scala new file mode 100644 index 000000000..de438d6e9 --- /dev/null +++ b/tests/pos/spec-doubledef-new.scala @@ -0,0 +1,30 @@ +import scala.reflect.runtime.universe._ + +object Test { + def fn[@specialized T, @specialized U](t : T => Int, u : U => Int) : T = + null.asInstanceOf[T] +} + +trait A[@specialized(Int) T] { + var value: T + def getWith[@specialized(Int) Z](f: T => Z) = f(value) +} + +class C extends A[Int] { + var value = 10 + override def getWith[@specialized(Int) Z](f: Int => Z) = f(value) +} + +abstract class B[T, @specialized(scala.Int) U : TypeTag, @specialized(scala.Int) V <% Ordered[V]] { + val u: U + val v: V + + def f(t: T, v2: V): Tuple2[U, V] = { + val m: Array[U] = null + if (m.isEmpty) { + (u, v) + } else { + (u, v2) + } + } +} diff --git a/tests/pos/spec-doubledef-old.scala b/tests/pos/spec-doubledef-old.scala new file mode 100644 index 000000000..bde259e4f --- /dev/null +++ b/tests/pos/spec-doubledef-old.scala @@ -0,0 +1,28 @@ +object Test { + def fn[@specialized T, @specialized U](t : T => Int, u : U => Int) : T = + null.asInstanceOf[T] +} + +trait A[@specialized(Int) T] { + var value: T + def getWith[@specialized(Int) Z](f: T => Z) = f(value) +} + +class C extends A[Int] { + var value = 10 + override def getWith[@specialized(Int) Z](f: Int => Z) = f(value) +} + +abstract class B[T, @specialized(scala.Int) U : Manifest, @specialized(scala.Int) V <% Ordered[V]] { + val u: U + val v: V + + def f(t: T, v2: V): Tuple2[U, V] = { + val m: Array[U] = null + if (m.isEmpty) { + (u, v) + } else { + (u, v2) + } + } +} diff --git a/tests/pos/spec-foo.scala b/tests/pos/spec-foo.scala new file mode 100644 index 000000000..aabe0d51e --- /dev/null +++ b/tests/pos/spec-foo.scala @@ -0,0 +1,4 @@ +class Foo { + val xs = List(1, 2) + 1 :: xs +} diff --git a/tests/pos/spec-groups.scala b/tests/pos/spec-groups.scala new file mode 100644 index 000000000..9b6359a98 --- /dev/null +++ b/tests/pos/spec-groups.scala @@ -0,0 +1,65 @@ +import Specializable._ + +class A[@specialized(Primitives) T](x: T) { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} +class B[@specialized(Everything) T] { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} +class C[@specialized(Bits32AndUp) T] { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} +class D[@specialized(Integral) T] { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} +class E[@specialized(AllNumeric) T] { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} +class F[@specialized(BestOfBreed) T] { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} +class G[@specialized(Byte, Double, AnyRef) T] { + def f1[@specialized(Primitives) U](x: T, y: U) = ((x, y)) + def f2[@specialized(Everything) U](x: T, y: U) = ((x, y)) + def f3[@specialized(Bits32AndUp) U](x: T, y: U) = ((x, y)) + def f4[@specialized(Integral) U](x: T, y: U) = ((x, y)) + def f5[@specialized(AllNumeric) U](x: T, y: U) = ((x, y)) + def f6[@specialized(BestOfBreed) U](x: T, y: U) = ((x, y)) + def f7[@specialized(Byte, Double, AnyRef) U](x: T, y: U) = ((x, y)) +} diff --git a/tests/pos/spec-partialmap.scala b/tests/pos/spec-partialmap.scala new file mode 100644 index 000000000..09684e024 --- /dev/null +++ b/tests/pos/spec-partialmap.scala @@ -0,0 +1,17 @@ + +// ticket #3378, overloaded specialized variants +import scala.collection.{Traversable,TraversableLike}; +import scala.collection.generic.CanBuildFrom; + +trait PartialMap[@specialized A,@specialized B] +extends PartialFunction[A,B] with Iterable[(A,B)] { + + // commenting out this declaration gives a different exception. + /** Getter for all values for which the given key function returns true. */ + def apply(f : (A => Boolean)) : Iterator[B] = + for ((k,v) <- iterator; if f(k)) yield v; + + // if this is commented, it compiles fine: + def apply[This <: Traversable[A], That](keys : TraversableLike[A,This]) + (implicit bf: CanBuildFrom[This, B, That]) : That = keys.map(apply); +} diff --git a/tests/pos/spec-traits.scala b/tests/pos/spec-traits.scala new file mode 100644 index 000000000..759fd2267 --- /dev/null +++ b/tests/pos/spec-traits.scala @@ -0,0 +1,64 @@ +trait A[@specialized(Int) T] { def foo: T } +class B extends A[Int] { val foo = 10 } +class C extends B + +// issue 3309 +class Lazy { + def test[U](block: => U): Unit = { block } + + test { lazy val x = 1 } +} + +// issue 3307 +class Bug3307 { + def f[Z](block: String => Z): Unit = { + block("abc") + } + + ({ () => + f { implicit x => println(x) } })() +} + +// issue 3301 + trait T[X] + +class Bug3301 { + def t[A]: T[A] = sys.error("stub") + + {() => { + type X = Int + + def foo[X] = t[X] + () + }} +} +// issue 3299 +object Failure { + def thunk(): Unit = { + for (i <- 1 to 2) { + val Array(a, b) = Array(1,2) + () + } + } +} + +// issue 3296 + +object AA +{ + def f(block: => Unit): Unit = {} + + object BB + { + f { + object CC + + () + } + } + + def foo[T](x: T) = { object A; false } +} + +// issue 3325 +object O { def f[@specialized T]: Unit = { for(k <- Nil: List[T]) { } } } diff --git a/tests/pos/tcpoly_seq.scala b/tests/pos/tcpoly_seq.scala new file mode 100644 index 000000000..e8711d1c4 --- /dev/null +++ b/tests/pos/tcpoly_seq.scala @@ -0,0 +1,175 @@ +package examples.tcpoly.collection; + +trait HOSeq { + // an internal interface that encapsulates the accumulation of elements (of type elT) to produce + // a structure of type coll[elT] -- different kinds of collections should provide different implicit + // values implementing this interface, in order to provide more performant ways of building that structure + trait Accumulator[+coll[x], elT] { + def += (el: elT): Unit + def result: coll[elT] + } + + + // Iterable abstracts over the type of its structure as well as its elements (see PolyP's Bifunctor) + // m[x] is intentionally unbounded: fold can then be defined nicely + // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type + // is an invariant position -- should probably rule that out? + trait Iterable[+m[+x], +t] { + //def unit[a](orig: a): m[a] + def iterator: Iterator[t] + + // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t + def accumulator[t]: Accumulator[m, t] + + def filter(p: t => Boolean): m[t] = { + val buf = accumulator[t] + val elems = iterator + while (elems.hasNext) { val x = elems.next; if (p(x)) buf += x } + buf.result + } + + def map[s](f: t => s): m[s] = { + val buf = accumulator[s] + val elems = iterator + while (elems.hasNext) buf += f(elems.next) + buf.result + } + + // flatMap is a more specialized map, it only works if the mapped function produces Iterable values, + // which are then added to the result one by one + // the compiler should be able to find the right accumulator (implicit buf) to build the result + // to get concat, resColl = SingletonIterable, f = unit for SingletonIterable + def flatMap[resColl[+x] <: Iterable[resColl, x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = { + // TODO: would a viewbound for resColl[x] be better? + // -- 2nd-order type params are not yet in scope in view bound + val elems = iterator + while (elems.hasNext) { + val elemss: Iterator[s] = f(elems.next).iterator + while (elemss.hasNext) buf += elemss.next + } + buf.result + } + } + + final class ListBuffer[A] { + private var start: List[A] = Nil + private var last: ::[A] = _ + private var exported: Boolean = false + + /** Appends a single element to this buffer. + * + * @param x the element to append. + */ + def += (x: A): Unit = { + if (exported) copy + if (start.isEmpty) { + last = new HOSeq.this.:: (x, Nil) + start = last + } else { + val last1 = last + last = new HOSeq.this.:: (x, null) // hack: ::'s tail will actually be last + //last1.tl = last + } + } + + /** Converts this buffer to a list + */ + def toList: List[A] = { + exported = !start.isEmpty + start + } + + /** Clears the buffer contents. + */ + def clear: Unit = { + start = Nil + exported = false + } + + /** Copy contents of this buffer */ + private def copy: Unit = { + var cursor = start + val limit = last.tail + clear + while (cursor ne limit) { + this += cursor.head + cursor = cursor.tail + } + } + } + + implicit def listAccumulator[elT]: Accumulator[List, elT] = new Accumulator[List, elT] { + private[this] val buff = new ListBuffer[elT] + def += (el: elT): Unit = buff += el + def result: List[elT] = buff.toList + } + + trait List[+t] extends Iterable[List, t] { + def head: t + def tail: List[t] + def isEmpty: Boolean + def iterator: Iterator[t] = new Iterator[t] { + var these = List.this + def hasNext: Boolean = !these.isEmpty + def next: t = + if (!hasNext) + throw new NoSuchElementException("next on empty Iterator") + else { + val result = these.head; these = these.tail; result + } + } + // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t + def accumulator[t]: Accumulator[List, t] = listAccumulator[t] + } + + // TODO: the var tl approach does not seem to work because subtyping isn't fully working yet + final case class ::[+b](hd: b, private val tl: List[b]) extends List[b] { + def head = hd + def tail = if (tl==null) this else tl // hack + override def isEmpty: Boolean = false + } + + case object Nil extends List[Nothing] { + def isEmpty = true + def head: Nothing = + throw new NoSuchElementException("head of empty list") + def tail: List[Nothing] = + throw new NoSuchElementException("tail of empty list") + } +} + + + +// misc signatures collected from mailing list / library code: + /*override def flatMap[B](f: A => Iterable[B]): Set[B] + final override def flatMap[b](f: Any => Iterable[b]): Array[b] + def flatMap[b](f: a => Parser[b]) = new Parser[b] + override def flatMap[b](f: a => Iterable[b]): List[b] + + + MapResult[K] <: Seq[K] + FilterResult <: Seq[T] + Concat <: Seq[T] + Subseq <: Seq[T] + + + def map[K](f: T=>K): MapResult[K] + def filter(f: T=>Boolean): FilterResult + def subseq(from: Int, to: Int): Subseq + def flatMap[S <: Seq[K], K](f: T => S): S#Concat // legal? + def concat(others: Seq[T]): Concat + */ + +/*trait Iterator[t] { + // @post hasAdvanced implies hasNext + // model def hasAdvanced: Boolean + + def hasNext: Boolean // pure + + // @pre hasAdvanced + def current: t // pure + + // @pre hasNext + // @post hasAdvanced + def advance: Unit +}*/ diff --git a/tests/pos/tcpoly_seq_typealias.scala b/tests/pos/tcpoly_seq_typealias.scala new file mode 100644 index 000000000..b758ecd99 --- /dev/null +++ b/tests/pos/tcpoly_seq_typealias.scala @@ -0,0 +1,143 @@ +package examples.tcpoly.collection + +trait HOSeq { + // an internal interface that encapsulates the accumulation of elements (of type elT) to produce + // a structure of type coll[elT] -- different kinds of collections should provide different implicit + // values implementing this interface, in order to provide more performant ways of building that structure + trait Accumulator[+coll[x], elT] { + def += (el: elT): Unit + def result: coll[elT] + } + + + // Iterable abstracts over the type of its structure as well as its elements (see PolyP's Bifunctor) + // m[x] is intentionally unbounded: fold can then be defined nicely + // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type + // is an invariant position -- should probably rule that out? + trait Iterable[+t] { + type m[+x] + + //def unit[a](orig: a): m[a] + def iterator: Iterator[t] + + // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t + def accumulator[t]: Accumulator[m, t] + + def filter(p: t => Boolean): m[t] = { + val buf = accumulator[t] + val elems = iterator + while (elems.hasNext) { val x = elems.next; if (p(x)) buf += x } + buf.result + } + + def map[s](f: t => s): m[s] = { + val buf = accumulator[s] + val elems = iterator + while (elems.hasNext) buf += f(elems.next) + buf.result + } + + // flatMap is a more specialized map, it only works if the mapped function produces Iterable values, + // which are then added to the result one by one + // the compiler should be able to find the right accumulator (implicit buf) to build the result + // to get concat, resColl = SingletonIterable, f = unit for SingletonIterable + def flatMap[resColl[+x] <: Iterable[x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = { + // TODO: would a viewbound for resColl[x] be better? + // -- 2nd-order type params are not yet in scope in view bound + val elems = iterator + while (elems.hasNext) { + val elemss: Iterator[s] = f(elems.next).iterator + while (elemss.hasNext) buf += elemss.next + } + buf.result + } + } + + final class ListBuffer[A] { + private var start: List[A] = Nil + private var last: ::[A] = _ + private var exported: Boolean = false + + /** Appends a single element to this buffer. + * + * @param x the element to append. + */ + def += (x: A): Unit = { + if (exported) copy + if (start.isEmpty) { + last = new HOSeq.this.:: (x, Nil) + start = last + } else { + val last1 = last + last = new HOSeq.this.:: (x, null) // hack: ::'s tail will actually be last + //last1.tl = last + } + } + + /** Converts this buffer to a list + */ + def toList: List[A] = { + exported = !start.isEmpty + start + } + + /** Clears the buffer contents. + */ + def clear: Unit = { + start = Nil + exported = false + } + + /** Copy contents of this buffer */ + private def copy: Unit = { + var cursor = start + val limit = last.tail + clear + while (cursor ne limit) { + this += cursor.head + cursor = cursor.tail + } + } + } + + implicit def listAccumulator[elT]: Accumulator[List, elT] = new Accumulator[List, elT] { + private[this] val buff = new ListBuffer[elT] + def += (el: elT): Unit = buff += el + def result: List[elT] = buff.toList + } + + trait List[+t] extends Iterable[t] { + type m[+x] = List[x] + + def head: t + def tail: List[t] + def isEmpty: Boolean + def iterator: Iterator[t] = new Iterator[t] { + var these = List.this + def hasNext: Boolean = !these.isEmpty + def next: t = + if (!hasNext) + throw new NoSuchElementException("next on empty Iterator") + else { + val result = these.head; these = these.tail; result + } + } + // construct an empty accumulator that will produce the same structure as this iterable, with elements of type t + def accumulator[t]: Accumulator[List, t] = listAccumulator[t] + } + + // TODO: the var tl approach does not seem to work because subtyping isn't fully working yet + final case class ::[+b](hd: b, private val tl: List[b]) extends List[b] { + def head = hd + def tail = if (tl==null) this else tl // hack + override def isEmpty: Boolean = false + } + + case object Nil extends List[Nothing] { + def isEmpty = true + def head: Nothing = + throw new NoSuchElementException("head of empty list") + def tail: List[Nothing] = + throw new NoSuchElementException("tail of empty list") + } +} -- cgit v1.2.3