Skip to content

Commit bfb9b9e

Browse files
jadenPeteJaden Peterson
and
Jaden Peterson
authored
Throw a more informative error when scala_macro_library isn't used (#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]>
1 parent 94422d4 commit bfb9b9e

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

src/java/io/bazel/rulesscala/scalac/scala_2/ScalacInvoker.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
//Invokes Scala 2 compiler
1313
class ScalacInvoker{
14-
14+
1515
public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs)
1616
throws IOException, Exception{
1717

1818
ReportableMainClass comp = new ReportableMainClass(ops);
1919

2020
ScalacInvokerResults results = new ScalacInvokerResults();
21-
21+
2222
results.startTime = System.currentTimeMillis();
2323
try {
2424
comp.process(compilerArgs);
@@ -28,7 +28,15 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
2828
} else if (ex.toString().contains("java.lang.StackOverflowError")) {
2929
throw new ScalacWorker.CompilationFailed("with StackOverflowError", ex);
3030
} else if (isMacroException(ex)) {
31-
throw new ScalacWorker.CompilationFailed("during macro expansion", ex);
31+
String reason;
32+
33+
if (ex instanceof ClassFormatError) {
34+
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";
35+
} else {
36+
reason = "during macro expansion";
37+
}
38+
39+
throw new ScalacWorker.CompilationFailed(reason, ex);
3240
} else {
3341
throw ex;
3442
}
@@ -37,7 +45,7 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
3745
}
3846

3947
results.stopTime = System.currentTimeMillis();
40-
48+
4149
ConsoleReporter reporter = (ConsoleReporter) comp.getReporter();
4250
if (reporter == null) {
4351
// Can happen only when `ReportableMainClass::newCompiler` was not invoked,

test/macros/BUILD

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("//scala:scala.bzl", "scala_library", "scala_macro_library")
2+
3+
scala_library(
4+
name = "incorrect-macro",
5+
srcs = ["IdentityMacro.scala"],
6+
)
7+
8+
scala_macro_library(
9+
name = "correct-macro",
10+
srcs = ["IdentityMacro.scala"],
11+
)
12+
13+
scala_library(
14+
name = "incorrect-macro-user",
15+
srcs = ["MacroUser.scala"],
16+
tags = ["manual"],
17+
deps = [":incorrect-macro"],
18+
)
19+
20+
scala_library(
21+
name = "correct-macro-user",
22+
srcs = ["MacroUser.scala"],
23+
deps = [":correct-macro"],
24+
)

test/macros/IdentityMacro.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package macros
2+
3+
import scala.language.experimental.macros
4+
import scala.reflect.macros.blackbox
5+
6+
object IdentityMacro {
7+
def identityMacro[A](value: A): A = macro identityMacroImpl[A]
8+
def identityMacroImpl[A](context: blackbox.Context)(value: context.Expr[A]): context.Expr[A] = value
9+
}

test/macros/MacroUser.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package macros
2+
3+
object Main {
4+
def main(arguments: Array[String]): Unit = println(IdentityMacro.identityMacro("Hello, world!"))
5+
}

test/shell/test_macros.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# shellcheck source=./test_runner.sh
2+
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
3+
. "${dir}"/test_runner.sh
4+
. "${dir}"/test_helper.sh
5+
runner=$(get_test_runner "${1:-local}")
6+
7+
incorrect_macro_user_does_not_build() {
8+
(! bazel build //test/macros:incorrect-macro-user) |&
9+
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'
10+
}
11+
12+
correct_macro_user_builds() {
13+
bazel build //test/macros:correct-macro-user
14+
}
15+
16+
$runner incorrect_macro_user_does_not_build
17+
$runner correct_macro_user_builds

0 commit comments

Comments
 (0)