Skip to content

Commit

Permalink
Fix module symbol recovery from NoClassDefFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Feb 8, 2024
1 parent 551eae4 commit 74619c1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/quoted/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,17 @@ object Interpreter:
if !ctx.compilationUnit.isSuspendable then None
else targetException match
case _: NoClassDefFoundError | _: ClassNotFoundException =>
val className = targetException.getMessage
if className eq null then None
val message = targetException.getMessage
if message eq null then None
else
val sym = staticRef(className.toTypeName).symbol
if (sym.isDefinedInCurrentRun) Some(sym) else None
val className = message.replace('/', '.')
val sym =
if className.endsWith("$") then staticRef(className.toTermName).symbol.moduleClass
else staticRef(className.toTypeName).symbol
// If the symbol does not a a position we assume that it came from the
// current run and it has an error
if sym.isDefinedInCurrentRun || (sym.exists && !sym.srcPos.span.exists) then Some(sym)
else None
case _ => None
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/neg-macros/i19601/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// macro.scala
package prelude
import scala.quoted.*

object Macros {
def validateInlineImpl[A: Type](assertionExpr: Expr[Assertion[A]], a: Expr[A])(using
Quotes
): Expr[Unit] = {
import quotes.reflect.*
val crashRoot = assertionExpr.value
'{ () }

}
given [A](using Type[A]): FromExpr[Assertion[A]] with {
def unapply(assertion: Expr[Assertion[A]])(using Quotes): Option[Assertion[A]] = {
import quotes.reflect.*

assertion match {
case '{ Assertion.greaterThanOrEqualTo[A](${ LiteralUnlift(value) })($_) } =>
Some(Assertion.greaterThanOrEqualTo(value)(orderingForValue(value)))
case _ => None
}
}
}

object LiteralUnlift {
def unapply[A: Type](expr: Expr[A])(using Quotes): Option[A] = expr match {
case '{ ${ Expr(int) }: Int } => Some(int)
case '{ Int.MaxValue } => Some(Int.MaxValue.asInstanceOf[A])
case '{ Int.MinValue } => Some(Int.MinValue.asInstanceOf[A])
case '{ ${ Expr(string) }: String } => Some(string)
case '{ ${ Expr(double) }: Double } => Some(double)
case '{ ${ Expr(float) }: Float } => Some(float)
case '{ ${ Expr(long) }: Long } => Some(long)
case '{ ${ Expr(short) }: Short } => Some(short)
case '{ ${ Expr(byte) }: Byte } => Some(byte)
case '{ ${ Expr(char) }: Char } => Some(char)
case _ => None
}
}

private def orderingForValue(any: Any)(using Quotes): Ordering[Any] = null.asInstanceOf[Ordering[Any]]
}
41 changes: 41 additions & 0 deletions tests/neg-macros/i19601/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// main.scala
package prelude

sealed trait Assertion[-A]:
def unary_! : Assertion[A] = ???
def apply(a: A): Either[AssertionError, Unit] = ???
object Assertion:
val anything: Assertion[Any] = ???
def greaterThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A] = ???

sealed trait AssertionError

abstract class NewtypeCustom[A] {
type Type
protected inline def validateInline(inline value: A): Unit

inline def apply(inline a1: A): Type = {
validateInline(a1)
a1.asInstanceOf[Type]
}
}
abstract class Newtype extends NewtypeCustom[Int] {
def assertion: Assertion[Int] = Assertion.anything
protected inline def validateInline(inline value: Int): Unit = ${
Macros.validateInlineImpl[Int]('assertion, 'value)
}
}


package object newtypes {
type Natural = Natural.Type
object Natural extends Newtype {
type Type <: Int

override inline def assertion = Assertion.greaterThanOrEqualTo(0)

val one: Natural = Natural(1) // triggers macro
}
}

// nopos-error: Cyclic macro dependencies in tests/pos-macros/i19601/Test.scala. Compilation stopped since no further progress can be made.

0 comments on commit 74619c1

Please sign in to comment.