Skip to content

Commit

Permalink
Fix scala#6538: Make interfaces.SourcePosition resilient to missing s…
Browse files Browse the repository at this point in the history
…ources
  • Loading branch information
nicolasstucki committed May 28, 2019
1 parent dbac6dc commit 316e317
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
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
4 changes: 3 additions & 1 deletion sbt-bridge/src/xsbt/DelegatingReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public Optional<String> sourcePath() {
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 Down

0 comments on commit 316e317

Please sign in to comment.