Skip to content

Commit 9e46225

Browse files
Merge pull request #6573 from dotty-staging/fix-#6538
Fix #6538: Make interfaces.SourcePosition resilient to missing sources
2 parents dbac6dc + b273329 commit 9e46225

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
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: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,17 @@ public void doReport(MessageContainer cont, Context ctx) {
8080
SourceFile src = pos.source();
8181
position = new Position() {
8282
public Optional<java.io.File> sourceFile() {
83-
return Optional.ofNullable(src.file().file());
83+
if (src.exists()) return Optional.empty();
84+
else return Optional.ofNullable(src.file().file());
8485
}
8586
public Optional<String> sourcePath() {
86-
return Optional.ofNullable(src.file().path());
87+
if (src.exists()) return Optional.empty();
88+
else return Optional.ofNullable(src.file().path());
8789
}
8890
public Optional<Integer> line() {
89-
return Optional.of(pos.line());
91+
int line = pos.line();
92+
if (line == -1) return Optional.empty();
93+
else return Optional.of(line);
9094
}
9195
public String lineContent() {
9296
String line = pos.lineContent();
@@ -101,15 +105,19 @@ public Optional<Integer> offset() {
101105
return Optional.of(pos.point());
102106
}
103107
public Optional<Integer> pointer() {
104-
return Optional.of(pos.point() - src.startOfLine(pos.point()));
108+
if (!src.exists()) return Optional.empty();
109+
else return Optional.of(pos.point() - src.startOfLine(pos.point()));
105110
}
106111
public Optional<String> pointerSpace() {
107-
String lineContent = this.lineContent();
108-
int pointer = this.pointer().get();
109-
StringBuilder result = new StringBuilder();
110-
for (int i = 0; i < pointer; i++)
111-
result.append(lineContent.charAt(i) == '\t' ? '\t' : ' ');
112-
return Optional.of(result.toString());
112+
if (!src.exists()) return Optional.empty();
113+
else {
114+
String lineContent = this.lineContent();
115+
int pointer = this.pointer().get();
116+
StringBuilder result = new StringBuilder();
117+
for (int i = 0; i < pointer; i++)
118+
result.append(lineContent.charAt(i) == '\t' ? '\t' : ' ');
119+
return Optional.of(result.toString());
120+
}
113121
}
114122
};
115123
} else {

0 commit comments

Comments
 (0)