Skip to content

Commit 5de6116

Browse files
authored
Merge pull request #5661 from dotty-staging/change-sourcepos-showable
Make SourcePosition showable
2 parents 4eee106 + 5eebf22 commit 5de6116

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import StdNames.nme
88
import ast.Trees._
99
import typer.Implicits._
1010
import typer.ImportInfo
11+
import util.SourcePosition
1112
import java.lang.Integer.toOctalString
1213
import config.Config.summarizeDepth
1314
import scala.util.control.NonFatal
@@ -504,6 +505,11 @@ class PlainPrinter(_ctx: Context) extends Printer {
504505
nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(tree.pos.toString) provided ctx.settings.YprintPos.value)
505506
}.close // todo: override in refined printer
506507

508+
def toText(pos: SourcePosition): Text =
509+
if (!pos.exists) "<no position>"
510+
else if (pos.source.exists) s"${pos.source.file}:${pos.line + 1}"
511+
else s"(no source file, offset = ${pos.pos.point})"
512+
507513
def toText(result: SearchResult): Text = result match {
508514
case result: SearchSuccess =>
509515
"SearchSuccess: " ~ toText(result.ref) ~ " via " ~ toText(result.tree)

compiler/src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Texts._, ast.Trees._
77
import Types.Type, Symbols.Symbol, Scopes.Scope, Constants.Constant,
88
Names.Name, Denotations._, Annotations.Annotation
99
import typer.Implicits.SearchResult
10+
import util.SourcePosition
1011
import typer.ImportInfo
1112

1213
import scala.annotation.internal.sharable
@@ -134,6 +135,9 @@ abstract class Printer {
134135
/** Textual representation of tree */
135136
def toText[T >: Untyped](tree: Tree[T]): Text
136137

138+
/** Textual representation of source position */
139+
def toText(pos: SourcePosition): Text
140+
137141
/** Textual representation of implicit search result */
138142
def toText(result: SearchResult): Text
139143

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait MessageRendering {
3333
*/
3434
def outer(pos: SourcePosition, prefix: String)(implicit ctx: Context): List[String] =
3535
if (pos.outer.exists) {
36-
s"$prefix| This location is in code that was inlined at ${pos.outer}" ::
36+
i"$prefix| This location is in code that was inlined at ${pos.outer}" ::
3737
outer(pos.outer, prefix)
3838
} else Nil
3939

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package dotty.tools
22
package dotc
33
package util
44

5+
import printing.{Showable, Printer}
6+
import printing.Texts._
57
import Positions.{Position, NoPosition}
68

79
import scala.annotation.internal.sharable
810

911
/** A source position is comprised of a position in a source file */
1012
case class SourcePosition(source: SourceFile, pos: Position, outer: SourcePosition = NoSourcePosition)
11-
extends interfaces.SourcePosition {
13+
extends interfaces.SourcePosition with Showable {
1214
/** Is `that` a source position contained in this source position ?
1315
* `outer` is not taken into account. */
1416
def contains(that: SourcePosition): Boolean =
@@ -56,8 +58,9 @@ extends interfaces.SourcePosition {
5658
def withOuter(outer: SourcePosition): SourcePosition = new SourcePosition(source, pos, outer)
5759

5860
override def toString: String =
59-
if (source.exists) s"${source.file}:${line + 1}"
60-
else s"(no source file, offset = ${pos.point})"
61+
s"${if (source.exists) source.file.toString else "(no source)"}:$pos"
62+
63+
def toText(printer: Printer): Text = printer.toText(this)
6164
}
6265

6366
/** A sentinel for a non-existing source position */

compiler/test/dotty/tools/dotc/reporting/TestReporter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package reporting
55
import java.io.{ PrintStream, PrintWriter, File => JFile, FileOutputStream }
66
import java.text.SimpleDateFormat
77
import java.util.Date
8+
import core.Decorators._
89

910
import scala.collection.mutable
1011

@@ -28,10 +29,10 @@ extends Reporter with UniqueMessagePositions with HideNonSensicalMessages with M
2829
private[this] var _didCrash = false
2930
final def compilerCrashed: Boolean = _didCrash
3031

31-
protected final def inlineInfo(pos: SourcePosition): String =
32+
protected final def inlineInfo(pos: SourcePosition)(implicit ctx: Context): String =
3233
if (pos.exists) {
3334
if (pos.outer.exists)
34-
s"\ninlined at ${pos.outer}:\n" + inlineInfo(pos.outer)
35+
i"\ninlined at ${pos.outer}:\n" + inlineInfo(pos.outer)
3536
else ""
3637
}
3738
else ""

0 commit comments

Comments
 (0)