Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw a more informative error when scala_macro_library isn't used #1680

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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