Skip to content

Make SourcePosition showable #5661

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 1 commit into from
Dec 29, 2018
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
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import StdNames.nme
import ast.Trees._
import typer.Implicits._
import typer.ImportInfo
import util.SourcePosition
import java.lang.Integer.toOctalString
import config.Config.summarizeDepth
import scala.util.control.NonFatal
Expand Down Expand Up @@ -504,6 +505,11 @@ class PlainPrinter(_ctx: Context) extends Printer {
nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(tree.pos.toString) provided ctx.settings.YprintPos.value)
}.close // todo: override in refined printer

def toText(pos: SourcePosition): Text =
if (!pos.exists) "<no position>"
else if (pos.source.exists) s"${pos.source.file}:${pos.line + 1}"
else s"(no source file, offset = ${pos.pos.point})"

def toText(result: SearchResult): Text = result match {
case result: SearchSuccess =>
"SearchSuccess: " ~ toText(result.ref) ~ " via " ~ toText(result.tree)
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/Printer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Texts._, ast.Trees._
import Types.Type, Symbols.Symbol, Scopes.Scope, Constants.Constant,
Names.Name, Denotations._, Annotations.Annotation
import typer.Implicits.SearchResult
import util.SourcePosition
import typer.ImportInfo

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

/** Textual representation of source position */
def toText(pos: SourcePosition): Text

/** Textual representation of implicit search result */
def toText(result: SearchResult): Text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait MessageRendering {
*/
def outer(pos: SourcePosition, prefix: String)(implicit ctx: Context): List[String] =
if (pos.outer.exists) {
s"$prefix| This location is in code that was inlined at ${pos.outer}" ::
i"$prefix| This location is in code that was inlined at ${pos.outer}" ::
outer(pos.outer, prefix)
} else Nil

Expand Down
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/util/SourcePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package dotty.tools
package dotc
package util

import printing.{Showable, Printer}
import printing.Texts._
import Positions.{Position, NoPosition}

import scala.annotation.internal.sharable

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

override def toString: String =
if (source.exists) s"${source.file}:${line + 1}"
else s"(no source file, offset = ${pos.point})"
s"${if (source.exists) source.file.toString else "(no source)"}:$pos"

def toText(printer: Printer): Text = printer.toText(this)
}

/** A sentinel for a non-existing source position */
Expand Down
5 changes: 3 additions & 2 deletions compiler/test/dotty/tools/dotc/reporting/TestReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package reporting
import java.io.{ PrintStream, PrintWriter, File => JFile, FileOutputStream }
import java.text.SimpleDateFormat
import java.util.Date
import core.Decorators._

import scala.collection.mutable

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

protected final def inlineInfo(pos: SourcePosition): String =
protected final def inlineInfo(pos: SourcePosition)(implicit ctx: Context): String =
if (pos.exists) {
if (pos.outer.exists)
s"\ninlined at ${pos.outer}:\n" + inlineInfo(pos.outer)
i"\ninlined at ${pos.outer}:\n" + inlineInfo(pos.outer)
else ""
}
else ""
Expand Down