Skip to content

Fix #6538: Make interfaces.SourcePosition resilient to missing sources #6573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions compiler/src/dotty/tools/dotc/util/SourcePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ extends interfaces.SourcePosition with Showable {

def point: Int = span.point

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

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

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

def start: Int = span.start
def startLine: Int = source.offsetToLine(start)
def startColumn: Int = source.column(start)
def startLine: Int = if (source.exists) source.offsetToLine(start) else -1
def startColumn: Int = if (source.exists) source.column(start) else -1
def startColumnPadding: String = source.startColumnPadding(start)

def end: Int = span.end
def endLine: Int = source.offsetToLine(end)
def endColumn: Int = source.column(end)
def endLine: Int = if (source.exists) source.offsetToLine(end) else -1
def endColumn: Int = if (source.exists) source.column(end) else -1

def withOuter(outer: SourcePosition): SourcePosition = SourcePosition(source, span, outer)
def withSpan(range: Span) = SourcePosition(source, range, outer)
Expand Down
12 changes: 6 additions & 6 deletions interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public interface SourcePosition {

/** @return Offset to the point */
int point();
/** @return Line number of the point, starting at 0 */
/** @return Line number of the point, starting at 0. -1 if the line cannot be computed */
int line();
/** @return Column number of the point, starting at 0 */
/** @return Column number of the point, starting at 0. -1 if the column cannot be computed */
int column();

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

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

/** The source file corresponding to this position.
Expand Down
28 changes: 18 additions & 10 deletions sbt-bridge/src/xsbt/DelegatingReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ public void doReport(MessageContainer cont, Context ctx) {
SourceFile src = pos.source();
position = new Position() {
public Optional<java.io.File> sourceFile() {
return Optional.ofNullable(src.file().file());
if (src.exists()) return Optional.empty();
else return Optional.ofNullable(src.file().file());
}
public Optional<String> sourcePath() {
return Optional.ofNullable(src.file().path());
if (src.exists()) return Optional.empty();
else return Optional.ofNullable(src.file().path());
}
public Optional<Integer> line() {
return Optional.of(pos.line());
int line = pos.line();
if (line == -1) return Optional.empty();
else return Optional.of(line);
}
public String lineContent() {
String line = pos.lineContent();
Expand All @@ -101,15 +105,19 @@ public Optional<Integer> offset() {
return Optional.of(pos.point());
}
public Optional<Integer> pointer() {
return Optional.of(pos.point() - src.startOfLine(pos.point()));
if (!src.exists()) return Optional.empty();
else return Optional.of(pos.point() - src.startOfLine(pos.point()));
}
public Optional<String> pointerSpace() {
String lineContent = this.lineContent();
int pointer = this.pointer().get();
StringBuilder result = new StringBuilder();
for (int i = 0; i < pointer; i++)
result.append(lineContent.charAt(i) == '\t' ? '\t' : ' ');
return Optional.of(result.toString());
if (!src.exists()) return Optional.empty();
else {
String lineContent = this.lineContent();
int pointer = this.pointer().get();
StringBuilder result = new StringBuilder();
for (int i = 0; i < pointer; i++)
result.append(lineContent.charAt(i) == '\t' ? '\t' : ' ');
return Optional.of(result.toString());
}
}
};
} else {
Expand Down