question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Got ArrayIndexOutOfBoundsException in SpaceLogic instead of type error due to missing argument

See original GitHub issue

Seems pretty similar to #13088

Compiler version

3.0.2

Minimized code

import scala.util.parsing.combinator.Parsers

object P extends Parsers

def res: P.ParseResult[Int] = ???
res match { case P.Success(v) => v } // P.Success takes a 2nd argument, but we get a crash instead of a type error

Output (click arrow to expand)

[error] (run-main-0) java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
[error] java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.sub$1(Space.scala:269)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$$anonfun$4(Space.scala:274)
[error]         at scala.runtime.java8.JFunction1$mcZI$sp.apply(JFunction1$mcZI$sp.scala:17)
[error]         at scala.collection.immutable.List.exists(List.scala:395)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus(Space.scala:274)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$(Space.scala:78)
[error]         at dotty.tools.dotc.transform.patmat.SpaceEngine.minus(Space.scala:315)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus(Space.scala:244)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$(Space.scala:78)
[error]         at dotty.tools.dotc.transform.patmat.SpaceEngine.minus(Space.scala:315)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$$anonfun$2(Space.scala:250)
[error]         at scala.collection.immutable.List.map(List.scala:246)
[error]         at scala.collection.immutable.List.map(List.scala:79)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus(Space.scala:250)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$(Space.scala:78)
[error]         at dotty.tools.dotc.transform.patmat.SpaceEngine.minus(Space.scala:315)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.tryDecompose1$3(Space.scala:230)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus(Space.scala:246)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$(Space.scala:78)
[error]         at dotty.tools.dotc.transform.patmat.SpaceEngine.minus(Space.scala:315)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$$anonfun$3(Space.scala:252)
[error]         at scala.collection.LinearSeqOps.foldLeft(LinearSeq.scala:169)
[error]         at scala.collection.LinearSeqOps.foldLeft$(LinearSeq.scala:165)
[error]         at scala.collection.immutable.List.foldLeft(List.scala:79)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus(Space.scala:252)
[error]         at dotty.tools.dotc.transform.patmat.SpaceLogic.minus$(Space.scala:78)
[error]         at dotty.tools.dotc.transform.patmat.SpaceEngine.minus(Space.scala:315)
[error]         at dotty.tools.dotc.transform.patmat.SpaceEngine.checkExhaustivity(Space.scala:862)
[error]         at dotty.tools.dotc.transform.PatternMatcher.transformMatch(PatternMatcher.scala:45)
[error]         at dotty.tools.dotc.transform.MegaPhase.goMatch(MegaPhase.scala:779)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:369)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
[error]         at dotty.tools.dotc.transform.MegaPhase.mapValDef$1(MegaPhase.scala:235)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:240)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
[error]         at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
[error]         at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:362)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:256)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
[error]         at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
[error]         at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
[error]         at dotty.tools.dotc.transform.MegaPhase.mapPackage$1(MegaPhase.scala:382)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:385)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
[error]         at dotty.tools.dotc.transform.MegaPhase.transformUnit(MegaPhase.scala:448)
[error]         at dotty.tools.dotc.transform.MegaPhase.run(MegaPhase.scala:460)
[error]         at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:303)
[error]         at scala.collection.immutable.List.map(List.scala:246)
[error]         at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:304)
[error]         at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:205)
[error]         at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error]         at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error]         at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
[error]         at dotty.tools.dotc.Run.runPhases$5(Run.scala:216)
[error]         at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:224)
[error]         at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[error]         at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
[error]         at dotty.tools.dotc.Run.compileUnits(Run.scala:231)
[error]         at dotty.tools.dotc.Run.compileUnits(Run.scala:172)
[error]         at dotty.tools.repl.ReplCompiler.runCompilationUnit(ReplCompiler.scala:152)
[error]         at dotty.tools.repl.ReplCompiler.compile(ReplCompiler.scala:162)
[error]         at dotty.tools.repl.ReplDriver.compile(ReplDriver.scala:250)
[error]         at dotty.tools.repl.ReplDriver.interpret(ReplDriver.scala:213)
[error]         at dotty.tools.repl.ReplDriver.loop$1(ReplDriver.scala:147)
[error]         at dotty.tools.repl.ReplDriver.runUntilQuit$$anonfun$1(ReplDriver.scala:150)
[error]         at dotty.tools.repl.ReplDriver.withRedirectedOutput(ReplDriver.scala:169)
[error]         at dotty.tools.repl.ReplDriver.runUntilQuit(ReplDriver.scala:150)
[error]         at xsbt.ConsoleInterface.run(ConsoleInterface.java:52)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[error]         at sbt.internal.inc.AnalyzingCompiler.invoke(AnalyzingCompiler.scala:329)
[error]         at sbt.internal.inc.AnalyzingCompiler.console(AnalyzingCompiler.scala:233)
[error]         at sbt.Console.console0$1(Console.scala:64)
[error]         at sbt.Console.$anonfun$apply$5(Console.scala:74)
[error]         at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
[error]         at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:17)
[error]         at sbt.TrapExit$App.run(TrapExit.scala:258)
[error]         at java.base/java.lang.Thread.run(Thread.java:829)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dwijnandcommented, Oct 14, 2021

Reviewing again I realise now that there’s two things at play: “name-based extractions” and “product matches”. Name-based extractions were added in 2.11 and require the result to have an isEmpty and a get, i.e. a generalisation of returning Option in an extractor. Product matches are new in 3.0 and generalise case classes extractions, allowing them to unify with how custom extractors were handled (i.e. which is why case classes now synthesises an an unapply which is an identity function).

Seems to me like Typer is accepting the Success result type as a name-based extractor, which returns a single result of type T, instead of considering it a product match, with two components (T and Int). Scala 2 doesn’t have product matches so it throws a type error. I think product matches should win (i.e. Typer is wrong), which means Space wouldn’t need to change. Here’s the doc page on the pattern matching changes:

https://docs.scala-lang.org/scala3/reference/changed-features/pattern-matching.html

When it says:

The former form of unapply has higher precedence, and single match has higher precedence over name-based match.

I think it’s saying that product matches (and boolean matches) - aka “the former form” trump name-based matches.

1reaction
KacperFKorbancommented, Oct 13, 2021

Minimized it to a single file:

sealed trait Result[+T]

case class Success[+T](result: T, next: Int) extends Result[T] {
  def isEmpty = true
  def get: T = result
}

def main =
  val res: Result[Int] = ???
  res match
    case Success(v) => v

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I overcome ArrayIndexOutOfBoundException for ...
This will give an error output. The one you got, with Array exception. 2. Try running this command, that is, type a number...
Read more >
When adding new tracker have option to filter already added ones ...
Got ArrayIndexOutOfBoundsException in SpaceLogic instead of type error due to missing argument, 7, 2021-10-12, 2022-07-26.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found