Skip to content

Commit

Permalink
bump compiler-interface and capture the diagnosticCode
Browse files Browse the repository at this point in the history
NOTE: It's important that in `Problem` we don't construct the actual
`DiagnosticCode` _unless_ the method is called to get it. By doing this we
keep compatibility ensuring that older build tools can still use the
bridge just fine. That's what the `sbt-test/sbt-bridge` tests ensuring
that this is still usable by an older tool using the old `Problem`. This
then unlocks forwarding the diagnostic code on for tools to use.

refs: scala#14904
  • Loading branch information
ckipp01 committed Jul 28, 2022
1 parent 634c580 commit aabe2aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Expand Up @@ -25,6 +25,6 @@ object Dependencies {
"com.vladsch.flexmark" % "flexmark-ext-yaml-front-matter" % flexmarkVersion,
)

val newCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.4.3"
val newCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.7.1"
val oldCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.3.5"
}
3 changes: 2 additions & 1 deletion sbt-bridge/src/dotty/tools/xsbt/DelegatingReporter.java
Expand Up @@ -39,12 +39,13 @@ public void doReport(Diagnostic dia, Context ctx) {
StringBuilder rendered = new StringBuilder();
rendered.append(messageAndPos(dia, ctx));
Message message = dia.msg();
String diagnosticCode = String.valueOf(message.errorId().errorNumber());
boolean shouldExplain = Diagnostic.shouldExplain(dia, ctx);
if (shouldExplain && !message.explanation().isEmpty()) {
rendered.append(explanation(message, ctx));
}

delegate.log(new Problem(position, message.msg(), severity, rendered.toString()));
delegate.log(new Problem(position, message.msg(), severity, rendered.toString(), diagnosticCode));
}

private static Severity severityOf(int level) {
Expand Down
23 changes: 23 additions & 0 deletions sbt-bridge/src/dotty/tools/xsbt/DiagnosticCode.java
@@ -0,0 +1,23 @@
package dotty.tools.xsbt;

import java.util.Optional;

final public class DiagnosticCode implements xsbti.DiagnosticCode {
private final String _code;
private final Optional<String> _explanation;

public DiagnosticCode(String code, Optional<String> explanation) {
super();
this._code = code;
this._explanation = explanation;
}

public String code() {
return _code;
}

public Optional<String> explanation() {
return _explanation;
}

}
15 changes: 13 additions & 2 deletions sbt-bridge/src/dotty/tools/xsbt/Problem.java
Expand Up @@ -9,13 +9,15 @@ final public class Problem implements xsbti.Problem {
private final String _message;
private final Severity _severity;
private final Optional<String> _rendered;
private final String _diagnosticCode;

public Problem(Position position, String message, Severity severity, String rendered) {
public Problem(Position position, String message, Severity severity, String rendered, String diagnosticCode) {
super();
this._position = position;
this._message = message;
this._severity = severity;
this._rendered = Optional.of(rendered);
this._diagnosticCode = diagnosticCode;
}

public String category() {
Expand All @@ -38,8 +40,17 @@ public Optional<String> rendered() {
return _rendered;
}

public Optional<xsbti.DiagnosticCode> diagnosticCode() {
// NOTE: It's important for compatibility that we only construct a
// DiagnosticCode here to maintain compatibility with older versions of
// zinc while using this newer version of the compiler. If we would
// contstruct it earlier, you'd end up with ClassNotFoundExceptions for
// DiagnosticCode.
return Optional.of(new DiagnosticCode(_diagnosticCode, Optional.empty()));
}

@Override
public String toString() {
return "Problem(" + _position + ", " + _message + ", " + _severity + ", " + _rendered + ")";
return "Problem(" + _position + ", " + _message + ", " + _severity + ", " + _rendered + ", " + _diagnosticCode + ")";
}
}
5 changes: 5 additions & 0 deletions sbt-test/compilerReporter/simple/project/Reporter.scala
Expand Up @@ -36,6 +36,11 @@ object Reporter {
assert(line.isPresent() == true)
assert(line.get() == 9)

val diagnosticCode = mainProblem.diagnosticCode()
assert(diagnosticCode.isPresent() == true)
val code = diagnosticCode.get()
assert(diagnosticCode.get().code() == "6")

val pointer = mainProblem.position().pointer()
assert(pointer.isPresent() == true)
assert(pointer.get() == 10)
Expand Down

0 comments on commit aabe2aa

Please sign in to comment.