From 316e317def8e3ddc67d78c073e6384a133a90713 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 27 May 2019 18:13:51 +0200 Subject: [PATCH 1/4] Fix #6538: Make interfaces.SourcePosition resilient to missing sources --- .../src/dotty/tools/dotc/util/SourcePosition.scala | 14 ++++++-------- .../tools/dotc/interfaces/SourcePosition.java | 12 ++++++------ sbt-bridge/src/xsbt/DelegatingReporter.java | 4 +++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala index 8333b376a38b..a68531138e94 100644 --- a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala +++ b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala @@ -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] = @@ -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) diff --git a/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java b/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java index d8afbf5f607a..8e0d1c5a2e45 100644 --- a/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java +++ b/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java @@ -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. diff --git a/sbt-bridge/src/xsbt/DelegatingReporter.java b/sbt-bridge/src/xsbt/DelegatingReporter.java index 98671a9aff24..77e5147047a7 100644 --- a/sbt-bridge/src/xsbt/DelegatingReporter.java +++ b/sbt-bridge/src/xsbt/DelegatingReporter.java @@ -86,7 +86,9 @@ public Optional sourcePath() { return Optional.ofNullable(src.file().path()); } public Optional 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(); From d2def980d5855efceb290e991d83f682bc125475 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 28 May 2019 07:42:32 +0200 Subject: [PATCH 2/4] Return empty Optional for poiter with empty source --- sbt-bridge/src/xsbt/DelegatingReporter.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/sbt-bridge/src/xsbt/DelegatingReporter.java b/sbt-bridge/src/xsbt/DelegatingReporter.java index 77e5147047a7..65506c190482 100644 --- a/sbt-bridge/src/xsbt/DelegatingReporter.java +++ b/sbt-bridge/src/xsbt/DelegatingReporter.java @@ -103,15 +103,19 @@ public Optional offset() { return Optional.of(pos.point()); } public Optional 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 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 { From af091694f16279694f403412f43255791638e0e2 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 28 May 2019 07:46:41 +0200 Subject: [PATCH 3/4] Explicit check for existence of file --- sbt-bridge/src/xsbt/DelegatingReporter.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sbt-bridge/src/xsbt/DelegatingReporter.java b/sbt-bridge/src/xsbt/DelegatingReporter.java index 65506c190482..0e144874c313 100644 --- a/sbt-bridge/src/xsbt/DelegatingReporter.java +++ b/sbt-bridge/src/xsbt/DelegatingReporter.java @@ -80,10 +80,12 @@ public void doReport(MessageContainer cont, Context ctx) { SourceFile src = pos.source(); position = new Position() { public Optional sourceFile() { - return Optional.ofNullable(src.file().file()); + if (src.exists()) return Optional.empty(); + else return Optional.of(src.file().file()); } public Optional sourcePath() { - return Optional.ofNullable(src.file().path()); + if (src.exists()) return Optional.empty(); + else return Optional.of(src.file().path()); } public Optional line() { int line = pos.line(); From b27332974f23641d18375f85d88629178d8cba96 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 28 May 2019 11:10:23 +0200 Subject: [PATCH 4/4] Keep Optional.ofNullable --- sbt-bridge/src/xsbt/DelegatingReporter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sbt-bridge/src/xsbt/DelegatingReporter.java b/sbt-bridge/src/xsbt/DelegatingReporter.java index 0e144874c313..5f72cb2c18d4 100644 --- a/sbt-bridge/src/xsbt/DelegatingReporter.java +++ b/sbt-bridge/src/xsbt/DelegatingReporter.java @@ -81,11 +81,11 @@ public void doReport(MessageContainer cont, Context ctx) { position = new Position() { public Optional sourceFile() { if (src.exists()) return Optional.empty(); - else return Optional.of(src.file().file()); + else return Optional.ofNullable(src.file().file()); } public Optional sourcePath() { if (src.exists()) return Optional.empty(); - else return Optional.of(src.file().path()); + else return Optional.ofNullable(src.file().path()); } public Optional line() { int line = pos.line();