Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Last active January 13, 2016 18:00
Show Gist options
  • Save travisbrown/5066283 to your computer and use it in GitHub Desktop.
Save travisbrown/5066283 to your computer and use it in GitHub Desktop.
Testing for compiler errors with untyped macros.
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
@travisbrown
Copy link
Author

I should note that this is my implementation of a suggestion by Miles Sabin in response to this question.

@jeffwilde
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment