diff --git a/staging/src/scala/quoted/staging/QuoteDriver.scala b/staging/src/scala/quoted/staging/QuoteDriver.scala index 8de0cd218b23..3cf8744f001d 100644 --- a/staging/src/scala/quoted/staging/QuoteDriver.scala +++ b/staging/src/scala/quoted/staging/QuoteDriver.scala @@ -53,7 +53,19 @@ private class QuoteDriver(appClassloader: ClassLoader) extends Driver: val method = clazz.getMethod("apply") val inst = clazz.getConstructor().newInstance() - method.invoke(inst).asInstanceOf[T] + try method.invoke(inst).asInstanceOf[T] + catch case ex: java.lang.reflect.InvocationTargetException => + ex.getCause match + case ex: java.lang.NoClassDefFoundError => + throw new Exception( + s"""`scala.quoted.staging.run` failed to load a class. + |The classloader used for the `staging.Compiler` instance might not be the correct one. + |Make sure that this classloader is the one that loaded the missing class. + |Note that the classloader that loads the standard library might not be the same as + |the one that loaded the application classes.""".stripMargin, + ex) + + case _ => throw ex end match end run diff --git a/tests/run-staging/i19170b.scala b/tests/run-staging/i19170b.scala new file mode 100644 index 000000000000..7fee2d2bc822 --- /dev/null +++ b/tests/run-staging/i19170b.scala @@ -0,0 +1,16 @@ +import scala.quoted.* + +given staging.Compiler = + staging.Compiler.make(getClass.getClassLoader) // warn: Suspicious top-level unqualified call to getClass + +class A(i: Int) + +def f(i: Expr[Int])(using Quotes): Expr[A] = { '{ new A($i) } } + +@main def Test = { + try + val g: Int => A = staging.run { '{ (i: Int) => ${ f('{i}) } } } + println(g(3)) + catch case ex: Exception => + assert(ex.getMessage().startsWith("`scala.quoted.staging.run` failed to load a class.")) +}