Skip to content

Range with size over Int.MaxValue can be intialized, but crashes upon use #16600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MarkovChainmail opened this issue Dec 29, 2022 · 1 comment
Labels

Comments

@MarkovChainmail
Copy link

Compiler version

3.2.0

Minimized code

val range = BigInt("0") until BigInt("1000000000000")
println(range.take(10).toList)

Output

java.lang.ExceptionInInitializerError
	at Main$.<clinit>(main.scala:8)
	at Main.main(main.scala)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at sbt.Run.invokeMain(Run.scala:143)
	at sbt.Run.execute$1(Run.scala:93)
	at sbt.Run.$anonfun$runWithLoader$5(Run.scala:120)
	at sbt.Run$.executeSuccess(Run.scala:186)
	at sbt.Run.runWithLoader(Run.scala:120)
	at sbt.Run.run(Run.scala:127)
	at com.olegych.scastie.sbtscastie.SbtScastiePlugin$$anon$1.$anonfun$run$1(SbtScastiePlugin.scala:38)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:17)
	at sbt.ScastieTrapExit$App.run(ScastieTrapExit.scala:258)
	at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.IllegalArgumentException: More than Int.MaxValue elements.
	at scala.collection.immutable.NumericRange$.check$1(NumericRange.scala:324)
	at scala.collection.immutable.NumericRange$.count(NumericRange.scala:334)
	at scala.collection.immutable.NumericRange.length$lzycompute(NumericRange.scala:75)
	at scala.collection.immutable.NumericRange.length(NumericRange.scala:75)
	at scala.collection.immutable.NumericRange.take(NumericRange.scala:143)
	at Playground$.<clinit>(main.scala:4)
	... 17 more

See also https://scastie.scala-lang.org/BalmungSan/J36g8PSjTuy0i284aMMw1A/1

Expectation

https://scala-lang.org/api/3.x/scala/collection/immutable/Range.html
Documentation says an exception will be thrown when a collection is created with too many elements, or asked to index beyond Int.MaxValue will thrown an exception. However, shown code example arguably does not access elements beyond Int.MaxValue?

Scala2 bug repository suggests an IllegalArgumentException should be thrown if the initialized range is too large scala/bug#4308 scala/bug#4042

@MarkovChainmail MarkovChainmail added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 29, 2022
@prolativ
Copy link
Contributor

That will require an upstream fix in scala 2.13 standard library. There already seems to be an issue for that scala/bug#12706

@prolativ prolativ added area:library Standard library and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 30, 2022
@prolativ prolativ closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants