Skip to content

Commit 6949ffc

Browse files
committed
feat: update Problem to account for related information and code
This PR makes changes to the existing `xsbti.Problem` to account for an optional diagnostic code that the compiler can return for a given diagnostic and also related information. Given a piece of code like: ```scala try {} ``` You'll receive the following: ``` -- [E002] Syntax Warning: /Users/ckipp/Documents/scala-workspace/dotty-error-index/examples/002_EmptyCatchAndFinallyBlockID.scala:3:2 3 | try {} | ^^^^^^ | A try without catch or finally is equivalent to putting | its body in a block; no exceptions are handled. ``` The `E002` here is the actual code. Right now there would be no description. Some diagnostics have multiple positions that they need to represent. You can see an example of this [here](scala/scala3#14002) in Dotty with the use of inlining. Instead of needing to rely on including all of that information in the diagnostic message it can now be extracted out into a `DiagnosticRelatedInformation`. These changes reference the conversation in sbt#6868
1 parent d065f38 commit 6949ffc

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* sbt
3+
* Copyright 2011 - 2018, Lightbend, Inc.
4+
* Copyright 2008 - 2010, Mark Harrah
5+
* Licensed under Apache License 2.0 (see LICENSE)
6+
*/
7+
8+
package xsbti;
9+
10+
import java.util.Optional;
11+
12+
/**
13+
* A DiagnosticCode is a unique identifier that the compiler can associate with a diagnostic. This
14+
* is useful for tools to be able to quickly identify what diagnostic is being reported without
15+
* having to rely on parsing the actual diagnostic message, which might not be stable.
16+
*/
17+
public interface DiagnosticCode {
18+
/** The unique code. This is typically in the format of E000 */
19+
String code();
20+
21+
/** Possible explanation to explain the meaning of the code */
22+
Optional<String> explanation();
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* sbt
3+
* Copyright 2011 - 2018, Lightbend, Inc.
4+
* Copyright 2008 - 2010, Mark Harrah
5+
* Licensed under Apache License 2.0 (see LICENSE)
6+
*/
7+
8+
package xsbti;
9+
10+
/**
11+
* Related information for a given diagnostic. At times this can be another place in your code
12+
* contributing to the diagnostic or just relevant code relating to the diagnostic.
13+
*/
14+
public interface DiagnosticRelatedInformation {
15+
/** Position of the related information */
16+
Position position();
17+
18+
/** Message indicating why this related information is attached to the diagnostic. */
19+
String message();
20+
}

internal/util-interface/src/main/java/xsbti/Problem.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package xsbti;
99

10+
import java.util.Collections;
11+
import java.util.List;
1012
import java.util.Optional;
1113

1214
public interface Problem {
@@ -26,4 +28,24 @@ public interface Problem {
2628
default Optional<String> rendered() {
2729
return Optional.empty();
2830
}
31+
32+
/**
33+
* The unique code attached to the diagnostic being reported.
34+
*
35+
* <p>NOTE: To avoid breaking compatibility we provide a default to account for older Scala
36+
* version that do not have codes.
37+
*/
38+
default Optional<DiagnosticCode> diagnosticCode() {
39+
return Optional.empty();
40+
}
41+
42+
/**
43+
* The possible releated information for the diagnostic being reported.
44+
*
45+
* <p>NOTE: To avoid breaking compatibility we provide a default to account for older Scala
46+
* version that do not have the concept of "related information".
47+
*/
48+
default List<DiagnosticRelatedInformation> diagnosticRelatedInforamation() {
49+
return Collections.emptyList();
50+
}
2951
}

0 commit comments

Comments
 (0)