Created
August 20, 2017 20:03
-
-
Save hrhino/143751ba259f70890ea9db478dda0349 to your computer and use it in GitHub Desktop.
illTyped vs c.eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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