From 86fb5519f8c946f9a039e1ce384dff50d2416f67 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 10 Feb 2020 09:18:30 +0100 Subject: [PATCH] Update SourceCodePrinter to new ContextualFunction syntax --- library/src/scala/tasty/Reflection.scala | 2 +- .../tasty/reflect/SourceCodePrinter.scala | 19 ++++++++++++++----- tests/run-staging/quote-nested-1.check | 2 +- tests/run-staging/quote-nested-2.check | 6 +++--- tests/run-staging/quote-nested-5.check | 6 +++--- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index 985ec1089f42..ba4d66035280 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -1015,7 +1015,7 @@ class Reflection(private[scala] val internal: CompilerInterface) { self => * of `Lambda`. */ object Lambda { - def unapply(tree: Tree)(using ctx: Context): Option[(List[ValDef], Term)] = tree match { + def unapply(tree: Block)(using ctx: Context): Option[(List[ValDef], Term)] = tree match { case Block((ddef @ DefDef(_, _, params :: Nil, _, Some(body))) :: Nil, Closure(meth, _)) if ddef.symbol == meth.symbol => Some(params, body) diff --git a/library/src/scala/tasty/reflect/SourceCodePrinter.scala b/library/src/scala/tasty/reflect/SourceCodePrinter.scala index 51351643b7e4..606b952cb191 100644 --- a/library/src/scala/tasty/reflect/SourceCodePrinter.scala +++ b/library/src/scala/tasty/reflect/SourceCodePrinter.scala @@ -4,6 +4,7 @@ package reflect import scala.annotation.switch import scala.quoted.show.SyntaxHighlight +/** Printer for fully elaborated representation of the source code */ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlight: SyntaxHighlight) extends Printer[R] { import tasty.{_, given} import syntaxHighlight._ @@ -382,10 +383,15 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += "}" case Apply(fn, args) => + var argsPrefix = "" fn match { case Select(This(_), "") => this += "this" // call to constructor inside a constructor - case Select(qual, "apply") if qual.tpe.isContextFunctionType => - printTree(qual) += " given " + case Select(qual, "apply") => + if qual.tpe.isContextFunctionType then + argsPrefix += "using " + if qual.tpe.isErasedFunctionType then + argsPrefix += "erased " + printQualTree(fn) case _ => printQualTree(fn) } val args1 = args match { @@ -393,7 +399,10 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case _ => args } - inParens(printTrees(args1, ", ")) + inParens { + this += argsPrefix + printTrees(args1, ", ") + } case TypeApply(fn, args) => printQualTree(fn) @@ -446,10 +455,10 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += " = " printTree(rhs) - case Lambda(params, body) => // must come before `Block` + case tree @ Lambda(params, body) => // must come before `Block` inParens { printArgsDefs(params) - this += " => " + this += (if tree.tpe.isContextFunctionType then " ?=> " else " => ") printTree(body) } diff --git a/tests/run-staging/quote-nested-1.check b/tests/run-staging/quote-nested-1.check index f14cd0cd6611..ca6f701a2eab 100644 --- a/tests/run-staging/quote-nested-1.check +++ b/tests/run-staging/quote-nested-1.check @@ -1 +1 @@ -((qctx: scala.quoted.QuoteContext) => scala.internal.quoted.CompileTime.exprQuote[scala.Int](3) given (qctx)) +((qctx: scala.quoted.QuoteContext) ?=> scala.internal.quoted.CompileTime.exprQuote[scala.Int](3).apply(using qctx)) diff --git a/tests/run-staging/quote-nested-2.check b/tests/run-staging/quote-nested-2.check index dcffb43a90a5..f1e228066874 100644 --- a/tests/run-staging/quote-nested-2.check +++ b/tests/run-staging/quote-nested-2.check @@ -1,4 +1,4 @@ -((qctx: scala.quoted.QuoteContext) => { - val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4) given (qctx) - ((qctx1_$1: scala.quoted.QuoteContext) => a) given (qctx) +((qctx: scala.quoted.QuoteContext) ?=> { + val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx) + ((qctx1_$1: scala.quoted.QuoteContext) ?=> a).apply(using qctx) }) diff --git a/tests/run-staging/quote-nested-5.check b/tests/run-staging/quote-nested-5.check index 0e26248287d1..02d099124875 100644 --- a/tests/run-staging/quote-nested-5.check +++ b/tests/run-staging/quote-nested-5.check @@ -1,4 +1,4 @@ -((qctx: scala.quoted.QuoteContext) => { - val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4) given (qctx) - ((qctx2: scala.quoted.QuoteContext) => ((qctx1_$1: scala.quoted.QuoteContext) => a) given (qctx2)) given (qctx) +((qctx: scala.quoted.QuoteContext) ?=> { + val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx) + ((qctx2: scala.quoted.QuoteContext) ?=> ((qctx1_$1: scala.quoted.QuoteContext) ?=> a).apply(using qctx2)).apply(using qctx) })