Skip to content

Reflective type checking does not check refchecks #8724

@scabug

Description

@scabug

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"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions