-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Labels
Description
Reflective type checking via toolbox (or in macros) does not check upper type bounds.
Consider the following code:
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
import scala.language.existentials
object Test extends App {
def typecheck(code: String) = {
val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox()
try {
toolbox.typecheck(toolbox.parse(code))
"contains no error"
} catch {
case _: Throwable => "does not typecheck"
}
}
val code = """
class A
class B[T <: A]
class C extends B[Any] """
val result = typecheck(code)
println(result)
}The code compiles and runs. The expected output would be does not typecheck, as the type argument Any does not conform to B's type parameter bound T <: A. The actual output is contains no error.
A work-around is to change the function as follows, which will result in a ToolBoxError with a meaningful error message being thrown in the try-block:
def typecheck(code: String) = {
val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox()
try {
val tree = toolbox.untypecheck(toolbox.parse(code))
toolbox.compile(tree)
"contains no error"
} catch {
case _: Throwable => "does not typecheck"
}
}Reactions are currently unavailable