Skip to content

Runtime java.lang.AbstractMethodError when combining opaque type erasure, value class erasure, and generic type erasure #17281

Open
@dzanot

Description

@dzanot

Compiler version

3.3.0

Minimized code

https://scastie.scala-lang.org/B5fP4EPnSJeMRIsI84IeQQ

type IO[T] = T
opaque type NonNegLong = Long

object NonNegLong {
  def apply(l: Long): Option[NonNegLong] = if (l < 0) None else Some(l)
}

case class Baz(value: NonNegLong) extends AnyVal

trait InnerThing[F[_]]{
  def withBaz(baz: Baz): F[Unit]
}

object Main {
  val testInnerImpl = new InnerThing[IO] {
    override def withBaz(baz: Baz): IO[Unit] = println("withBaz")
  }

  @main def run = {
    println("Hello")
    NonNegLong(1l).map(Baz.apply(_)).fold(println("Nope"))(testInnerImpl.withBaz(_))
    println("Goodbye")
  }
}

Output

Hello

With excaption

java.lang.AbstractMethodError: Receiver class Main$$anon$1 does not define or inherit an implementation of the resolved method 'abstract java.lang.Object withBaz(long)' of interface InnerThing.
	at Main$.run$$anonfun$3(main.scala:22)
	at Main$.run$$anonfun$adapted$3(main.scala:22)
	at scala.Option.fold(Option.scala:263)
	at Main$.run(main.scala:22)
	at run.main(main.scala:20)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	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:21)
	at sbt.ScastieTrapExit$App.run(ScastieTrapExit.scala:258)
	at java.base/java.lang.Thread.run(Thread.java:833)

Expectation

No AbstractMethodError. This works as expected if you do any one of:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions