scala/scala — pull request #2477 by ihji (2013-04-30 16:44:18+00:00)
Title: SI-6736, SI-6747 sort out Range boundary problems

When Range contains more than Int.MaxValue elements,
it should throw IllegalArgumentException before every
meaningful operation on it. It seems that the only
reason of delaying this exception is for allowing "by"
operation.

> (Int.MinValue to 0).contains(4) # should throw exception
> (Int.MinValue to 0 by 3).contains(4) # okay

The source of all evil here is irregular use of
`numRangeElements`. It returns -1 if Range object is
invalid. Some use sites of `numRangeElements` properly check
its validity before using it, but some of them miss this
check and cause the integer overflow problems (for example,
in `lastElements`).

I think better strategy is to make `numRangeElements` lazy
value and throw exception inside itself when initialized
but turned to be invalid.

Additionally, because they (`numRangeElements`,`lastElement`,
`terminalElement`) are completely undocumented and also have
better public alternatives (`last`,`length`), I propose to
make them private.