Skip to content

ClassCastException running staged code that uses typeOf #7919

Closed
@tellurion

Description

@tellurion

minimized code

import scala.quoted._
import scala.quoted.staging.{run, withQuoteContext, Toolbox}

object Main {
  given Toolbox = Toolbox.make(getClass.getClassLoader)

  def main(args: Array[String]): Unit = {
    println(staged[Int])
  }

  def staged[T] = run { (given qctx: QuoteContext) =>
    import qctx.tasty.{_, given}
    given typeT: quoted.Type[T]
    val tTypeTree = typeT.unseal // class cast exception
    val tt = typeOf[T] // also class cast exception

    '{ "in staged" }
  }
}
Execution output
[info] Running Main
[error] (run-main-a) java.lang.ClassCastException: Main$typeT$1$ cannot be cast to dotty.tools.dotc.tastyreflect.TreeType
[error] java.lang.ClassCastException: Main$typeT$1$ cannot be cast to dotty.tools.dotc.tastyreflect.TreeType
[error] 	at dotty.tools.dotc.core.quoted.PickledQuotes$.quotedTypeToTree(PickledQuotes.scala:48)
[error] 	at dotty.tools.dotc.tastyreflect.ReflectionCompilerInterface.QuotedType_unseal(ReflectionCompilerInterface.scala:1777)
[error] 	at dotty.tools.dotc.tastyreflect.ReflectionCompilerInterface.QuotedType_unseal(ReflectionCompilerInterface.scala:1776)
[error] 	at scala.tasty.reflect.QuotedOps$QuotedTypeAPI.unseal(QuotedOps.scala:19)
[error] 	at Main$.staged$$anonfun$1(Main.scala:14)
[error] 	at scala.quoted.staging.package$.run$$anonfun$1(staging.scala:19)
[error] 	at scala.quoted.staging.QuoteCompiler$QuotedFrontend.runOn$$anonfun$1(QuoteCompiler.scala:71)
[error] 	at scala.collection.immutable.List.flatMap(List.scala:265)
[error] 	at scala.quoted.staging.QuoteCompiler$QuotedFrontend.runOn(QuoteCompiler.scala:84)
[error] 	at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:161)
[error] 	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] 	at dotty.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:171)
[error] 	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:179)
[error] 	at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error] 	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:65)
[error] 	at dotty.tools.dotc.Run.compileUnits(Run.scala:186)
[error] 	at dotty.tools.dotc.Run.compileUnits(Run.scala:128)
[error] 	at scala.quoted.staging.QuoteCompiler$ExprRun.compileExpr(QuoteCompiler.scala:104)
[error] 	at scala.quoted.staging.QuoteDriver.run(QuoteDriver.scala:37)
[error] 	at scala.quoted.staging.Toolbox$$anon$1.run(Toolbox.scala:36)
[error] 	at scala.quoted.staging.package$.run(staging.scala:19)
[error] 	at Main$.staged(Main.scala:18)
[error] 	at Main$.main(Main.scala:8)
[error] 	at Main.main(Main.scala)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] 	at java.lang.reflect.Method.invoke(Method.java:498)
[error] Nonzero exit code: 1
[error] (Compile / run) Nonzero exit code: 1

The code compiles without error and throws exception when run.
Expectation is that it should either not compile if unsupported, or run without error.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions