Skip to content

Commit 00fb40f

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

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extends interfaces.SourcePosition with Showable {
2323
def point: Int = span.point
2424

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

2828
/** Extracts the lines from the underlying source file as `Array[Char]`*/
2929
def linesSlice: Array[Char] =
@@ -44,16 +44,16 @@ extends interfaces.SourcePosition with Showable {
4444
lineOffsets.partition(_ <= point)
4545

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

4949
def start: Int = span.start
50-
def startLine: Int = source.offsetToLine(start)
51-
def startColumn: Int = source.column(start)
50+
def startLine: Int = if (source.exists) source.offsetToLine(start) else -1
51+
def startColumn: Int = if (source.exists) source.column(start) else -1
5252
def startColumnPadding: String = source.startColumnPadding(start)
5353

5454
def end: Int = span.end
55-
def endLine: Int = source.offsetToLine(end)
56-
def endColumn: Int = source.column(end)
55+
def endLine: Int = if (source.exists) source.offsetToLine(end) else -1
56+
def endColumn: Int = if (source.exists) source.column(end) else -1
5757

5858
def withOuter(outer: SourcePosition): SourcePosition = SourcePosition(source, span, outer)
5959
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)