Skip to content
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

"NoSuchElementException: key not found" with unusual partial function #9391

Closed
szeiger opened this issue Jul 19, 2020 · 1 comment · Fixed by #10979
Closed

"NoSuchElementException: key not found" with unusual partial function #9391

szeiger opened this issue Jul 19, 2020 · 1 comment · Fixed by #10979
Milestone

Comments

@szeiger
Copy link
Contributor

szeiger commented Jul 19, 2020

In 0.26.0-bin-20200718-c753ca3-NIGHTLY and 0.25.0-RC2 this crashes the compiler:

(f => scala.util.Try(f(0)) match { case v => v }): PartialFunction[Any => Any, Any]
java.util.NoSuchElementException: key not found: method $anonfun while traversing x
java.util.NoSuchElementException: key not found: method $anonfun while traversing x.apply
java.util.NoSuchElementException: key not found: method $anonfun while traversing x.apply(scala.Int.box(0))
java.util.NoSuchElementException: key not found: method $anonfun while traversing def $anonfun(): Object = x.apply(scala.Int.box(0))
java.util.NoSuchElementException: key not found: method $anonfun while traversing {
  def $anonfun(): Object = x.apply(scala.Int.box(0))
  closure($anonfun:Function0)
}
...

It works in 2.11/2.12/2.13. Minimized from this real use case: https://github.com/slick/slick/blob/eecc74fa1ba8e477e6fbd991b5b9bbb87b8f3918/slick/src/main/scala/slick/jdbc/SQLiteProfile.scala#L136-L138

@griggt
Copy link
Contributor

griggt commented Sep 19, 2020

Minimized further:

def g(x: => Any): Any = x
val a: PartialFunction[Any => Any, Any] = (f => g(f(0)) match { case v => v })

With dotty 0.27.0-RC1:

[info] Compiling 1 Scala source to /src/dotty-issues/i9391/target/scala-0.27/classes ...
java.util.NoSuchElementException: key not found: method $anonfun while traversing x
java.util.NoSuchElementException: key not found: method $anonfun while traversing x.apply
java.util.NoSuchElementException: key not found: method $anonfun while traversing x.apply(scala.Int.box(0))
java.util.NoSuchElementException: key not found: method $anonfun while traversing def $anonfun(): Object = x.apply(scala.Int.box(0))
java.util.NoSuchElementException: key not found: method $anonfun while traversing {
...

The trigger seems to be the by-name parameter to g, as this modified example illustrates:

def g(x: => Any): Any = x
def h(x: Any): Any = x

//val a: PartialFunction[Any => Any, Any] = (f => g(f(0)) match { case v => v })    // error
val a: PartialFunction[Any => Any, Any] = (f => h(f(0)) match { case v => v })      // OK
[info] Compiling 1 Scala source to /src/dotty-issues/i9391/target/scala-0.27/classes ...
[success] Total time: 2 s, completed Sep 19, 2020, 11:52:02 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants