Skip to content

Add test of pickling positions under -Ytest-pickler #1541

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

Closed
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
4 changes: 4 additions & 0 deletions src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import java.lang.Integer.toOctalString
import config.Config.summarizeDepth
import scala.annotation.switch

/**
* Pretty printer with minimal formating. Similar to `.toString` but with proper indentation,
* position and a few more details. Accessible under `-Yplain-printer`.
*/
class PlainPrinter(_ctx: Context) extends Printer {
protected[this] implicit def ctx: Context = _ctx.addMode(Mode.Printing)

Expand Down
50 changes: 27 additions & 23 deletions src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import config.Config
import scala.annotation.switch
import language.implicitConversions

/** Pretty printer used by default with `.show`, generate strings very close to scala source code. */
class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {

/** A stack of enclosing DefDef, TypeDef, or ClassDef, or ModuleDefs nodes */
Expand Down Expand Up @@ -64,12 +65,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {

override def toTextRef(tp: SingletonType): Text = controlled {
tp match {
case tp: ThisType =>
if (tp.cls.isAnonymousClass) return "this"
if (tp.cls is ModuleClass) return fullNameString(tp.cls.sourceModule)
case tp: ThisType if tp.cls.isAnonymousClass =>
"this"
case tp: ThisType if tp.cls is ModuleClass =>
fullNameString(tp.cls.sourceModule)
case _ =>
super.toTextRef(tp)
}
super.toTextRef(tp)
}

override def toTextPrefix(tp: Type): Text = controlled {
Expand Down Expand Up @@ -113,44 +115,46 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
homogenize(tp) match {
case AppliedType(tycon, args) =>
val cls = tycon.typeSymbol
if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"
if (defn.isFunctionClass(cls)) return toTextFunction(args)
if (defn.isTupleClass(cls)) return toTextTuple(args)
return (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
if (tycon.isRepeatedParam) toTextLocal(args.head) ~ "*"
else if (defn.isFunctionClass(cls)) toTextFunction(args)
else if (defn.isTupleClass(cls)) toTextTuple(args)
else (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
case tp: TypeRef =>
val hideType = tp.symbol is AliasPreferred
if (hideType && !ctx.phase.erasedTypes && !tp.symbol.isCompleting) {
tp.info match {
case TypeAlias(alias) => return toText(alias)
case _ => if (tp.prefix.isInstanceOf[ThisType]) return nameString(tp.symbol)
case TypeAlias(alias) => toText(alias)
case _ if (tp.prefix.isInstanceOf[ThisType]) => nameString(tp.symbol)
case _ => super.toText(tp)
}
}
else if (tp.symbol.isAnonymousClass && !ctx.settings.uniqid.value)
return toText(tp.info)
} else if (tp.symbol.isAnonymousClass && !ctx.settings.uniqid.value)
toText(tp.info)
else
super.toText(tp)
case ExprType(result) =>
return "=> " ~ toText(result)
"=> " ~ toText(result)
case ErasedValueType(tycon, underlying) =>
return "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
"ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
case tp: ClassInfo =>
return toTextParents(tp.parentsWithArgs) ~ "{...}"
toTextParents(tp.parentsWithArgs) ~ "{...}"
case JavaArrayType(elemtp) =>
return toText(elemtp) ~ "[]"
toText(elemtp) ~ "[]"
case tp: SelectionProto =>
return "?{ " ~ toText(tp.name) ~ (" " provided !tp.name.decode.last.isLetterOrDigit) ~
"?{ " ~ toText(tp.name) ~ (" " provided !tp.name.decode.last.isLetterOrDigit) ~
": " ~ toText(tp.memberProto) ~ " }"
case tp: ViewProto =>
return toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)
toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)
case tp @ FunProto(args, resultType, _) =>
val argsText = args match {
case dummyTreeOfType(tp) :: Nil if !(tp isRef defn.NullClass) => "null: " ~ toText(tp)
case _ => toTextGlobal(args, ", ")
}
return "FunProto(" ~ argsText ~ "):" ~ toText(resultType)
"FunProto(" ~ argsText ~ "):" ~ toText(resultType)
case tp: IgnoredProto =>
return "?"
"?"
case _ =>
super.toText(tp)
}
super.toText(tp)
}

def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
Expand Down Expand Up @@ -527,7 +531,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
else if (!tree.isDef) txt = ("<" ~ txt ~ ":" ~ toText(tp) ~ ">").close
}
if (ctx.settings.Yprintpos.value && !tree.isInstanceOf[WithoutTypeOrPos[_]])
txt = txt ~ "@" ~ tree.pos.toString
txt = txt ~ "@" ~ tree.pos.toString(simplified = true)
tree match {
case Block(_, _) | Template(_, _, _, _) => txt
case _ => txt.close
Expand Down
8 changes: 5 additions & 3 deletions src/dotty/tools/dotc/util/Positions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ object Positions {
/** A synthetic copy of this position */
def toSynthetic = if (isSynthetic) this else Position(start, end)

override def toString = {
val (left, right) = if (isSynthetic) ("<", ">") else ("[", "]")
override def toString: String = toString(simplified = false)

def toString(simplified: Boolean): String = {
val (left, right) = if (simplified || isSynthetic) ("<", ">") else ("[", "]")
if (exists)
s"$left$start..${if (point == start) "" else s"$point.."}$end$right"
s"$left$start..${if (simplified || point == start) "" else s"$point.."}$end$right"
else
s"${left}no position${right}"
}
Expand Down
2 changes: 1 addition & 1 deletion test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class tests extends CompilerTest {
else List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef")
}

val testPickling = List("-Xprint-types", "-Ytest-pickler", "-Ystop-after:pickler")
val testPickling = List("-Xprint-types", "-Yprintpos", "-Ytest-pickler", "-Ystop-after:pickler")

val twice = List("#runs", "2")
val staleSymbolError: List[String] = List()
Expand Down