Closed
Description
Compiler version
Scala 3.1.1-RC1-bin-SNAPSHOT-git-3a6a949 (16.0.1, Java OpenJDK 64-Bit Server VM)
Minimized code
The following code compiles, but produces a runtime exception:
import language.experimental.saferExceptions
trait Decoder[+T]:
def apply(): T
given Decoder[Int throws Exception] = new Decoder[Int throws Exception]:
def apply(): Int throws Exception = 1
@main def run(): Unit =
import unsafeExceptions.canThrowAny
summon[Decoder[Int throws Exception]]()
Output
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class scala.Function0 (java.lang.Integer is in module java.base of loader 'bootstrap'; scala.Function0 is in unnamed module of loader java.net.URLClassLoader @59ec2012)
at throwing.throws$package$.run(throws.scala:13)
at throwing.run.main(throws.scala:11)
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:567)
at dotty.tools.runner.RichClassLoader$.run$extension$$anonfun$1(ScalaClassLoader.scala:95)
at dotty.tools.runner.RichClassLoader$.asContext$extension(ScalaClassLoader.scala:30)
at dotty.tools.runner.RichClassLoader$.run$extension(ScalaClassLoader.scala:95)
at dotty.tools.runner.CommonRunner.run(ObjectRunner.scala:23)
at dotty.tools.runner.CommonRunner.run$(ObjectRunner.scala:13)
at dotty.tools.runner.ObjectRunner$.run(ObjectRunner.scala:48)
at dotty.tools.runner.CommonRunner.runAndCatch(ObjectRunner.scala:30)
at dotty.tools.runner.CommonRunner.runAndCatch$(ObjectRunner.scala:13)
at dotty.tools.runner.ObjectRunner$.runAndCatch(ObjectRunner.scala:48)
at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:178)
at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:206)
at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
Expectation
Successful execution.
Note that changing the given
definition to this will eliminate the exception:
given Decoder[Int throws Exception] with
def apply(): Int throws Exception = 1