Skip to content

Update SourceCodePrinter to new ContextualFunction syntax #8271

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
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
2 changes: 1 addition & 1 deletion library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions library/src/scala/tasty/reflect/SourceCodePrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -382,18 +383,26 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
this += "}"

case Apply(fn, args) =>
var argsPrefix = ""
fn match {
case Select(This(_), "<init>") => 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 {
case init :+ Typed(Repeated(Nil, _), _) => init // drop empty var args at the end
case _ => args
}

inParens(printTrees(args1, ", "))
inParens {
this += argsPrefix
printTrees(args1, ", ")
}

case TypeApply(fn, args) =>
printQualTree(fn)
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/quote-nested-1.check
Original file line number Diff line number Diff line change
@@ -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))
6 changes: 3 additions & 3 deletions tests/run-staging/quote-nested-2.check
Original file line number Diff line number Diff line change
@@ -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)
})
6 changes: 3 additions & 3 deletions tests/run-staging/quote-nested-5.check
Original file line number Diff line number Diff line change
@@ -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)
})