Last active
January 13, 2016 18:00
-
-
Save travisbrown/5066283 to your computer and use it in GitHub Desktop.
Testing for compiler errors with untyped macros.
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> import scala.language.experimental.macros | |
import scala.language.experimental.macros | |
scala> import scala.reflect.macros.{ Context, TypecheckException } | |
import scala.reflect.macros.{Context, TypecheckException} | |
scala> object NoncompilationTests { | |
| def compiles(code: _): Boolean = macro compiles_impl | |
| def compiles_impl(c: Context)(code: c.Tree) = c.literal( | |
| try { | |
| c typeCheck code | |
| true | |
| } catch { | |
| case _: TypecheckException => false | |
| } | |
| ) | |
| } | |
defined object NoncompilationTests | |
scala> import shapeless._ | |
import shapeless._ | |
scala> NoncompilationTests compiles HNil.length | |
res0: Boolean = true | |
scala> NoncompilationTests compiles HNil.head | |
res1: Boolean = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anybody looking at this code should keep in mind that it relies on an experimental macro-paradise feature—untyped macros—that never made it into any released version of Scala (as noted by Miles Sabin). The best alternative nowadays is compile-checking code fragments represented as free-form strings. Test frameworks generally include this functionality, e.g. http://www.scalatest.org/user_guide/using_matchers#checkingThatCodeDoesNotCompile or
https://etorreborre.github.io/specs2/guide/SPECS2-3.6/org.specs2.guide.Matchers.html#typecheck-matchers