From 02b2da63409af6a28824cbb74d00d0ec04518c8d Mon Sep 17 00:00:00 2001 From: Jean-Remi Desjardins Date: Sun, 23 Dec 2012 16:44:59 -0500 Subject: SI-5017 Poor performance of :+ operator on Arrays Control performance of :+ and +: operator on my machine were 700-800 ms After adding size hint on the implementation in SeqLike, it went down to 500-600 ms But with specialixed implementation in ArrayOps, brings it down to 300-400 ms Unfortunatly, this method will only be called when the Array object is being referenced directly as it's type, but that should be the case enough times to justify the extra method. I ended up removing the sizeHint in SeqLike because it made the execution of the "benchmark" slower when the Array was being manipulated as a Seq. Side note: Interestingly enough, the benchmark performed better on my virtualized Fedora 17 with JDK 7 than natively on Mac OS X with JDK 6 --- test/files/run/array-addition.check | 4 ++++ test/files/run/array-addition.scala | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/files/run/array-addition.check create mode 100644 test/files/run/array-addition.scala (limited to 'test') diff --git a/test/files/run/array-addition.check b/test/files/run/array-addition.check new file mode 100644 index 0000000000..7bfbd9c711 --- /dev/null +++ b/test/files/run/array-addition.check @@ -0,0 +1,4 @@ +Array(1, 2, 3, 4) +Array(1, 2, 3, 4) +Array(1) +Array(1) diff --git a/test/files/run/array-addition.scala b/test/files/run/array-addition.scala new file mode 100644 index 0000000000..8def48e85c --- /dev/null +++ b/test/files/run/array-addition.scala @@ -0,0 +1,11 @@ +object Test { + def prettyPrintArray(x: Array[_]) = println("Array(" + x.mkString(", ") + ")") + + def main(args: Array[String]): Unit = { + prettyPrintArray(Array(1,2,3) :+ 4) + prettyPrintArray(1 +: Array(2,3,4)) + prettyPrintArray(Array() :+ 1) + prettyPrintArray(1 +: Array()) + } +} + -- cgit v1.2.3 From 231d59dcf57af99a9e2a1366afd18680c13cd6ce Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sat, 22 Dec 2012 10:12:03 -0800 Subject: SI-6829, SI-6788, NPEs during erroneous compilation. Have to intercept trees which have a null type due to errors before they leave the warm confines of 'def typed' because from that point everything assumes tree.tpe != null. --- .../scala/tools/nsc/typechecker/Typers.scala | 12 +++- test/files/neg/t6788.check | 5 ++ test/files/neg/t6788.scala | 7 +++ test/files/neg/t6829.check | 36 ++++++++++++ test/files/neg/t6829.scala | 64 ++++++++++++++++++++++ 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 test/files/neg/t6788.check create mode 100644 test/files/neg/t6788.scala create mode 100644 test/files/neg/t6829.check create mode 100644 test/files/neg/t6829.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 e45d64ade5..9d390476db 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -5264,7 +5264,7 @@ trait Typers extends Modes with Adaptations with Tags { def typedDocDef(docdef: DocDef) = { if (forScaladoc && (sym ne null) && (sym ne NoSymbol)) { - val comment = docdef.comment + val comment = docdef.comment docComments(sym) = comment comment.defineVariables(sym) val typer1 = newTyper(context.makeNewScope(tree, context.owner)) @@ -5595,12 +5595,18 @@ trait Typers extends Modes with Adaptations with Tags { "context.owner" -> context.owner ) ) - val tree1 = typed1(tree, mode, dropExistential(pt)) + typed1(tree, mode, dropExistential(pt)) + } + // Can happen during erroneous compilation - error(s) have been + // reported, but we need to avoid causing an NPE with this tree + if (tree1.tpe eq null) + return setError(tree) + + if (!alreadyTyped) { printTyping("typed %s: %s%s".format( ptTree(tree1), tree1.tpe, if (isSingleType(tree1.tpe)) " with underlying "+tree1.tpe.widen else "") ) - tree1 } tree1.tpe = addAnnotations(tree1, tree1.tpe) diff --git a/test/files/neg/t6788.check b/test/files/neg/t6788.check new file mode 100644 index 0000000000..96a6f8b601 --- /dev/null +++ b/test/files/neg/t6788.check @@ -0,0 +1,5 @@ +t6788.scala:6: error: not found: value foo +Error occurred in an application involving default arguments. + s.copy(b = foo) + ^ +one error found diff --git a/test/files/neg/t6788.scala b/test/files/neg/t6788.scala new file mode 100644 index 0000000000..77949ed621 --- /dev/null +++ b/test/files/neg/t6788.scala @@ -0,0 +1,7 @@ +case class B[T](b: T, a: List[Int]) // need two args, B must be polymorphic + +class A { + var s: B[Int] = _ // has to be a var + + s.copy(b = foo) +} diff --git a/test/files/neg/t6829.check b/test/files/neg/t6829.check new file mode 100644 index 0000000000..8ee6d182eb --- /dev/null +++ b/test/files/neg/t6829.check @@ -0,0 +1,36 @@ +t6829.scala:35: error: type mismatch; + found : AgentSimulation.this.state.type (with underlying type G#State) + required: _10.State + lazy val actions: Map[G#Agent,G#Action] = agents.map(a => a -> a.chooseAction(state)).toMap + ^ +t6829.scala:45: error: trait AgentSimulation takes type parameters + pastHistory: List[G#State] = Nil) extends AgentSimulation + ^ +t6829.scala:47: error: class LearningSimulation takes type parameters + lazy val step: LearningSimulation = { + ^ +t6829.scala:49: error: not found: value actions + val (s,a,s2) = (state,actions(agent),nextState) + ^ +t6829.scala:49: error: not found: value nextState + val (s,a,s2) = (state,actions(agent),nextState) + ^ +t6829.scala:50: error: type mismatch; + found : s.type (with underlying type Any) + required: _54.State where val _54: G + val r = rewards(agent).r(s,a,s2) + ^ +t6829.scala:51: error: type mismatch; + found : s.type (with underlying type Any) + required: _51.State + agent.learn(s,a,s2,r): G#Agent + ^ +t6829.scala:53: error: not found: value nextState +Error occurred in an application involving default arguments. + copy(agents = updatedAgents, state = nextState, pastHistory = currentHistory) + ^ +t6829.scala:53: error: not found: value currentHistory +Error occurred in an application involving default arguments. + copy(agents = updatedAgents, state = nextState, pastHistory = currentHistory) + ^ +9 errors found diff --git a/test/files/neg/t6829.scala b/test/files/neg/t6829.scala new file mode 100644 index 0000000000..7cbe3c9542 --- /dev/null +++ b/test/files/neg/t6829.scala @@ -0,0 +1,64 @@ +package bugs + +/** + * Created with IntelliJ IDEA. + * User: arya + * Date: 12/18/12 + * Time: 4:17 PM + * To change this template use File | Settings | File Templates. + */ +object currenttype2 { + + type Reward = Double + + trait AbstractAgent[State,Action] { + type A = AbstractAgent[State,Action] + def chooseAction(s: State): Action + def startEpisode: A = this + def learn(s1: State, a: Action, s2: State, r: Reward): A + } + + case class RewardFunction[State,Action](r: (State,Action,State) => Reward) + + trait Rules[G<:GameDomain] { + def simulate(state: G#State, agentActions: List[(G#Agent,G#Action)]): G#State + } + + trait AgentSimulation[G<:GameDomain] { + val agents: List[G#Agent] + val state: G#State + val rewards: Map[G#Agent,G#Rewards] + val rules: Rules[G] + val pastHistory: List[G#State] + lazy val currentHistory = state :: pastHistory + + lazy val actions: Map[G#Agent,G#Action] = agents.map(a => a -> a.chooseAction(state)).toMap + lazy val nextState: G#State = rules.simulate(state, actions.toList) + + def step: AgentSimulation[G] + } + + case class LearningSimulation[G<:GameDomain](agents: List[G#Agent], + state: G#State, + rewards: Map[G#Agent,G#Rewards], + rules: Rules[G], + pastHistory: List[G#State] = Nil) extends AgentSimulation + { + lazy val step: LearningSimulation = { + val updatedAgents: List[G#Agent] = agents map { agent => + val (s,a,s2) = (state,actions(agent),nextState) + val r = rewards(agent).r(s,a,s2) + agent.learn(s,a,s2,r): G#Agent + } + copy(agents = updatedAgents, state = nextState, pastHistory = currentHistory) + } + } + + trait GameDomain { + domain => + type State + type Action + type Agent = AbstractAgent[State, Action] // agent supertype + type Rewards = RewardFunction[State,Action] + } + } -- cgit v1.2.3 From ac61e341216c2c61e29c74a8c3c27e915d479925 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Thu, 27 Dec 2012 11:41:22 -0800 Subject: SI-6194, repl crash. Always a bad idea to use replaceAll on unknown strings, as we saw here when windows classpaths arrived containing escape-requiring backslashes. --- src/compiler/scala/tools/nsc/util/ClassPath.scala | 8 ++++++-- test/files/run/t6194.check | 1 + test/files/run/t6194.scala | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 test/files/run/t6194.check create mode 100644 test/files/run/t6194.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/util/ClassPath.scala b/src/compiler/scala/tools/nsc/util/ClassPath.scala index 8732e06502..471e2653cf 100644 --- a/src/compiler/scala/tools/nsc/util/ClassPath.scala +++ b/src/compiler/scala/tools/nsc/util/ClassPath.scala @@ -15,6 +15,7 @@ import scala.reflect.ClassTag import Jar.isJarOrZip import File.pathSeparator import java.net.MalformedURLException +import java.util.regex.PatternSyntaxException /**

* This module provides star expansion of '-classpath' option arguments, behaves the same as @@ -39,8 +40,11 @@ object ClassPath { if (pattern == "*") lsDir(Directory(".")) else if (pattern endsWith wildSuffix) lsDir(Directory(pattern dropRight 2)) else if (pattern contains '*') { - val regexp = ("^%s$" format pattern.replaceAll("""\*""", """.*""")).r - lsDir(Directory(pattern).parent, regexp findFirstIn _ isDefined) + try { + val regexp = ("^" + pattern.replaceAllLiterally("""\*""", """.*""") + "$").r + lsDir(Directory(pattern).parent, regexp findFirstIn _ isDefined) + } + catch { case _: PatternSyntaxException => List(pattern) } } else List(pattern) } diff --git a/test/files/run/t6194.check b/test/files/run/t6194.check new file mode 100644 index 0000000000..b325f479d7 --- /dev/null +++ b/test/files/run/t6194.check @@ -0,0 +1 @@ +C:\FooBar\Java\includes\*.jar diff --git a/test/files/run/t6194.scala b/test/files/run/t6194.scala new file mode 100644 index 0000000000..ced3259427 --- /dev/null +++ b/test/files/run/t6194.scala @@ -0,0 +1,8 @@ +import scala.tools.nsc.util._ + +object Test { + def main(args: Array[String]): Unit = { + val cp = ClassPath.expandPath("""C:\FooBar\Java\includes\*.jar""") mkString java.io.File.pathSeparator + println(cp) + } +} -- cgit v1.2.3 From 9575ee9961df08e3e83b473c40f41a1e3548fb8d Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Thu, 27 Dec 2012 16:00:18 -0800 Subject: Remove -deprecation from partest default options. Who knows why it was ever like this; it's not like anyone sees the deprecation warnings. In PR #1807 there is now a test which depends on partest not making this move, so it's a good time to finally expunge it. --- src/partest/scala/tools/partest/PartestDefaults.scala | 2 +- src/partest/scala/tools/partest/nest/CompileManager.scala | 1 - test/files/neg/classmanifests_new_deprecations.flags | 2 +- test/files/neg/for-comprehension-old.flags | 1 + test/files/neg/macro-deprecate-idents.flags | 2 +- test/files/neg/macro-invalidshape-d.flags | 2 +- test/files/neg/names-defaults-neg.flags | 1 + test/files/neg/t5589neg.flags | 1 + test/files/neg/t5956.flags | 1 + 9 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 test/files/neg/for-comprehension-old.flags create mode 100644 test/files/neg/names-defaults-neg.flags create mode 100644 test/files/neg/t5589neg.flags create mode 100644 test/files/neg/t5956.flags (limited to 'test') diff --git a/src/partest/scala/tools/partest/PartestDefaults.scala b/src/partest/scala/tools/partest/PartestDefaults.scala index b27ce6ff75..a21c602d14 100644 --- a/src/partest/scala/tools/partest/PartestDefaults.scala +++ b/src/partest/scala/tools/partest/PartestDefaults.scala @@ -21,7 +21,7 @@ object PartestDefaults { def javaCmd = propOrElse("partest.javacmd", "java") def javacCmd = propOrElse("partest.javac_cmd", "javac") def javaOpts = propOrElse("partest.java_opts", "") - def scalacOpts = propOrElse("partest.scalac_opts", "-deprecation") + def scalacOpts = propOrElse("partest.scalac_opts", "") def testBuild = propOrNone("partest.build") def errorCount = propOrElse("partest.errors", "0").toInt diff --git a/src/partest/scala/tools/partest/nest/CompileManager.scala b/src/partest/scala/tools/partest/nest/CompileManager.scala index 188ebf66ed..3d902d6d00 100644 --- a/src/partest/scala/tools/partest/nest/CompileManager.scala +++ b/src/partest/scala/tools/partest/nest/CompileManager.scala @@ -41,7 +41,6 @@ class ExtConsoleReporter(settings: Settings, val writer: PrintWriter) extends Co class TestSettings(cp: String, error: String => Unit) extends Settings(error) { def this(cp: String) = this(cp, _ => ()) - deprecation.value = true nowarnings.value = false encoding.value = "UTF-8" classpath.value = cp diff --git a/test/files/neg/classmanifests_new_deprecations.flags b/test/files/neg/classmanifests_new_deprecations.flags index e8fb65d50c..c6bfaf1f64 100644 --- a/test/files/neg/classmanifests_new_deprecations.flags +++ b/test/files/neg/classmanifests_new_deprecations.flags @@ -1 +1 @@ --Xfatal-warnings \ No newline at end of file +-deprecation -Xfatal-warnings diff --git a/test/files/neg/for-comprehension-old.flags b/test/files/neg/for-comprehension-old.flags new file mode 100644 index 0000000000..dcc59ebe32 --- /dev/null +++ b/test/files/neg/for-comprehension-old.flags @@ -0,0 +1 @@ +-deprecation diff --git a/test/files/neg/macro-deprecate-idents.flags b/test/files/neg/macro-deprecate-idents.flags index e8fb65d50c..c6bfaf1f64 100644 --- a/test/files/neg/macro-deprecate-idents.flags +++ b/test/files/neg/macro-deprecate-idents.flags @@ -1 +1 @@ --Xfatal-warnings \ No newline at end of file +-deprecation -Xfatal-warnings diff --git a/test/files/neg/macro-invalidshape-d.flags b/test/files/neg/macro-invalidshape-d.flags index cd66464f2f..83b7265eb9 100644 --- a/test/files/neg/macro-invalidshape-d.flags +++ b/test/files/neg/macro-invalidshape-d.flags @@ -1 +1 @@ --language:experimental.macros \ No newline at end of file +-deprecation -language:experimental.macros diff --git a/test/files/neg/names-defaults-neg.flags b/test/files/neg/names-defaults-neg.flags new file mode 100644 index 0000000000..dcc59ebe32 --- /dev/null +++ b/test/files/neg/names-defaults-neg.flags @@ -0,0 +1 @@ +-deprecation diff --git a/test/files/neg/t5589neg.flags b/test/files/neg/t5589neg.flags new file mode 100644 index 0000000000..dcc59ebe32 --- /dev/null +++ b/test/files/neg/t5589neg.flags @@ -0,0 +1 @@ +-deprecation diff --git a/test/files/neg/t5956.flags b/test/files/neg/t5956.flags new file mode 100644 index 0000000000..dcc59ebe32 --- /dev/null +++ b/test/files/neg/t5956.flags @@ -0,0 +1 @@ +-deprecation -- cgit v1.2.3 From 24a033b2aa4174fe8e9c3c02372b6508ef404601 Mon Sep 17 00:00:00 2001 From: Jean-Remi Desjardins Date: Thu, 22 Nov 2012 00:59:43 -0500 Subject: SI-6415, overly eager evaluation in Stream. The lengthCompare method in LinearSeqOptimized was looking one step further than it needed to in order to give the correct result, which was creating some unwanted side effects related to Streams. --- .../scala/collection/LinearSeqOptimized.scala | 20 +++++++++++++------- src/library/scala/collection/SeqLike.scala | 16 ++++++++++------ test/files/run/streams.check | 14 ++++++++++++++ test/files/run/streams.scala | 20 ++++++++++++++++++++ 4 files changed, 57 insertions(+), 13 deletions(-) (limited to 'test') diff --git a/src/library/scala/collection/LinearSeqOptimized.scala b/src/library/scala/collection/LinearSeqOptimized.scala index 0f0a405a85..48b0b5469e 100755 --- a/src/library/scala/collection/LinearSeqOptimized.scala +++ b/src/library/scala/collection/LinearSeqOptimized.scala @@ -247,14 +247,20 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea } override /*SeqLike*/ - def lengthCompare(len: Int): Int = { - var i = 0 - var these = self - while (!these.isEmpty && i <= len) { - i += 1 - these = these.tail + def lengthCompare(len: Int): Int = { + // TODO: Remove this method when incrementing a major revision + // This method is the same as in SeqLike, no need to redefine it + if (len < 0) 1 + else { + var i = 0 + val it = iterator + while (it.hasNext) { + if (i == len) return if (it.hasNext) 1 else 0 + it.next() + i += 1 + } + i - len } - i - len } override /*SeqLike*/ diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index f65e2ef9cd..1be0dba29f 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -84,13 +84,17 @@ trait SeqLike[+A, +Repr] extends Any with IterableLike[A, Repr] with GenSeqLike[ * if computing `length` is cheap. */ def lengthCompare(len: Int): Int = { - var i = 0 - val it = iterator - while (it.hasNext && i <= len) { - it.next() - i += 1 + if (len < 0) 1 + else { + var i = 0 + val it = iterator + while (it.hasNext) { + if (i == len) return if (it.hasNext) 1 else 0 + it.next() + i += 1 + } + i - len } - i - len } override /*IterableLike*/ def isEmpty: Boolean = lengthCompare(0) == 0 diff --git a/test/files/run/streams.check b/test/files/run/streams.check index 7f894052d9..db6d2eebab 100644 --- a/test/files/run/streams.check +++ b/test/files/run/streams.check @@ -1,5 +1,8 @@ Stream() Stream() +true +true +true Array(1) Stream(1, ?) @@ -8,12 +11,21 @@ Stream() Stream() Stream(1) Stream() +true +true +true +true Array(1, 2) Stream(2) Stream() Stream(1, 2) Stream() +true +true +true +true +true 999 512 @@ -23,3 +35,5 @@ Stream(100001, ?) true true 705082704 + +true diff --git a/test/files/run/streams.scala b/test/files/run/streams.scala index 51b4e5d76c..03b2622edd 100644 --- a/test/files/run/streams.scala +++ b/test/files/run/streams.scala @@ -2,6 +2,9 @@ object Test extends App { val s0: Stream[Int] = Stream.empty println(s0.take(1)) println(s0.takeWhile(_ > 0)) + println(s0.lengthCompare(-5) > 0) + println(s0.lengthCompare(0) == 0) + println(s0.lengthCompare(5) < 0) println val s1 = Stream.cons(1, Stream.empty) @@ -12,6 +15,10 @@ object Test extends App { println(s1.drop(2)) println(s1.drop(-1)) println(s1.dropWhile(_ > 0)) + println(s1.lengthCompare(-5) > 0) + println(s1.lengthCompare(0) > 0) + println(s1.lengthCompare(1) == 0) + println(s1.lengthCompare(5) < 0) println val s2 = s1.append(Stream.cons(2, Stream.empty)) @@ -20,6 +27,11 @@ object Test extends App { println(s2.drop(2)) println(s2.drop(-1)) println(s2.dropWhile(_ > 0)) + println(s2.lengthCompare(-5) > 0) + println(s2.lengthCompare(0) > 0) + println(s2.lengthCompare(1) > 0) + println(s2.lengthCompare(2) == 0) + println(s2.lengthCompare(5) < 0) println val s3 = Stream.range(1, 1000) //100000 (ticket #153: Stackoverflow) @@ -43,4 +55,12 @@ object Test extends App { println(Stream.from(1).take(size).foldLeft(0)(_ + _)) val arr = new Array[Int](size) Stream.from(1).take(size).copyToArray(arr, 0) + + println + + // ticket #6415 + lazy val x = { println("evaluated"); 1 } + val s4 = 0 #:: x #:: Stream.empty + + println(s4.isDefinedAt(0)) } -- cgit v1.2.3 From 13643815fe41b17e931be9d0a8906200b0147a23 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 28 Dec 2012 16:52:47 -0800 Subject: LinearSeq lengthCompare without an iterator. Had to fix up an iffy test: not only was it testing undefined behavior, it demanded just the right numbers be printed in a context where all negative or positive numbers are equivalent. It's the ol' "get them coming and going" trick. --- .../scala/collection/LinearSeqOptimized.scala | 22 ++++++++++------------ test/files/run/t2544.check | 4 ++-- test/files/run/t2544.scala | 22 ++++++++++++++-------- 3 files changed, 26 insertions(+), 22 deletions(-) (limited to 'test') diff --git a/src/library/scala/collection/LinearSeqOptimized.scala b/src/library/scala/collection/LinearSeqOptimized.scala index 48b0b5469e..81cccea519 100755 --- a/src/library/scala/collection/LinearSeqOptimized.scala +++ b/src/library/scala/collection/LinearSeqOptimized.scala @@ -12,6 +12,7 @@ import generic._ import mutable.ListBuffer import immutable.List import scala.util.control.Breaks._ +import scala.annotation.tailrec /** A template trait for linear sequences of type `LinearSeq[A]` which optimizes * the implementation of several methods under the assumption of fast linear access. @@ -248,19 +249,16 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea override /*SeqLike*/ def lengthCompare(len: Int): Int = { - // TODO: Remove this method when incrementing a major revision - // This method is the same as in SeqLike, no need to redefine it - if (len < 0) 1 - else { - var i = 0 - val it = iterator - while (it.hasNext) { - if (i == len) return if (it.hasNext) 1 else 0 - it.next() - i += 1 - } - i - len + @tailrec def loop(i: Int, xs: Repr): Int = { + if (i == len) + if (xs.isEmpty) 0 else 1 + else if (xs.isEmpty) + -1 + else + loop(i + 1, xs.tail) } + if (len < 0) 1 + else loop(0, this) } override /*SeqLike*/ diff --git a/test/files/run/t2544.check b/test/files/run/t2544.check index 716b146ac4..d19538dca3 100644 --- a/test/files/run/t2544.check +++ b/test/files/run/t2544.check @@ -2,8 +2,8 @@ 2 3 3 --2 --2 +-1 +-1 1 1 0 diff --git a/test/files/run/t2544.scala b/test/files/run/t2544.scala index 7e7cfeb357..6bee2f1082 100644 --- a/test/files/run/t2544.scala +++ b/test/files/run/t2544.scala @@ -1,19 +1,25 @@ object Test { object Foo extends Seq[Int] { def apply(i: Int) = i - def length = 4 + def length = 5 def iterator = Iterator(0,1,2,3,4) } + def lengthEquiv(result: Int) = println( + if (result < 0) -1 + else if (result == 0) 0 + else 1 + ) + def main(args: Array[String]) = { println(Foo indexWhere(_ >= 2,1)) println(Foo.toList indexWhere(_ >= 2,1)) println(Foo segmentLength(_ <= 3,1)) println(Foo.toList segmentLength(_ <= 3,1)) - println(Foo lengthCompare 7) - println(Foo.toList lengthCompare 7) - println(Foo lengthCompare 2) - println(Foo.toList lengthCompare 2) - println(Foo lengthCompare 5) - println(Foo.toList lengthCompare 5) + lengthEquiv(Foo lengthCompare 7) + lengthEquiv(Foo.toList lengthCompare 7) + lengthEquiv(Foo lengthCompare 2) + lengthEquiv(Foo.toList lengthCompare 2) + lengthEquiv(Foo lengthCompare 5) + lengthEquiv(Foo.toList lengthCompare 5) } -} \ No newline at end of file +} -- cgit v1.2.3 From a6ce037f9647615192e4dc1dd3162c0fe0a4d132 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 31 Dec 2012 02:07:58 -0800 Subject: SI-6896, spurious warning with overloaded main. Make sure there's no legit main signature before issuing any warnings about missing main methods. --- .../scala/tools/nsc/backend/jvm/GenJVMASM.scala | 24 ++++++++++++---------- test/files/pos/t6896.flags | 1 + test/files/pos/t6896.scala | 7 +++++++ 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 test/files/pos/t6896.flags create mode 100644 test/files/pos/t6896.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 49c0fa2757..540935fd57 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenJVMASM.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenJVMASM.scala @@ -78,17 +78,19 @@ trait GenJVMASM { failNoForwarder("companion is a trait") // Now either succeeed, or issue some additional warnings for things which look like // attempts to be java main methods. - else possibles exists { m => - m.info match { - case PolyType(_, _) => - fail("main methods cannot be generic.") - case MethodType(params, res) => - if (res.typeSymbol :: params exists (_.isAbstractType)) - fail("main methods cannot refer to type parameters or abstract types.", m.pos) - else - isJavaMainMethod(m) || fail("main method must have exact signature (Array[String])Unit", m.pos) - case tp => - fail("don't know what this is: " + tp, m.pos) + else (possibles exists isJavaMainMethod) || { + possibles exists { m => + m.info match { + case PolyType(_, _) => + fail("main methods cannot be generic.") + case MethodType(params, res) => + if (res.typeSymbol :: params exists (_.isAbstractType)) + fail("main methods cannot refer to type parameters or abstract types.", m.pos) + else + isJavaMainMethod(m) || fail("main method must have exact signature (Array[String])Unit", m.pos) + case tp => + fail("don't know what this is: " + tp, m.pos) + } } } } diff --git a/test/files/pos/t6896.flags b/test/files/pos/t6896.flags new file mode 100644 index 0000000000..85d8eb2ba2 --- /dev/null +++ b/test/files/pos/t6896.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/pos/t6896.scala b/test/files/pos/t6896.scala new file mode 100644 index 0000000000..ab527a804a --- /dev/null +++ b/test/files/pos/t6896.scala @@ -0,0 +1,7 @@ +object TooManyMains { + def main(args: Array[String]): Unit = { + println("Hello, World!") + } + def main(a: Int, b: Int) = ??? + def main(s: String, n: String) = ??? +} -- cgit v1.2.3 From 340529410696da2e5110d49a5a8d19572bf272ba Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 31 Dec 2012 01:49:32 -0800 Subject: SI-6897, lubs and varargs star. Don't allow lubs to calculate refinement types which contain a varargs star outside of legal varargs star position. --- src/reflect/scala/reflect/internal/Types.scala | 5 ++++- test/files/pos/t6897.scala | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/t6897.scala (limited to 'test') diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index 119a57d268..bfd18f6a43 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -6801,7 +6801,10 @@ trait Types extends api.Types { self: SymbolTable => else lubBase } } - existentialAbstraction(tparams, lubType) + // dropRepeatedParamType is a localized fix for SI-6897. We should probably + // integrate that transformation at a lower level in master, but lubs are + // the likely and maybe only spot they escape, so fixing here for 2.10.1. + existentialAbstraction(tparams, dropRepeatedParamType(lubType)) } if (printLubs) { println(indent + "lub of " + ts + " at depth "+depth)//debug diff --git a/test/files/pos/t6897.scala b/test/files/pos/t6897.scala new file mode 100644 index 0000000000..a7a03a1d3a --- /dev/null +++ b/test/files/pos/t6897.scala @@ -0,0 +1,6 @@ +class A { + val html = (null: Any) match { + case 1 => + case 2 =>

+ } +} -- cgit v1.2.3 From eeb6ee6eb21e7bd473ab5775474444b5d1b72856 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Thu, 3 Jan 2013 20:35:22 -0800 Subject: SI-6911, regression in generated case class equality. Caught out by the different semantics of isInstanceOf and pattern matching. trait K { case class CC(name: String) } object Foo extends K object Bar extends K Foo.CC("a") == Bar.CC("a") That expression is supposed to be false, and with this commit it is once again. --- .../tools/nsc/typechecker/SyntheticMethods.scala | 20 ++- test/files/run/idempotency-case-classes.check | 5 +- test/files/run/inline-ex-handlers.check | 146 +++++++++++---------- test/files/run/t6911.scala | 24 ++++ 4 files changed, 122 insertions(+), 73 deletions(-) create mode 100644 test/files/run/t6911.scala (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index 17e67e6429..a907ab6c66 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -157,11 +157,23 @@ trait SyntheticMethods extends ast.TreeDSL { Ident(m.firstParam) IS_OBJ classExistentialType(clazz)) } - /** (that.isInstanceOf[this.C]) - * where that is the given methods first parameter. + /** that match { case _: this.C => true ; case _ => false } + * where `that` is the given method's first parameter. + * + * An isInstanceOf test is insufficient because it has weaker + * requirements than a pattern match. Given an inner class Foo and + * two different instantiations of the container, an x.Foo and and a y.Foo + * are both .isInstanceOf[Foo], but the one does not match as the other. */ - def thatTest(eqmeth: Symbol): Tree = - gen.mkIsInstanceOf(Ident(eqmeth.firstParam), classExistentialType(clazz), true, false) + def thatTest(eqmeth: Symbol): Tree = { + Match( + Ident(eqmeth.firstParam), + List( + CaseDef(Typed(Ident(nme.WILDCARD), TypeTree(clazz.tpe)), EmptyTree, TRUE), + CaseDef(WILD.empty, EmptyTree, FALSE) + ) + ) + } /** (that.asInstanceOf[this.C]) * where that is the given methods first parameter. diff --git a/test/files/run/idempotency-case-classes.check b/test/files/run/idempotency-case-classes.check index d2fb8169cb..700af3b81b 100644 --- a/test/files/run/idempotency-case-classes.check +++ b/test/files/run/idempotency-case-classes.check @@ -29,7 +29,10 @@ C(2,3) 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].&&({ + override def equals(x$1: Any): Boolean = C.this.eq(x$1.asInstanceOf[Object]).||(x$1 match { + case (_: C) => true + case _ => false +}.&&({ 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)) })) diff --git a/test/files/run/inline-ex-handlers.check b/test/files/run/inline-ex-handlers.check index 45db7c3a15..282542a732 100644 --- a/test/files/run/inline-ex-handlers.check +++ b/test/files/run/inline-ex-handlers.check @@ -13,15 +13,23 @@ < 92 JUMP 2 < < 2: -370c369 +247c246 +< blocks: [1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18] +--- +> blocks: [1,2,3,4,5,6,8,10,11,12,13,14,15,16,17,18] +258,260d256 +< 92 JUMP 7 +< +< 7: +395c391 < locals: value args, variable result, value ex6, value x4, value x5, value message, value x --- > locals: value args, variable result, value ex6, value x4, value x5, value x -372c371 +397c393 < blocks: [1,2,3,4,5,8,11,13,14,16] --- > blocks: [1,2,3,5,8,11,13,14,16,17] -396c395,404 +421c417,426 < 103 THROW(MyException) --- > ? STORE_LOCAL(value ex6) @@ -34,25 +42,25 @@ > 106 LOAD_LOCAL(value x4) > 106 IS_INSTANCE REF(class MyException) > 106 CZJUMP (BOOL)NE ? 5 : 11 -409,411d416 +434,436d438 < 101 JUMP 4 < < 4: -425,428d429 +450,453d451 < 106 LOAD_LOCAL(value x5) < 106 CALL_METHOD MyException.message (dynamic) < 106 STORE_LOCAL(value message) < 106 SCOPE_ENTER value message -430c431,432 +455c453,454 < 106 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 106 CALL_METHOD MyException.message (dynamic) -502c504 +527c526 < blocks: [1,2,3,4,6,7,8,9,10] --- > blocks: [1,2,3,4,6,7,8,9,10,11,12,13] -531c533,538 +556c555,560 < 306 THROW(MyException) --- > ? JUMP 11 @@ -61,11 +69,11 @@ > ? LOAD_LOCAL(variable monitor4) > 305 MONITOR_EXIT > ? JUMP 12 -537c544 +562c566 < ? THROW(Throwable) --- > ? JUMP 12 -543c550,557 +568c572,579 < ? THROW(Throwable) --- > ? STORE_LOCAL(value t) @@ -76,7 +84,7 @@ > 304 MONITOR_EXIT > ? STORE_LOCAL(value t) > ? JUMP 13 -558a573,584 +583a595,606 > 13: > 310 LOAD_MODULE object Predef > 310 CALL_PRIMITIVE(StartConcat) @@ -89,34 +97,35 @@ > 310 CALL_METHOD scala.Predef.println (dynamic) > 310 JUMP 2 > -567c593 +592c615 < catch (Throwable) in ArrayBuffer(7, 8, 9, 10) starting at: 6 --- > catch (Throwable) in ArrayBuffer(7, 8, 9, 10, 11) starting at: 6 -570c596 +595c618 < catch (Throwable) in ArrayBuffer(4, 6, 7, 8, 9, 10) starting at: 3 --- > catch (Throwable) in ArrayBuffer(4, 6, 7, 8, 9, 10, 11, 12) starting at: 3 -602c628 +627c650 < blocks: [1,2,3,4,5,6,7,9,10] --- > blocks: [1,2,3,4,5,6,7,9,10,11,12] -626c652,658 +651c674,675 < 78 THROW(IllegalArgumentException) --- > ? STORE_LOCAL(value e) > ? JUMP 11 -> +652a677,681 > 11: > 81 LOAD_LOCAL(value e) > ? STORE_LOCAL(variable exc1) > ? JUMP 12 -655c687,688 +> +680c709,710 < 81 THROW(Exception) --- > ? STORE_LOCAL(variable exc1) > ? JUMP 12 -671a705,717 +696a727,739 > 12: > 83 LOAD_MODULE object Predef > 83 CONSTANT("finally") @@ -130,19 +139,19 @@ > 84 LOAD_LOCAL(variable exc1) > 84 THROW(Throwable) > -677c723 +702c745 < catch () in ArrayBuffer(4, 6, 7, 9) starting at: 3 --- > catch () in ArrayBuffer(4, 6, 7, 9, 11) starting at: 3 -701c747 +726c769 < locals: value args, variable result, value ex6, variable exc2, value x4, value x5, value message, value x, value ex6, value x4, value x5, value message, value x --- > locals: value args, variable result, value ex6, variable exc2, value x4, value x5, value x, value ex6, value x4, value x5, value x -703c749 +728c771 < blocks: [1,2,3,4,5,6,9,12,14,17,18,19,22,25,27,28,30,31] --- > blocks: [1,2,3,4,5,6,9,12,14,17,18,19,22,25,27,28,30,31,32,33,34] -727c773,780 +752c795,802 < 172 THROW(MyException) --- > ? STORE_LOCAL(value ex6) @@ -153,64 +162,64 @@ > 170 STORE_LOCAL(value x4) > 170 SCOPE_ENTER value x4 > 170 JUMP 18 -774,777d826 +799,802d848 < 175 LOAD_LOCAL(value x5) < 175 CALL_METHOD MyException.message (dynamic) < 175 STORE_LOCAL(value message) < 175 SCOPE_ENTER value message -779c828,829 +804c850,851 < 176 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 176 CALL_METHOD MyException.message (dynamic) -783c833,834 +808c855,856 < 177 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 177 CALL_METHOD MyException.message (dynamic) -785c836,837 +810c858,859 < 177 THROW(MyException) --- > ? STORE_LOCAL(value ex6) > ? JUMP 33 -789c841,842 +814c863,864 < 170 THROW(Throwable) --- > ? STORE_LOCAL(value ex6) > ? JUMP 33 -798a852,857 +823a874,879 > 33: > 169 LOAD_LOCAL(value ex6) > 169 STORE_LOCAL(value x4) > 169 SCOPE_ENTER value x4 > 169 JUMP 5 > -813,816d871 +838,841d893 < 180 LOAD_LOCAL(value x5) < 180 CALL_METHOD MyException.message (dynamic) < 180 STORE_LOCAL(value message) < 180 SCOPE_ENTER value message -818c873,874 +843c895,896 < 181 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 181 CALL_METHOD MyException.message (dynamic) -822c878,879 +847c900,901 < 182 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 182 CALL_METHOD MyException.message (dynamic) -824c881,882 +849c903,904 < 182 THROW(MyException) --- > ? STORE_LOCAL(variable exc2) > ? JUMP 34 -828c886,887 +853c908,909 < 169 THROW(Throwable) --- > ? STORE_LOCAL(variable exc2) > ? JUMP 34 -844a904,916 +869a926,938 > 34: > 184 LOAD_MODULE object Predef > 184 CONSTANT("finally") @@ -224,23 +233,23 @@ > 185 LOAD_LOCAL(variable exc2) > 185 THROW(Throwable) > -850c922 +875c944 < catch (Throwable) in ArrayBuffer(17, 18, 19, 22, 25, 27, 28, 30) starting at: 4 --- > catch (Throwable) in ArrayBuffer(17, 18, 19, 22, 25, 27, 28, 30, 32) starting at: 4 -853c925 +878c947 < catch () in ArrayBuffer(4, 5, 6, 9, 12, 17, 18, 19, 22, 25, 27, 28, 30) starting at: 3 --- > catch () in ArrayBuffer(4, 5, 6, 9, 12, 17, 18, 19, 22, 25, 27, 28, 30, 32, 33) starting at: 3 -877c949 +902c971 < locals: value args, variable result, value e, value ex6, value x4, value x5, value message, value x --- > locals: value args, variable result, value e, value ex6, value x4, value x5, value x -879c951 +904c973 < blocks: [1,2,3,6,7,8,11,14,16,17,19] --- > blocks: [1,2,3,6,7,8,11,14,16,17,19,20] -903c975,982 +928c997,1004 < 124 THROW(MyException) --- > ? STORE_LOCAL(value ex6) @@ -251,29 +260,29 @@ > 122 STORE_LOCAL(value x4) > 122 SCOPE_ENTER value x4 > 122 JUMP 7 -932,935d1010 +957,960d1032 < 127 LOAD_LOCAL(value x5) < 127 CALL_METHOD MyException.message (dynamic) < 127 STORE_LOCAL(value message) < 127 SCOPE_ENTER value message -937c1012,1013 +962c1034,1035 < 127 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 127 CALL_METHOD MyException.message (dynamic) -966c1042 +991c1064 < catch (IllegalArgumentException) in ArrayBuffer(6, 7, 8, 11, 14, 16, 17, 19) starting at: 3 --- > catch (IllegalArgumentException) in ArrayBuffer(6, 7, 8, 11, 14, 16, 17, 19, 20) starting at: 3 -990c1066 +1015c1088 < locals: value args, variable result, value ex6, value x4, value x5, value message, value x, value e --- > locals: value args, variable result, value ex6, value x4, value x5, value x, value e -992c1068 +1017c1090 < blocks: [1,2,3,4,5,8,11,15,16,17,19] --- > blocks: [1,2,3,5,8,11,15,16,17,19,20] -1016c1092,1101 +1041c1114,1123 < 148 THROW(MyException) --- > ? STORE_LOCAL(value ex6) @@ -286,49 +295,50 @@ > 154 LOAD_LOCAL(value x4) > 154 IS_INSTANCE REF(class MyException) > 154 CZJUMP (BOOL)NE ? 5 : 11 -1037,1039d1121 +1062,1064d1143 < 145 JUMP 4 < < 4: -1053,1056d1134 +1078,1081d1156 < 154 LOAD_LOCAL(value x5) < 154 CALL_METHOD MyException.message (dynamic) < 154 STORE_LOCAL(value message) < 154 SCOPE_ENTER value message -1058c1136,1137 +1083c1158,1159 < 154 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 154 CALL_METHOD MyException.message (dynamic) -1275c1354 +1300c1376 < blocks: [1,2,3,4,5,7] --- > blocks: [1,2,3,4,5,7,8] -1299c1378,1385 +1324c1400,1401 < 38 THROW(IllegalArgumentException) --- > ? STORE_LOCAL(value e) > ? JUMP 8 -> +1325a1403,1408 > 8: > 42 LOAD_MODULE object Predef > 42 CONSTANT("IllegalArgumentException") > 42 CALL_METHOD scala.Predef.println (dynamic) > 42 JUMP 2 -1346c1432 +> +1371c1454 < locals: value args, variable result, value ex6, value x4, value x5, value message, value x --- > locals: value args, variable result, value ex6, value x4, value x5, value x -1348c1434 +1373c1456 < blocks: [1,2,3,4,5,8,11,13,14,16,17,19] --- > blocks: [1,2,3,5,8,11,13,14,16,17,19,20] -1372c1458,1459 +1397c1480,1481 < 203 THROW(MyException) --- > ? STORE_LOCAL(value ex6) > ? JUMP 20 -1392c1479,1488 +1417c1501,1510 < 209 THROW(MyException) --- > ? STORE_LOCAL(value ex6) @@ -341,41 +351,41 @@ > 212 LOAD_LOCAL(value x4) > 212 IS_INSTANCE REF(class MyException) > 212 CZJUMP (BOOL)NE ? 5 : 11 -1405,1407d1500 +1430,1432d1522 < 200 JUMP 4 < < 4: -1421,1424d1513 +1446,1449d1535 < 212 LOAD_LOCAL(value x5) < 212 CALL_METHOD MyException.message (dynamic) < 212 STORE_LOCAL(value message) < 212 SCOPE_ENTER value message -1426c1515,1516 +1451c1537,1538 < 213 LOAD_LOCAL(value message) --- > ? LOAD_LOCAL(value x5) > 213 CALL_METHOD MyException.message (dynamic) -1470c1560 +1495c1582 < blocks: [1,2,3,4,5,7] --- > blocks: [1,2,3,4,5,7,8] -1494c1584,1585 +1519c1606,1607 < 58 THROW(IllegalArgumentException) --- > ? STORE_LOCAL(value e) > ? JUMP 8 -1495a1587,1592 +1520a1609,1614 > 8: > 62 LOAD_MODULE object Predef > 62 CONSTANT("RuntimeException") > 62 CALL_METHOD scala.Predef.println (dynamic) > 62 JUMP 2 > -1543c1640 +1568c1662 < blocks: [1,2,3,4] --- > blocks: [1,2,3,4,5] -1563c1660,1665 +1588c1682,1687 < 229 THROW(MyException) --- > ? JUMP 5 @@ -384,19 +394,19 @@ > ? LOAD_LOCAL(variable monitor1) > 228 MONITOR_EXIT > 228 THROW(Throwable) -1569c1671 +1594c1693 < ? THROW(Throwable) --- > 228 THROW(Throwable) -1597c1699 +1622c1721 < locals: value args, variable result, variable monitor2, variable monitorResult1 --- > locals: value exception$1, value args, variable result, variable monitor2, variable monitorResult1 -1599c1701 +1624c1723 < blocks: [1,2,3,4] --- > blocks: [1,2,3,4,5] -1622c1724,1732 +1647c1746,1754 < 245 THROW(MyException) --- > ? STORE_LOCAL(value exception$1) @@ -408,7 +418,7 @@ > ? LOAD_LOCAL(variable monitor2) > 244 MONITOR_EXIT > 244 THROW(Throwable) -1628c1738 +1653c1760 < ? THROW(Throwable) --- > 244 THROW(Throwable) diff --git a/test/files/run/t6911.scala b/test/files/run/t6911.scala new file mode 100644 index 0000000000..dd81257a96 --- /dev/null +++ b/test/files/run/t6911.scala @@ -0,0 +1,24 @@ +trait K { + case class CC(name: String) + case class DD[+A1, A2](x1: A1, x2: A2) +} + +object Test { + object Foo extends K + object Bar extends K + + val b1 = Foo.CC("b") + val b2 = Bar.CC("b") + val b3 = Foo.CC("b") + + val c1 = Foo.DD("a", 5) + val c2 = Bar.DD("a", 5) + val c3 = Foo.DD("a", 5) + + def main(args: Array[String]): Unit = { + assert(b1 != b2, ((b1, b2))) // false under 2.9, true under 2.10-RC5 + assert(b1 == b3, ((b1, b3))) + assert(c1 != c2, ((c1, c2))) + assert(c1 == c3, ((c1, c3))) + } +} -- cgit v1.2.3 From 92cf0e354e38d450bfe34d4612f9f869faec2baf Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Mon, 17 Dec 2012 23:28:08 -0500 Subject: Fix Iterator#copyToArray (fixes SI-6827). As pointed out in #scala, when using a non-zero start it's possible to get an ArrayIndexOutOfBoundsException due to an incorrect bounds check. This patch fixes this, as well as another potential bounds error, and adds test cases. Incorporates some other suggestions by Som-Snytt to ensure that callers will get useful error messages in cases where the start parameter is wrong (negative or out-of-array-bounds). Review by @som-snytt. --- src/library/scala/collection/Iterator.scala | 5 +++-- test/files/run/t6827.check | 15 ++++++++++++++ test/files/run/t6827.scala | 31 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/files/run/t6827.check create mode 100644 test/files/run/t6827.scala (limited to 'test') diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index cbf8cc4931..696bc4ab5c 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -1111,9 +1111,10 @@ trait Iterator[+A] extends TraversableOnce[A] { * $willNotTerminateInf */ def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit = { + require(start >= 0 && start < xs.length, s"start $start out of range ${xs.length}") var i = start - val end = start + math.min(len, xs.length) - while (hasNext && i < end) { + val end = start + math.min(len, xs.length - start) + while (i < end && hasNext) { xs(i) = next() i += 1 } diff --git a/test/files/run/t6827.check b/test/files/run/t6827.check new file mode 100644 index 0000000000..3a3a71c67d --- /dev/null +++ b/test/files/run/t6827.check @@ -0,0 +1,15 @@ +start at -5: java.lang.IllegalArgumentException: requirement failed: start -5 out of range 10 +start at -1: java.lang.IllegalArgumentException: requirement failed: start -1 out of range 10 +start at limit: java.lang.IllegalArgumentException: requirement failed: start 10 out of range 10 +start at limit-1: ok +first 10: ok +read all: ok +test huge len: ok +5 from 5: ok +20 from 5: ok +test len overflow: ok +start beyond limit: java.lang.IllegalArgumentException: requirement failed: start 30 out of range 10 +read 0: ok +read -1: ok +invalid read 0: java.lang.IllegalArgumentException: requirement failed: start 30 out of range 10 +invalid read -1: java.lang.IllegalArgumentException: requirement failed: start 30 out of range 10 diff --git a/test/files/run/t6827.scala b/test/files/run/t6827.scala new file mode 100644 index 0000000000..7e8918e3dc --- /dev/null +++ b/test/files/run/t6827.scala @@ -0,0 +1,31 @@ +object Test extends App { + val ns = (0 until 20) + val arr = new Array[Int](10) + + def tryit(label: String, start: Int, len: Int): Unit = { + val status = try { + val it = ns.toIterator + it.copyToArray(arr, start, len) + "ok" + } catch { + case e: Exception => e.toString + } + println("%s: %s" format (label, status)) + } + + tryit("start at -5", -5, 10) + tryit("start at -1", -1, 10) + tryit("start at limit", 10, 10) + tryit("start at limit-1", 9, 10) + tryit("first 10", 0, 10) + tryit("read all", 0, 20) + tryit("test huge len", 0, Int.MaxValue) + tryit("5 from 5", 5, 10) + tryit("20 from 5", 5, 20) + tryit("test len overflow", 5, Int.MaxValue) + tryit("start beyond limit", 30, 10) + tryit("read 0", 0, 0) + tryit("read -1", 0, -1) + tryit("invalid read 0", 30, 0) + tryit("invalid read -1", 30, -1) +} -- cgit v1.2.3