Skip to content

Commit 316e317

Browse files
committed
Fix #6538: Make interfaces.SourcePosition resilient to missing sources
1 parent dbac6dc commit 316e317

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

compiler/src/dotty/tools/dotc/util/SourcePosition.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ extends interfaces.SourcePosition with Showable {
2222

2323
def point: Int = span.point
2424

25-
/** The line of the position, starting at 0 */
26-
def line: Int = source.offsetToLine(point)
25+
def line: Int = if (source.exists) source.offsetToLine(point) else -1
2726

2827
/** Extracts the lines from the underlying source file as `Array[Char]`*/
2928
def linesSlice: Array[Char] =
@@ -43,17 +42,16 @@ extends interfaces.SourcePosition with Showable {
4342
def beforeAndAfterPoint: (List[Int], List[Int]) =
4443
lineOffsets.partition(_ <= point)
4544

46-
/** The column of the position, starting at 0 */
47-
def column: Int = source.column(point)
45+
def column: Int = if (source.exists) source.column(point) else -1
4846

4947
def start: Int = span.start
50-
def startLine: Int = source.offsetToLine(start)
51-
def startColumn: Int = source.column(start)
48+
def startLine: Int = if (source.exists) source.offsetToLine(start) else -1
49+
def startColumn: Int = if (source.exists) source.column(start) else -1
5250
def startColumnPadding: String = source.startColumnPadding(start)
5351

5452
def end: Int = span.end
55-
def endLine: Int = source.offsetToLine(end)
56-
def endColumn: Int = source.column(end)
53+
def endLine: Int = if (source.exists) source.offsetToLine(end) else -1
54+
def endColumn: Int = if (source.exists) source.column(end) else -1
5755

5856
def withOuter(outer: SourcePosition): SourcePosition = SourcePosition(source, span, outer)
5957
def withSpan(range: Span) = SourcePosition(source, range, outer)

interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ public interface SourcePosition {
1717

1818
/** @return Offset to the point */
1919
int point();
20-
/** @return Line number of the point, starting at 0 */
20+
/** @return Line number of the point, starting at 0. -1 if the line cannot be computed */
2121
int line();
22-
/** @return Column number of the point, starting at 0 */
22+
/** @return Column number of the point, starting at 0. -1 if the column cannot be computed */
2323
int column();
2424

2525
/** @return Offset to the range start */
2626
int start();
27-
/** @return Line number of the range start, starting at 0 */
27+
/** @return Line number of the range start, starting at 0. -1 if the line cannot be computed */
2828
int startLine();
29-
/** @return Column number of the range start, starting at 0 */
29+
/** @return Column number of the range start, starting at 0. -1 if the column cannot be computed */
3030
int startColumn();
3131

3232
/** @return Offset to the range end */
3333
int end();
34-
/** @return Line number of the range end, starting at 0 */
34+
/** @return Line number of the range end, starting at 0. -1 if the line cannot be computed */
3535
int endLine();
36-
/** @return Column number of the range end, starting at 0 */
36+
/** @return Column number of the range end, starting at 0. -1 if the column cannot be computed */
3737
int endColumn();
3838

3939
/** The source file corresponding to this position.

sbt-bridge/src/xsbt/DelegatingReporter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public Optional<String> sourcePath() {
8686
return Optional.ofNullable(src.file().path());
8787
}
8888
public Optional<Integer> line() {
89-
return Optional.of(pos.line());
89+
int line = pos.line();
90+
if (line == -1) return Optional.empty();
91+
else return Optional.of(line);
9092
}
9193
public String lineContent() {
9294
String line = pos.lineContent();

0 commit comments

Comments
 (0)