Unlike runtime errors, compilation errors prevent successful compilation, which makes them harder to test, since we can’t even compile the units tests we want to write and run to test them!

Larceny makes it possible to write those tests. Code which would normally fail compilation, for any reason (provided it parses as well-formed Scala) is permitted inside certain blocks of code, but instead of being compiled and run, instead returns a list of compilation errors, as runtime values, which are perfect for testing.

Larceny is a compiler plugin, and can be included in a compilation with the -Xplugin:larceny.jar parameter to scalac:

scalac -d bin -Xplugin:larceny.jar -classpath larceny.jar *.scala`

The compiler plugin identifies code blocks whose compilation errors should be suppressed, which are inside a demilitarize block (using any valid Scala syntax), for example:

package com.example

import larceny.*

@main def run(): Unit =
  demilitarize("Hello world".substring("5"))

    val x = 8

Here, the code inside each demilitarize block will never compile: the first, because substring takes an Int as a parameter, and the second because missingMethod is not a member of Int.

But despite this, if the Larceny plugin is enabled, then the code will compile.

Any invalid code that is not within a demilitarize block will still result in the expected compilation errors.

The compilation error from each demilitarize block will be returned (in a List) from each block. We could adjust the code to see them, like so:

@main def run(): Unit =
  val errors = demilitarize:
    "Hello world".substring("5")

    case CompileError(ordinal, message, code, position, offset) =>
      println(s"[$id] Found error '$message' in the code '$code' with offset $offset")

The parameters of CompileError need some explanation:

Taking the second example above,

  val x = 8

the message would be:

value missingMethod is not a member of Int

while the code value would be x.missingMethod (note that the surrounding println is not considered erroneous), and the offset would be 2. The value 2 is because the erroneous code begins x., but the point of the error is considered to be the m of missingMethod, which is character 2.

The error IDs are defined in the Scala compiler and correspond to an enumeration of values. For convenience, these values have been copied into the CompileErrorId enumeration, and can be accessed by the id method of CompileError.

CompileErrorId is also an extractor on CompileError, so it’s possible to write:

demilitarize(summon[Ordering[Exception]]) match
  case ErrorId(ErrorId.MissingImplicitArgumentID) => "expected"
  case _                                          => "unexpected"


Here are the details of how Larceny works. It should not be necessary to understand its implementation for normal usage, but as experimental software, it may behave unexpectedly, and this explanation may help to diagnose misbehavior.

Larceny runs on each source file before typechecking, but after parsing. Any blocks named demilitarize found in the the untyped AST will trigger a new and independent compilation of the same source file (with the same classpath, but without the Larceny plugin) from within the main compilation.

Since the demilitarize blocks should contain compile errors, this child compilation is expected to fail, but its compilation errors will be captured. Each compilation error which is positioned within a demilitarize block will be converted to static code which constructs a new CompileError instance, and inserts it into the demilitarize block, in place of entire erroneous contents.

If there are multiple demilitarize blocks in the same source file, some errors which occur in earlier phases of compilation may suppress later phases from running, and the errors from those later phases will not be captured during the first compilation. Larceny will rerun the compiler as many times as necessary to capture errors from later phases, each time removing more code which would have precluded these later phases.

The main compilation is then allowed to continue to typechecking, which will only see the CompileError constructions, not the original code. As long as there are no compilation errors outside of a demilitarize block, compilation should succeed. When the code is run, each demilitarize block will simply return a list of CompileErrors.

Testing Frameworks

Larceny should work with any Scala unit testing framework or library. For example, with Probably, we could write a compile error test with:

test(t"cannot sort data without an Ordering"):
.assert(_.startsWith("No implicit Ordering"))