Skip to content

Commit

Permalink
Throw a more informative error when scala_macro_library isn't used (#…
Browse files Browse the repository at this point in the history
…1680)

* Throw a more informative error when scala_macro_library isn't used

* fixup! Fixed failing tests

* fixup! Catch `ClassFormatError`s in ScalacInvoker

---------

Co-authored-by: Jaden Peterson <[email protected]>
  • Loading branch information
jadenPete and Jaden Peterson authored Feb 4, 2025
1 parent 94422d4 commit bfb9b9e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/java/io/bazel/rulesscala/scalac/scala_2/ScalacInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

//Invokes Scala 2 compiler
class ScalacInvoker{

public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs)
throws IOException, Exception{

ReportableMainClass comp = new ReportableMainClass(ops);

ScalacInvokerResults results = new ScalacInvokerResults();

results.startTime = System.currentTimeMillis();
try {
comp.process(compilerArgs);
Expand All @@ -28,7 +28,15 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
} else if (ex.toString().contains("java.lang.StackOverflowError")) {
throw new ScalacWorker.CompilationFailed("with StackOverflowError", ex);
} else if (isMacroException(ex)) {
throw new ScalacWorker.CompilationFailed("during macro expansion", ex);
String reason;

if (ex instanceof ClassFormatError) {
reason = "during macro expansion. You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target";
} else {
reason = "during macro expansion";
}

throw new ScalacWorker.CompilationFailed(reason, ex);
} else {
throw ex;
}
Expand All @@ -37,7 +45,7 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
}

results.stopTime = System.currentTimeMillis();

ConsoleReporter reporter = (ConsoleReporter) comp.getReporter();
if (reporter == null) {
// Can happen only when `ReportableMainClass::newCompiler` was not invoked,
Expand Down
24 changes: 24 additions & 0 deletions test/macros/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("//scala:scala.bzl", "scala_library", "scala_macro_library")

scala_library(
name = "incorrect-macro",
srcs = ["IdentityMacro.scala"],
)

scala_macro_library(
name = "correct-macro",
srcs = ["IdentityMacro.scala"],
)

scala_library(
name = "incorrect-macro-user",
srcs = ["MacroUser.scala"],
tags = ["manual"],
deps = [":incorrect-macro"],
)

scala_library(
name = "correct-macro-user",
srcs = ["MacroUser.scala"],
deps = [":correct-macro"],
)
9 changes: 9 additions & 0 deletions test/macros/IdentityMacro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package macros

import scala.language.experimental.macros
import scala.reflect.macros.blackbox

object IdentityMacro {
def identityMacro[A](value: A): A = macro identityMacroImpl[A]
def identityMacroImpl[A](context: blackbox.Context)(value: context.Expr[A]): context.Expr[A] = value
}
5 changes: 5 additions & 0 deletions test/macros/MacroUser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package macros

object Main {
def main(arguments: Array[String]): Unit = println(IdentityMacro.identityMacro("Hello, world!"))
}
17 changes: 17 additions & 0 deletions test/shell/test_macros.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# shellcheck source=./test_runner.sh
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
runner=$(get_test_runner "${1:-local}")

incorrect_macro_user_does_not_build() {
(! bazel build //test/macros:incorrect-macro-user) |&
grep --fixed-strings 'Build failure during macro expansion. You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target'
}

correct_macro_user_builds() {
bazel build //test/macros:correct-macro-user
}

$runner incorrect_macro_user_does_not_build
$runner correct_macro_user_builds

0 comments on commit bfb9b9e

Please sign in to comment.