Skip to content

NoSuchMethod error when matching on a private inner class #13256

Closed
@hamdiallam

Description

@hamdiallam

Compiler version: 3.0.1

Minimized code

Scastie Link: https://scastie.scala-lang.org/AL4lT7w7RIuGUBPnhoCoTA

object Monitor {
  def handle(exc: Exception, pf: PartialFunction[Throwable, Unit]): Unit = {
    pf.applyOrElse(exc, { (exc: Exception) =>
      ()
    })
  }
}

class Outer {
  private class MyExc extends Exception
  Monitor.handle(new MyExc, {
      case _: MyExc => println("here")
  })
}

object Main {
  def main(args: Array[String]): Unit = {
    new Test
  }
}

Output (Runtime, not compile time)

java.lang.NoSuchMethodError: Test$MyExc.Test$MyExc$$$outer()LTest;

Expectation

When the inner class is marked private, Dotty will not generate an outer method. Verified this in the byte code. Making MyExc public will have the outer method generated. However, even when private, the bytecode generated in the for the PartialFunctions applyOrElse needs access to the outer instance to when checking to MyExc

Works fine in Scala 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions