diff options
22 files changed, 320 insertions, 103 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/TreeInfo.scala b/src/compiler/scala/tools/nsc/ast/TreeInfo.scala index 3c77a9e4d3..f349786712 100644 --- a/src/compiler/scala/tools/nsc/ast/TreeInfo.scala +++ b/src/compiler/scala/tools/nsc/ast/TreeInfo.scala @@ -85,6 +85,13 @@ abstract class TreeInfo { tree.symbol.isStable && isPureExpr(qual) case TypeApply(fn, _) => isPureExpr(fn) + case Apply(fn, List()) => + /* Note: After uncurry, field accesses are represented as Apply(getter, Nil), + * so an Apply can also be pure. + * However, before typing, applications of nullary functional values are also + * Apply(function, Nil) trees. To prevent them from being treated as pure, + * we check that the callee is a method. */ + fn.symbol.isMethod && isPureExpr(fn) case Typed(expr, _) => isPureExpr(expr) case Block(stats, expr) => diff --git a/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala b/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala index 92ba50e88e..174a1b778e 100644 --- a/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala +++ b/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala @@ -851,6 +851,10 @@ abstract class GenMSIL extends SubComponent { log("Generating code for block: " + block) for (handler <- beginCatchBlock.get(block)) { + if (!currentHandlers.isEmpty && currentHandlers.top.covered == handler.covered) { + currentHandlers.pop() + currentHandlers.push(handler) + } if (handler.cls == NoSymbol) { // `finally` blocks are represented the same as `catch`, but with no catch-type mcode.BeginFinallyBlock() diff --git a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala index 8e6a519986..2cfbc928a9 100644 --- a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala +++ b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala @@ -509,7 +509,15 @@ trait NamesDefaults { self: Analyzer => def companionModuleOf(clazz: Symbol, context: Context) = { var res = clazz.companionModule if (res == NoSymbol) - res = context.lookup(clazz.name.toTermName, clazz.owner) + res = context.lookup(clazz.name.toTermName, clazz.owner).suchThat(sym => + sym.hasFlag(MODULE) && sym.isCoDefinedWith(clazz)) + res + } + + def companionClassOf(module: Symbol, context: Context) = { + var res = module.companionClass + if (res == NoSymbol) + res = context.lookup(module.name.toTypeName, module.owner).suchThat(_.isCoDefinedWith(module)) res } } diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 87a11497fd..4a4a7e699a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1030,7 +1030,7 @@ abstract class RefChecks extends InfoTransform { if ((pname startsWith nme.CHECK_IF_REFUTABLE_STRING) && isIrrefutable(pat1, tpt.tpe) && (qual.tpe <:< tree.tpe)) => - qual + transform(qual) case Apply(Select(New(tpt), name), args) if (tpt.tpe.typeSymbol == ArrayClass && args.length >= 2) => diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index 44930ffc81..0da00f5b83 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -290,15 +290,18 @@ trait SyntheticMethods extends ast.TreeDSL { } if (clazz.isModuleClass) { - if (!hasSerializableAnnotation(clazz)) - clazz addAnnotation AnnotationInfo(SerializableAttr.tpe, Nil, Nil) + if (!hasSerializableAnnotation(clazz)) { + val comp = companionClassOf(clazz, context) + if (comp.hasFlag(Flags.CASE) || hasSerializableAnnotation(comp)) + clazz addAnnotation AnnotationInfo(SerializableAttr.tpe, Nil, Nil) + } /** If you serialize a singleton and then deserialize it twice, * you will have two instances of your singleton, unless you implement * the readResolve() method (see http://www.javaworld.com/javaworld/ * jw-04-2003/jw-0425-designpatterns_p.html) */ - if (!hasImplementation(nme.readResolve)) + if (hasSerializableAnnotation(clazz) && !hasImplementation(nme.readResolve)) ts += readResolveMethod } } catch { diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index 9712990d73..8327b93d66 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -16,7 +16,7 @@ import compat.Platform.arraycopy import scala.reflect.ClassManifest import scala.runtime.ScalaRunTime.{array_apply, array_update} -/** A class containing a fall back builder for arrays where the element type +/** Contains a fallback builder for arrays when the element type * does not have a class manifest. In that case a generic array is built. */ class FallbackArrayBuilding { @@ -35,7 +35,7 @@ class FallbackArrayBuilding { } } -/** This object contains utility methods operating on arrays. +/** Utility methods for operating on arrays. * * @author Martin Odersky * @version 1.0 @@ -47,6 +47,9 @@ object Array extends FallbackArrayBuilding { def apply() = ArrayBuilder.make[T]()(m) } + /** + * Returns a new [[scala.collection.mutable.ArrayBuilder]]. + */ def newBuilder[T](implicit m: ClassManifest[T]): ArrayBuilder[T] = ArrayBuilder.make[T]()(m) private def slowcopy(src : AnyRef, @@ -65,15 +68,19 @@ object Array extends FallbackArrayBuilding { } /** Copy one array to another. - * Equivalent to - * <code>System.arraycopy(src, srcPos, dest, destPos, length)</code>, - * except that this works also for polymorphic and boxed arrays. + * Equivalent to Java's + * `System.arraycopy(src, srcPos, dest, destPos, length)`, + * except that this also works for polymorphic and boxed arrays. + * + * Note that the passed-in `dest` array will be modified by this call. * - * @param src ... - * @param srcPos ... - * @param dest ... - * @param destPos ... - * @param length ... + * @param src the source array. + * @param srcPos starting position in the source array. + * @param dest destination array. + * @param destPos starting position in the destination array. + * @param length the number of array elements to be copied. + * + * @see `java.lang.System#arraycopy` */ def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int) { val srcClass = src.getClass @@ -83,13 +90,13 @@ object Array extends FallbackArrayBuilding { slowcopy(src, srcPos, dest, destPos, length) } - /** Returns array of length 0 */ + /** Returns an array of length 0 */ def empty[T: ClassManifest]: Array[T] = new Array[T](0) - /** Create an array with given elements. + /** Creates an array with given elements. * * @param xs the elements to put in the array - * @return the array containing elements xs. + * @return an array containing all elements from xs. */ def apply[T: ClassManifest](xs: T*): Array[T] = { val array = new Array[T](xs.length) @@ -98,6 +105,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Boolean` objects */ def apply(x: Boolean, xs: Boolean*): Array[Boolean] = { val array = new Array[Boolean](xs.length + 1) array(0) = x @@ -106,6 +114,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Byte` objects */ def apply(x: Byte, xs: Byte*): Array[Byte] = { val array = new Array[Byte](xs.length + 1) array(0) = x @@ -114,6 +123,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Short` objects */ def apply(x: Short, xs: Short*): Array[Short] = { val array = new Array[Short](xs.length + 1) array(0) = x @@ -122,6 +132,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Char` objects */ def apply(x: Char, xs: Char*): Array[Char] = { val array = new Array[Char](xs.length + 1) array(0) = x @@ -130,6 +141,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Int` objects */ def apply(x: Int, xs: Int*): Array[Int] = { val array = new Array[Int](xs.length + 1) array(0) = x @@ -138,6 +150,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Long` objects */ def apply(x: Long, xs: Long*): Array[Long] = { val array = new Array[Long](xs.length + 1) array(0) = x @@ -146,6 +159,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Float` objects */ def apply(x: Float, xs: Float*): Array[Float] = { val array = new Array[Float](xs.length + 1) array(0) = x @@ -154,6 +168,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Double` objects */ def apply(x: Double, xs: Double*): Array[Double] = { val array = new Array[Double](xs.length + 1) array(0) = x @@ -162,6 +177,7 @@ object Array extends FallbackArrayBuilding { array } + /** Creates an array of `Unit` objects */ def apply(x: Unit, xs: Unit*): Array[Unit] = { val array = new Array[Unit](xs.length + 1) array(0) = x @@ -170,26 +186,30 @@ object Array extends FallbackArrayBuilding { array } - /** Create array with given dimensions */ + /** Creates array with given dimensions */ def ofDim[T: ClassManifest](n1: Int): Array[T] = new Array[T](n1) + /** Creates a 2-dimensional array */ def ofDim[T: ClassManifest](n1: Int, n2: Int): Array[Array[T]] = { val arr: Array[Array[T]] = (new Array[Array[T]](n1): Array[Array[T]]) for (i <- 0 until n1) arr(i) = new Array[T](n2) arr // tabulate(n1)(_ => ofDim[T](n2)) } + /** Creates a 3-dimensional array */ def ofDim[T: ClassManifest](n1: Int, n2: Int, n3: Int): Array[Array[Array[T]]] = tabulate(n1)(_ => ofDim[T](n2, n3)) + /** Creates a 4-dimensional array */ def ofDim[T: ClassManifest](n1: Int, n2: Int, n3: Int, n4: Int): Array[Array[Array[Array[T]]]] = tabulate(n1)(_ => ofDim[T](n2, n3, n4)) + /** Creates a 5-dimensional array */ def ofDim[T: ClassManifest](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int): Array[Array[Array[Array[Array[T]]]]] = tabulate(n1)(_ => ofDim[T](n2, n3, n4, n5)) - /** Concatenate all argument sequences into a single array. + /** Concatenates all arrays into a single array. * - * @param xs the given argument sequences - * @return the array created from the concatenated arguments + * @param xss the given arrays + * @return the array created from concatenating `xss` */ def concat[T: ClassManifest](xss: Array[T]*): Array[T] = { val b = newBuilder[T] @@ -198,11 +218,19 @@ object Array extends FallbackArrayBuilding { b.result } - /** An array that contains the results of some element computation a number + /** Returns an array that contains the results of some element computation a number * of times. * - * @param n the number of elements returned + * Note that this means that `elem` is computed a total of n times: + * {{{ + * scala> Array.fill(3){ java.lang.Math.random } + * res3: Array[Double] = Array(0.365461167592537, 1.550395944913685E-4, 0.7907242137333306) + * }}} + * + * @param n the number of elements desired * @param elem the element computation + * @return an Array of size n, where each element contains the result of computing + * `elem`. */ def fill[T: ClassManifest](n: Int)(elem: => T): Array[T] = { val b = newBuilder[T] @@ -215,7 +243,7 @@ object Array extends FallbackArrayBuilding { b.result } - /** A two-dimensional array that contains the results of some element + /** Returns a two-dimensional array that contains the results of some element * computation a number of times. * * @param n1 the number of elements in the 1st dimension @@ -225,7 +253,7 @@ object Array extends FallbackArrayBuilding { def fill[T: ClassManifest](n1: Int, n2: Int)(elem: => T): Array[Array[T]] = tabulate(n1)(_ => fill(n2)(elem)) - /** A three-dimensional array that contains the results of some element + /** Returns a three-dimensional array that contains the results of some element * computation a number of times. * * @param n1 the number of elements in the 1st dimension @@ -236,7 +264,7 @@ object Array extends FallbackArrayBuilding { def fill[T: ClassManifest](n1: Int, n2: Int, n3: Int)(elem: => T): Array[Array[Array[T]]] = tabulate(n1)(_ => fill(n2, n3)(elem)) - /** A four-dimensional array that contains the results of some element + /** Returns a four-dimensional array that contains the results of some element * computation a number of times. * * @param n1 the number of elements in the 1st dimension @@ -248,7 +276,7 @@ object Array extends FallbackArrayBuilding { def fill[T: ClassManifest](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => T): Array[Array[Array[Array[T]]]] = tabulate(n1)(_ => fill(n2, n3, n4)(elem)) - /** A five-dimensional array that contains the results of some element + /** Returns a five-dimensional array that contains the results of some element * computation a number of times. * * @param n1 the number of elements in the 1st dimension @@ -261,12 +289,12 @@ object Array extends FallbackArrayBuilding { def fill[T: ClassManifest](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => T): Array[Array[Array[Array[Array[T]]]]] = tabulate(n1)(_ => fill(n2, n3, n4, n5)(elem)) - /** An array containing values of a given function over a range of integer + /** Returns an array containing values of a given function over a range of integer * values starting from 0. * - * @param n The number of elements in the traversable + * @param n The number of elements in the array * @param f The function computing element values - * @return A traversable consisting of elements `f(0), ..., f(n -1)` + * @return A traversable consisting of elements `f(0),f(1), ..., f(n - 1)` */ def tabulate[T: ClassManifest](n: Int)(f: Int => T): Array[T] = { val b = newBuilder[T] @@ -279,7 +307,7 @@ object Array extends FallbackArrayBuilding { b.result } - /** A two-dimensional array containing values of a given function over + /** Returns a two-dimensional array containing values of a given function over * ranges of integer values starting from 0. * * @param n1 the number of elements in the 1st dimension @@ -289,7 +317,7 @@ object Array extends FallbackArrayBuilding { def tabulate[T: ClassManifest](n1: Int, n2: Int)(f: (Int, Int) => T): Array[Array[T]] = tabulate(n1)(i1 => tabulate(n2)(f(i1, _))) - /** A three-dimensional array containing values of a given function over + /** Returns a three-dimensional array containing values of a given function over * ranges of integer values starting from 0. * * @param n1 the number of elements in the 1st dimension @@ -300,7 +328,7 @@ object Array extends FallbackArrayBuilding { def tabulate[T: ClassManifest](n1: Int, n2: Int, n3: Int)(f: (Int, Int, Int) => T): Array[Array[Array[T]]] = tabulate(n1)(i1 => tabulate(n2, n3)(f(i1, _, _))) - /** A four-dimensional array containing values of a given function over + /** Returns a four-dimensional array containing values of a given function over * ranges of integer values starting from 0. * * @param n1 the number of elements in the 1st dimension @@ -312,7 +340,7 @@ object Array extends FallbackArrayBuilding { def tabulate[T: ClassManifest](n1: Int, n2: Int, n3: Int, n4: Int)(f: (Int, Int, Int, Int) => T): Array[Array[Array[Array[T]]]] = tabulate(n1)(i1 => tabulate(n2, n3, n4)(f(i1, _, _, _))) - /** A five-dimensional array containing values of a given function over + /** Returns a five-dimensional array containing values of a given function over * ranges of integer values starting from 0. * * @param n1 the number of elements in the 1st dimension @@ -325,20 +353,20 @@ object Array extends FallbackArrayBuilding { def tabulate[T: ClassManifest](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(f: (Int, Int, Int, Int, Int) => T): Array[Array[Array[Array[Array[T]]]]] = tabulate(n1)(i1 => tabulate(n2, n3, n4, n5)(f(i1, _, _, _, _))) - /** An array containing a sequence of increasing integers in a range. + /** Returns an array containing a sequence of increasing integers in a range. * * @param from the start value of the array - * @param end the end value of the array (the first value NOT returned) + * @param end the end value of the array, exclusive (in other words, this is the first value '''not''' returned) * @return the array with values in range `start, start + 1, ..., end - 1` - * up to, but exclusding, `end`. + * up to, but excluding, `end`. */ def range(start: Int, end: Int): Array[Int] = range(start, end, 1) - /** An array containing equally spaced values in some integer interval. + /** Returns an array containing equally spaced values in some integer interval. * * @param start the start value of the array - * @param end the end value of the array (the first value NOT returned) - * @param step the increment value of the array (must be positive or negative) + * @param end the end value of the array, exclusive (in other words, this is the first value '''not''' returned) + * @param step the increment value of the array (may not be zero) * @return the array with values in `start, start + step, ...` up to, but excluding `end` */ def range(start: Int, end: Int, step: Int): Array[Int] = { @@ -354,11 +382,11 @@ object Array extends FallbackArrayBuilding { b.result } - /** An array containing repeated applications of a function to a start value. + /** Returns an array containing repeated applications of a function to a start value. * * @param start the start value of the array * @param len the number of elements returned by the array - * @param f the function that's repeatedly applied + * @param f the function that is repeatedly applied * @return the array returning `len` values in the sequence `start, f(start), f(f(start)), ...` */ def iterate[T: ClassManifest](start: T, len: Int)(f: T => T): Array[T] = { @@ -374,17 +402,17 @@ object Array extends FallbackArrayBuilding { b.result } - /** This method is called in a pattern match { case Seq(...) => }. + /** Called in a pattern match like `{ case Array(x,y,z) => println('3 elements')}`. * * @param x the selector value - * @return sequence wrapped in an option, if this is a Seq, otherwise none + * @return sequence wrapped in a [[scala.Some]], if x is a Seq, otherwise `None` */ def unapplySeq[T](x: Array[T]): Option[IndexedSeq[T]] = if (x == null) None else Some(x.toIndexedSeq) // !!! the null check should to be necessary, but without it 2241 fails. Seems to be a bug // in pattern matcher. - /** Create an array containing several copies of an element. + /** Creates an array containing several copies of an element. * * @param n the length of the resulting array * @param elem the element composing the resulting array @@ -401,8 +429,8 @@ object Array extends FallbackArrayBuilding { a } - /** Create an array containing the values of a given function <code>f</code> - * over given range <code>[0..n)</code> + /** Creates an array containing the values of a given function `f` + * over given range `[0..n)` */ @deprecated("use `Array.tabulate' instead") def fromFunction[T: ClassManifest](f: Int => T)(n: Int): Array[T] = { @@ -415,37 +443,37 @@ object Array extends FallbackArrayBuilding { a } - /** Create an array containing the values of a given function <code>f</code> - * over given range <code>[0..n1, 0..n2)</code> + /** Creates an array containing the values of a given function `f` + * over given range `[0..n1, 0..n2)` */ @deprecated("use `Array.tabulate' instead") def fromFunction[T: ClassManifest](f: (Int, Int) => T)(n1: Int, n2: Int): Array[Array[T]] = fromFunction(i => fromFunction(f(i, _))(n2))(n1) - /** Create an array containing the values of a given function <code>f</code> - * over given range <code>[0..n1, 0..n2, 0..n3)</code> + /** Creates an array containing the values of a given function `f` + * over given range `[0..n1, 0..n2, 0..n3)` */ @deprecated("use `Array.tabulate' instead") def fromFunction[T: ClassManifest](f: (Int, Int, Int) => T)(n1: Int, n2: Int, n3: Int): Array[Array[Array[T]]] = fromFunction(i => fromFunction(f(i, _, _))(n2, n3))(n1) - /** Create an array containing the values of a given function <code>f</code> - * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4)</code> + /** Creates an array containing the values of a given function `f` + * over given range `[0..n1, 0..n2, 0..n3, 0..n4)` */ @deprecated("use `Array.tabulate' instead") def fromFunction[T: ClassManifest](f: (Int, Int, Int, Int) => T)(n1: Int, n2: Int, n3: Int, n4: Int): Array[Array[Array[Array[T]]]] = fromFunction(i => fromFunction(f(i, _, _, _))(n2, n3, n4))(n1) - /** Create an array containing the values of a given function <code>f</code> - * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4, 0..n5)</code> + /** Creates an array containing the values of a given function `f` + * over given range `[0..n1, 0..n2, 0..n3, 0..n4, 0..n5)` */ @deprecated("use `Array.tabulate' instead") def fromFunction[T: ClassManifest](f: (Int, Int, Int, Int, Int) => T)(n1: Int, n2: Int, n3: Int, n4: Int, n5: Int): Array[Array[Array[Array[Array[T]]]]] = fromFunction(i => fromFunction(f(i, _, _, _, _))(n2, n3, n4, n5))(n1) } -/** This class represents polymorphic arrays. <code>Array[T]</code> is Scala's representation - * for Java's <code>T[]</code>. +/** Represents polymorphic arrays. `Array[T]` is Scala's representation + * for Java's `T[]`. * * @author Martin Odersky * @version 1.0 @@ -513,17 +541,17 @@ final class Array[T](_length: Int) { /** The element at given index. * <p> - * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first - * element of array <code>xs</code>. + * Indices start a `0`; `xs.apply(0)` is the first + * element of array `xs`. * </p> * <p> - * Note the indexing syntax <code>xs(i)</code> is a shorthand for - * <code>xs.apply(i)</code>. + * Note the indexing syntax `xs(i)` is a shorthand for + * `xs.apply(i)`. * </p> * * @param i the index - * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or - * <code>length <= i</code> + * @throws ArrayIndexOutOfBoundsException if `i < 0` or + * `length <= i` */ def apply(i: Int): T = throw new Error() @@ -531,18 +559,18 @@ final class Array[T](_length: Int) { * Update the element at given index. * </p> * <p> - * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first - * element of array <code>xs</code>. + * Indices start a `0`; `xs.apply(0)` is the first + * element of array `xs`. * </p> * <p> - * Note the indexing syntax <code>xs(i) = x</code> is a shorthand - * for <code>xs.update(i, x)</code>. + * Note the indexing syntax `xs(i) = x` is a shorthand + * for `xs.update(i, x)`. * </p> * * @param i the index - * @param x the value to be written at index <code>i</code> - * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or - * <code>length <= i</code> + * @param x the value to be written at index `i` + * @throws ArrayIndexOutOfBoundsException if `i < 0` or + * `length <= i` */ def update(i: Int, x: T) { throw new Error() } diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala index 6412a21531..e9955b5563 100644 --- a/src/library/scala/collection/mutable/ArrayBuffer.scala +++ b/src/library/scala/collection/mutable/ArrayBuffer.scala @@ -59,7 +59,7 @@ class ArrayBuffer[A](override protected val initialSize: Int) override def sizeHint(len: Int) { if (len > size && len >= 1) { val newarray = new Array[AnyRef](len) - Array.copy(array, 0, newarray, 0, size0) + compat.Platform.arraycopy(array, 0, newarray, 0, size0) array = newarray } } diff --git a/src/library/scala/collection/package.scala b/src/library/scala/collection/package.scala index bcd2fb5d38..c0e3c14647 100644 --- a/src/library/scala/collection/package.scala +++ b/src/library/scala/collection/package.scala @@ -1,5 +1,72 @@ package scala +/** + * Contains the base traits and objects needed to use and extend Scala's collection library. + * + * == Guide == + * + * A detailed guide for the collections library is avaialble + * at [[http://lampwww.epfl.ch/~odersky/whatsnew/collections-api/collections.html]]. + * + * == Using Collections == + * + * It is convienient to treat all collections as either + * a [[scala.collection.Traversable]] or [[scala.collection.Iterable]], as + * these traits define the vast majority of operations + * on a collection. + * + * Collections can, of course, be treated as specifically as needed, and + * the library is designed to ensure that + * the methods that transform collections will return a collection of the same + * type: {{{ + * scala> val array = Array(1,2,3,4,5,6) + * array: Array[Int] = Array(1, 2, 3, 4, 5, 6) + * + * scala> array map { _.toString } + * res0: Array[java.lang.String] = Array(1, 2, 3, 4, 5, 6) + * + * scala> val list = List(1,2,3,4,5,6) + * list: List[Int] = List(1, 2, 3, 4, 5, 6) + * + * scala> list map { _.toString } + * res1: List[java.lang.String] = List(1, 2, 3, 4, 5, 6) + * + * }}} + * + * == Creating Collections == + * + * The most common way to create a collection is to use the companion objects as factories. + * Of these, the three most common + * are [[scala.collection.immutable.Seq]], [[scala.collection.immutable.Set]], and [[scala.collection.immutable.Map]]. Their + * companion objects are all available + * as type aliases the either the [[scala]] package or in `scala.Predef`, and can be used + * like so: + * {{{ + * scala> val seq = Seq(1,2,3,4,1) + * seq: Seq[Int] = List(1, 2, 3, 4, 1) + * + * scala> val set = Set(1,2,3,4,1) + * set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) + * + * scala> val map = Map(1 -> "one",2 -> "two", 3 -> "three",2 -> "too") + * map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,too), (3,three)) + * }}} + * + * It is also typical to use the [[scala.collection.immutable]] collections over those + * in [[scala.collection.mutable]]; The types aliased in the [[scala]] package and + * the `scala.Predef` object are the immutable versions. + * + * Also note that the collections library was carefully designed to include several implementations of + * each of the three basic collection types. These implementations have specific performance + * characteristics which are described + * in [[http://lampwww.epfl.ch/~odersky/whatsnew/collections-api/collections.html the guide]]. + * + * === Converting between Java Collections === + * + * The `JavaConversions` object provides implicit defs that will allow mostly seamless integration + * between Java Collections-based APIs and the Scala collections library. + * + */ package object collection { import scala.collection.generic.CanBuildFrom // can't refer to CanBuild here @@ -9,4 +76,4 @@ package object collection { new CanBuildFrom[From, T, To] { // TODO: could we just return b instead? def apply(from: From) = b.apply() ; def apply() = b.apply() } -}
\ No newline at end of file +} diff --git a/src/library/scala/package.scala b/src/library/scala/package.scala index 50eccc8ec2..9531572321 100644 --- a/src/library/scala/package.scala +++ b/src/library/scala/package.scala @@ -8,6 +8,9 @@ +/** + * Core Scala types. They are always available without an explicit import. + */ package object scala { type Throwable = java.lang.Throwable type Exception = java.lang.Exception diff --git a/src/library/scala/xml/pull/XMLEvent.scala b/src/library/scala/xml/pull/XMLEvent.scala index 3de618e3a7..255bf3b8a8 100644 --- a/src/library/scala/xml/pull/XMLEvent.scala +++ b/src/library/scala/xml/pull/XMLEvent.scala @@ -11,27 +11,49 @@ package scala.xml package pull -/** This class represents an XML event for pull parsing. - * Pull parsing means that during the traversal of the XML - * tree we are parsing, each "event" is returned to the caller - * and the traversal is suspended. +/** An XML event for pull parsing. All events received during + * parsing will be one of the subclasses of this trait. */ trait XMLEvent -/** An element is encountered the first time */ +/** + * An Element's start tag was encountered. + * @param pre prefix, if any, on the element. This is the `xs` in `<xs:string>foo</xs:string>`. + * @param label the name of the element, not including the prefix + * @param attrs any attributes on the element + */ case class EvElemStart(pre: String, label: String, attrs: MetaData, scope: NamespaceBinding) extends XMLEvent -/** An element is encountered the last time */ +/** + * An Element's end tag was encountered. + * @param pre prefix, if any, on the element. This is the `xs` in `<xs:string>foo</xs:string>`. + * @param label the name of the element, not including the prefix + */ case class EvElemEnd(pre: String, label: String) extends XMLEvent -/** A text node is encountered */ +/** + * A text node was encountered. + * @param text the text that was found + */ case class EvText(text: String) extends XMLEvent -/** An entity reference is encountered */ +/** An entity reference was encountered. + * @param the name of the entity, e.g. `gt` when encountering the entity `>` + */ case class EvEntityRef(entity: String) extends XMLEvent -/** A processing instruction is encountered */ +/** + * A processing instruction was encountered. + * @param target the "PITarget" of the processing instruction. For the instruction `<?foo bar="baz"?>`, the target would + * be `foo` + * @param text the remainder of the instruction. For the instruction `<?foo bar="baz"?>`, the text would + * be `bar="baz"` + * @see [[http://www.w3.org/TR/REC-xml/#sec-pi]] + */ case class EvProcInstr(target: String, text: String) extends XMLEvent -/** A comment is encountered */ +/** + * A comment was encountered + * @param text the text of the comment + */ case class EvComment(text: String) extends XMLEvent diff --git a/src/library/scala/xml/pull/XMLEventReader.scala b/src/library/scala/xml/pull/XMLEventReader.scala index 90c19f9c0b..fa428a440f 100644 --- a/src/library/scala/xml/pull/XMLEventReader.scala +++ b/src/library/scala/xml/pull/XMLEventReader.scala @@ -16,28 +16,14 @@ import java.nio.channels.ClosedChannelException import scala.io.Source import scala.xml.parsing.{ ExternalSources, MarkupHandler, MarkupParser } -/** <p> - * A pull parser that offers to view an XML document as a series of events. - * Example usage: - * </p><pre> - * <b>import</b> scala.xml.pull._ - * <b>import</b> scala.io.Source - * - * <b>object</b> reader { - * <b>val</b> src = Source.fromString("<hello><world/></hello>") - * <b>val</b> er = new XMLEventReader(src) - * - * <b>def</b> main(args: Array[String]) { - * while (er.hasNext) - * Console.println(er.next) - * } - * } - * </pre> +/** + * Main entry point into creating an event-based XML parser. Treating this + * as a [[scala.collection.Iterator]] will provide access to the generated events. + * @param src A [[scala.io.Source]] for XML data to parse * * @author Burak Emir * @author Paul Phillips */ - class XMLEventReader(src: Source) extends ProducerConsumerIterator[XMLEvent] { // We implement a pull parser as an iterator, but since we may be operating on diff --git a/src/library/scala/xml/pull/package.scala b/src/library/scala/xml/pull/package.scala new file mode 100644 index 0000000000..3742c55513 --- /dev/null +++ b/src/library/scala/xml/pull/package.scala @@ -0,0 +1,41 @@ +package scala.xml + +/** + * Classes needed to view an XML document as a series of events. The document + * is parsed by an [[scala.xml.pull.XMLEventReader]] instance. You can treat it as + * an [[scala.collection.Iterator]] to retrieve the events, which are all + * subclasses of [[scala.xml.pull.XMLEvent]]. + * + * {{{ + * scala> val source = Source.fromString("""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> + * <?instruction custom value="customvalue"?> + * <!DOCTYPE foo [ + * <!ENTITY bar "BAR"> + * ]><foo>Hello<!-- this is a comment --><bar>&bar;</bar><bar>></bar></foo>""") + * + * source: scala.io.Source = non-empty iterator + * + * scala> val reader = new XMLEventReader(source) + * reader: scala.xml.pull.XMLEventReader = non-empty iterator + * + * scala> reader.foreach{ println(_) } + * EvProcInstr(instruction,custom value="customvalue") + * EvText( + * ) + * EvElemStart(null,foo,,) + * EvText(Hello) + * EvComment( this is a comment ) + * EvElemStart(null,bar,,) + * EvText(BAR) + * EvElemEnd(null,bar) + * EvElemStart(null,bar,,) + * EvEntityRef(gt) + * EvElemEnd(null,bar) + * EvElemEnd(null,foo) + * EvText( + * + * ) + * + * }}} + */ +package object pull diff --git a/test/files/neg/t3769.check b/test/files/neg/t3769.check new file mode 100644 index 0000000000..7537ed2ddf --- /dev/null +++ b/test/files/neg/t3769.check @@ -0,0 +1,10 @@ +t3769.scala:2: error: in XML literal: expected closing tag of a + val x = <b> <c><a></c> {"text"} </b> + ^ +t3769.scala:2: error: start tag was here: a> + val x = <b> <c><a></c> {"text"} </b> + ^ +t3769.scala:2: error: ';' expected but '{' found. + val x = <b> <c><a></c> {"text"} </b> + ^ +three errors found diff --git a/test/files/neg/t3769.scala b/test/files/neg/t3769.scala new file mode 100644 index 0000000000..0132b59edf --- /dev/null +++ b/test/files/neg/t3769.scala @@ -0,0 +1,3 @@ +object Test { + val x = <b> <c><a></c> {"text"} </b> +} diff --git a/test/files/neg/t3773.check b/test/files/neg/t3773.check new file mode 100644 index 0000000000..29a3e14d83 --- /dev/null +++ b/test/files/neg/t3773.check @@ -0,0 +1,4 @@ +t3773.scala:3: error: method elements in trait IterableLike is deprecated: use `iterator' instead + for ((v, t) <- m.elements) () + ^ +one error found diff --git a/test/files/neg/t3773.flags b/test/files/neg/t3773.flags new file mode 100644 index 0000000000..d1b831ea87 --- /dev/null +++ b/test/files/neg/t3773.flags @@ -0,0 +1 @@ +-deprecation -Xfatal-warnings
\ No newline at end of file diff --git a/test/files/neg/t3773.scala b/test/files/neg/t3773.scala new file mode 100644 index 0000000000..3b92ed2d23 --- /dev/null +++ b/test/files/neg/t3773.scala @@ -0,0 +1,5 @@ +object t { + val m = Map(1 -> "one") + for ((v, t) <- m.elements) () +} + diff --git a/test/files/run/exceptions-nest.check b/test/files/run/exceptions-nest.check index ae66da0a99..48725e4d27 100644 --- a/test/files/run/exceptions-nest.check +++ b/test/files/run/exceptions-nest.check @@ -10,3 +10,4 @@ OK 10 1 () +10 diff --git a/test/files/run/exceptions-nest.scala b/test/files/run/exceptions-nest.scala index 40b00988e4..d3f37452b5 100644 --- a/test/files/run/exceptions-nest.scala +++ b/test/files/run/exceptions-nest.scala @@ -11,6 +11,7 @@ object Test extends Application { println(test9) println(test10) println(test11) + println(test12) def test1 = { var x = 1 @@ -136,4 +137,21 @@ object Test extends Application { try { () } catch { case e => () } } + + class E1 extends Exception + class E2 extends Exception + class E3 extends Exception + + def test12_impl(op: => Int) = try { + op + } catch { + case e: E1 => 2 + case e: E2 => 3 + case e: E3 => 4 + } + def test12 = + test12_impl(1) + + test12_impl(throw new E1) + + test12_impl(throw new E2) + + test12_impl(throw new E3) } diff --git a/test/files/run/t3667.scala b/test/files/run/t3667.scala index 804432c516..7bd0b4ec5e 100644 --- a/test/files/run/t3667.scala +++ b/test/files/run/t3667.scala @@ -18,6 +18,14 @@ object Test { ser(new o3.Inner(1)) o3.Inner ser(new o3.Inner(1)) + + foo + } + + def foo { + case class C(x: Int) + ser(new C(1)) + ser(C) } def ser(o: AnyRef) { diff --git a/test/files/scalap/packageObject/result.test b/test/files/scalap/packageObject/result.test index 324992aae1..6a8d6ae1d5 100644 --- a/test/files/scalap/packageObject/result.test +++ b/test/files/scalap/packageObject/result.test @@ -2,5 +2,4 @@ package object PackageObject extends java.lang.Object with scala.ScalaObject { def this() = { /* compiled code */ } type A = scala.Predef.String def foo(i : scala.Int) : scala.Int = { /* compiled code */ } - protected def readResolve() : java.lang.Object = { /* compiled code */ } } diff --git a/test/files/scalap/traitObject/result.test b/test/files/scalap/traitObject/result.test index 8978873fc7..0d7de1535d 100644 --- a/test/files/scalap/traitObject/result.test +++ b/test/files/scalap/traitObject/result.test @@ -5,5 +5,4 @@ trait TraitObject extends java.lang.Object with scala.ScalaObject { object TraitObject extends java.lang.Object with scala.ScalaObject { def this() = { /* compiled code */ } def bar : scala.Int = { /* compiled code */ } - protected def readResolve() : java.lang.Object = { /* compiled code */ } } |