Skip to content

Commit cb89f8f

Browse files
committed
bump compiler-interface and capture the diagnosticCode
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: #14904
1 parent 634c580 commit cb89f8f

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

project/Dependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ object Dependencies {
2525
"com.vladsch.flexmark" % "flexmark-ext-yaml-front-matter" % flexmarkVersion,
2626
)
2727

28-
val newCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.4.3"
28+
val newCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.7.0"
2929
val oldCompilerInterface = "org.scala-sbt" % "compiler-interface" % "1.3.5"
3030
}

sbt-bridge/src/dotty/tools/xsbt/DelegatingReporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ public void doReport(Diagnostic dia, Context ctx) {
3939
StringBuilder rendered = new StringBuilder();
4040
rendered.append(messageAndPos(dia, ctx));
4141
Message message = dia.msg();
42+
String diagnosticCode = String.valueOf(message.errorId().errorNumber());
4243
boolean shouldExplain = Diagnostic.shouldExplain(dia, ctx);
4344
if (shouldExplain && !message.explanation().isEmpty()) {
4445
rendered.append(explanation(message, ctx));
4546
}
4647

47-
delegate.log(new Problem(position, message.msg(), severity, rendered.toString()));
48+
delegate.log(new Problem(position, message.msg(), severity, rendered.toString(), diagnosticCode));
4849
}
4950

5051
private static Severity severityOf(int level) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dotty.tools.xsbt;
2+
3+
import java.util.Optional;
4+
5+
final public class DiagnosticCode implements xsbti.DiagnosticCode {
6+
private final String _code;
7+
private final Optional<String> _explanation;
8+
9+
public DiagnosticCode(String code, Optional<String> explanation) {
10+
super();
11+
this._code = code;
12+
this._explanation = explanation;
13+
}
14+
15+
public String code() {
16+
return _code;
17+
}
18+
19+
public Optional<String> explanation() {
20+
return _explanation;
21+
}
22+
23+
}

sbt-bridge/src/dotty/tools/xsbt/Problem.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ final public class Problem implements xsbti.Problem {
99
private final String _message;
1010
private final Severity _severity;
1111
private final Optional<String> _rendered;
12+
private final String _diagnosticCode;
1213

13-
public Problem(Position position, String message, Severity severity, String rendered) {
14+
public Problem(Position position, String message, Severity severity, String rendered, String diagnosticCode) {
1415
super();
1516
this._position = position;
1617
this._message = message;
1718
this._severity = severity;
1819
this._rendered = Optional.of(rendered);
20+
this._diagnosticCode = diagnosticCode;
1921
}
2022

2123
public String category() {
@@ -38,8 +40,17 @@ public Optional<String> rendered() {
3840
return _rendered;
3941
}
4042

43+
public Optional<xsbti.DiagnosticCode> diagnosticCode() {
44+
// NOTE: It's important for compatibility that we only construct a
45+
// DiagnosticCode here to maintain compatibility with older versions of
46+
// zinc while using this newer version of the compiler. If we would
47+
// contstruct it earlier, you'd end up with ClassNotFoundExceptions for
48+
// DiagnosticCode.
49+
return Optional.of(new DiagnosticCode(_diagnosticCode, Optional.empty()));
50+
}
51+
4152
@Override
4253
public String toString() {
43-
return "Problem(" + _position + ", " + _message + ", " + _severity + ", " + _rendered + ")";
54+
return "Problem(" + _position + ", " + _message + ", " + _severity + ", " + _rendered + ", " + _diagnosticCode + ")";
4455
}
4556
}

sbt-test/compilerReporter/simple/project/Reporter.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ object Reporter {
3636
assert(line.isPresent() == true)
3737
assert(line.get() == 9)
3838

39+
val diagnosticCode = mainProblem.diagnosticCode()
40+
assert(diagnosticCode.isPresent() == true)
41+
val code = diagnosticCode.get()
42+
assert(diagnosticCode.get().code() == "6")
43+
3944
val pointer = mainProblem.position().pointer()
4045
assert(pointer.isPresent() == true)
4146
assert(pointer.get() == 10)

0 commit comments

Comments
 (0)