Skip to content

Instantly share code, notes, and snippets.

@hrhino
Created August 20, 2017 20:03
Show Gist options
  • Save hrhino/143751ba259f70890ea9db478dda0349 to your computer and use it in GitHub Desktop.
Save hrhino/143751ba259f70890ea9db478dda0349 to your computer and use it in GitHub Desktop.
illTyped vs c.eval
scala> def snc_impl(c: blackbox.Context)(s: c.Expr[String]): c.Tree = {
| import c.universe._
| val Literal(Constant(code:String)) = s.tree; val wrapped = q""" { lazy val ignore = { ${c.parse(code)} }; () } """
| val err = try { c.eval(c.Expr[Unit](wrapped)); None } catch { case e: Throwable => Some(e) }
| err match {
| case Some(e) => c.info(NoPosition, e.getMessage, true); q"()"
| case None => c.abort(NoPosition, "unexpected success")
| }
| }
snc_impl: (c: scala.reflect.macros.blackbox.Context)(s: c.Expr[String])c.Tree
scala> def snc(s: String): Unit = macro snc_impl
defined term macro snc: (s: String)Unit
scala> snc ( """ class Bah { type R; def foo(r: AnyRef) = ???; def foo(r: R) = ??? } """ )
reflective compilation has failed:
double definition:
def foo(r: Object): Nothing at line 1 and
def foo(r: Bah.this.R): Nothing at line 1
have same type after erasure: (r: Object)Nothing
scala> snc ( """ class Bah { type R; def foo(r: AnyRef) = ??? } """ )
<console>:15: error: unexpected success
snc ( """ class Bah { type R; def foo(r: AnyRef) = ??? } """ )
^
scala> snc ( """ { class Boo { type R; def foo(r: AnyRef) = ???; def foo(r: R) = ??? } ; throw new Error("") } """ )
reflective compilation has failed:
double definition:
def foo(r: Object): Nothing at line 1 and
def foo(r: Boo.this.R): Nothing at line 1
have same type after erasure: (r: Object)Nothing
scala> class Bah { type R; def foo(r: AnyRef) = ???; def foo(r: R) = ??? }
<console>:42: error: double definition:
def foo(r: AnyRef): Nothing at line 42 and
def foo(r: Bah.this.R): Nothing at line 42
have same type after erasure: (r: Object)Nothing
class Bah { type R; def foo(r: AnyRef) = ???; def foo(r: R) = ??? }
^
scala> import shapeless.test.illTyped
import shapeless.test.illTyped
scala> illTyped("""class Bah { type R; def foo(r: AnyRef) = ???; def foo(r: R) = ??? }""")
<console>:44: error: Type-checking succeeded unexpectedly.
Expected some error.
illTyped("""class Bah { type R; def foo(r: AnyRef) = ???; def foo(r: R) = ??? }""")
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment