bug icon indicating copy to clipboard operation
bug copied to clipboard

Errors in certain unapplications persevere through silent=true

Open scabug opened this issue 11 years ago • 3 comments

import scala.language.experimental.macros
import scala.reflect.macros.TypecheckException
import scala.reflect.macros.whitebox.Context

object Macros {
  def typecheck_impl(c: Context)(code: c.Expr[String]): c.Expr[Option[String]] = {
    import c.universe._

    val Expr(Literal(Constant(codeStr: String))) = code

    try {
      c.typecheck(c.parse(codeStr))
      c.Expr(q"None: Option[String]")
    } catch {
      case e: TypecheckException =>
        c.Expr(q"Some(${ e.toString }): Option[String]")
    }
  }

  def typecheck(code: String): Option[String] = macro typecheck_impl
}
case class Foo(i: Int, c: Char)

object Bar {
  def unapply(foo: Foo): Option[(Int, Char)] = Some((foo.i, foo.c))
}

object Test {
  println(Macros.typecheck("val Foo(x, y, z) = Foo(1, 'a')"))
  println(Macros.typecheck("val Bar(x, y, z) = Foo(1, 'a')"))
}
<macro>:1: error: too many patterns for object Bar offering (Int, Char): expected 2, found 3
val Bar(x, y, z) = Foo(1, 'a')
    ^
one error found

scabug avatar Jul 13 '14 07:07 scabug

Imported From: https://issues.scala-lang.org/browse/SI-8719?orig=1 Reporter: @xeno-by Affected Versions: 2.11.0

scabug avatar Jul 13 '14 07:07 scabug

@xeno-by said: Looks like the error in question is emitted by reporter.error as opposed to the standard mechanism of Context.issue(AbsTypeError): https://github.com/scala/scala/blob/75ac2250156dc36f072d44b82b5b82dc155691d8/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala#L106

scabug avatar Jul 13 '14 07:07 scabug

@xeno-by said: https://github.com/scala/scala/pull/3876

scabug avatar Jul 13 '14 09:07 scabug