Skip to content

Fix #7154: Adjust RefinedPrinter to handle extension methods #7155

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
Sep 2, 2019
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
18 changes: 10 additions & 8 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
def tparamsText[T >: Untyped](params: List[Tree[T]]): Text =
"[" ~ toText(params, ", ") ~ "]" provided params.nonEmpty

def addVparamssText[T >: Untyped](txt: Text, vparamss: List[List[ValDef[T]]], isExtension: Boolean = false): Text = {
def paramsText(params: List[ValDef[T]]) = "(" ~ toText(params, ", ") ~ ")"
val (leading, paramss) =
if (isExtension && vparamss.nonEmpty) (paramsText(vparamss.head) ~ " " ~ txt, vparamss.tail)
else (txt, vparamss)
paramss.foldLeft(leading)((txt, params) =>
def addVparamssText[T >: Untyped](leading: Text, vparamss: List[List[ValDef[T]]]): Text = {
vparamss.foldLeft(leading)((txt, params) =>
txt ~
(Str(" given ") provided params.nonEmpty && params.head.mods.is(Given)) ~
paramsText(params))
Expand All @@ -761,13 +757,19 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
}
}

private def paramsText[T>: Untyped](params: List[ValDef[T]]) = "(" ~ toText(params, ", ") ~ ")"

protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
import untpd.{modsDeco => _}
dclTextOr(tree) {
val prefix = modText(tree.mods, tree.symbol, keywordStr("def"), isType = false) ~~ valDefText(nameIdText(tree))
val defKeyword = modText(tree.mods, tree.symbol, keywordStr("def"), isType = false)
val isExtension = tree.hasType && tree.symbol.is(Extension)
withEnclosingDef(tree) {
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss, isExtension) ~
val (prefix, vparamss) =
if(isExtension) (defKeyword ~~ paramsText(tree.vparamss.head) ~~ valDefText(nameIdText(tree)), tree.vparamss.tail)
else (defKeyword ~~ valDefText(nameIdText(tree)), tree.vparamss)

addVparamssText(prefix ~ tparamsText(tree.tparams), vparamss) ~
optAscription(tree.tpt) ~
optText(tree.rhs)(" = " ~ _)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/printing/i620.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package O {
private[A] def e: Int = 0
protected[A] def f: Int = 0
def g: Int = 0
def g1(t: Int): Int = 0
def (c: D.this.C) g1: Int = 0
}
private[D] class E() extends Object() {}
private class F() extends Object() {}
Expand Down
2 changes: 2 additions & 0 deletions tests/printing/i620.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class D {
private[A] def e: Int = 0
protected[A] def f: Int = 0
def g: Int = 0
def g1(t: Int) = 0
def (c: C) g1 = 0
}

private[D] class E
Expand Down