Skip to content

Commit 45b6ebd

Browse files
Merge pull request #7346 from dotty-staging/spliced-names
Add customizable names for definitions in quotes
2 parents 441c2ee + 8fb7a44 commit 45b6ebd

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package scala.internal.quoted
2+
3+
/** Annotation used inside a quote to give a custom name to a definition.
4+
* The `name` argument must be a literal String.
5+
*
6+
* Usage:
7+
* ```scala
8+
* def let(name: String)(value: Expr[Int])(in: Expr[Int] => Expr[Int]): Expr[Int] = '{
9+
* @showName(${Expr(name)})
10+
* val x = $value
11+
* ${ in('x) }
12+
* }
13+
* ```
14+
* then using it in
15+
* ```scala
16+
* let("myVal")('{4})(x => '{ $x + 1}).show
17+
* ```
18+
* will retuns the code
19+
* ```scala
20+
* val myVal = 4
21+
* myVal + 1
22+
* ```
23+
*/
24+
class showName(name: String) extends scala.annotation.Annotation

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,8 @@ trait Printers
679679
if (vdef.symbol.flags.is(Flags.Mutable)) this += highlightKeyword("var ")
680680
else this += highlightKeyword("val ")
681681

682-
this += highlightValDef(name) += ": "
682+
val name1 = splicedName(vdef.symbol).getOrElse(name)
683+
this += highlightValDef(name1) += ": "
683684
printTypeTree(tpt)
684685
rhs match {
685686
case Some(tree) =>
@@ -714,7 +715,8 @@ trait Printers
714715

715716
printProtectedOrPrivate(ddef)
716717

717-
this += highlightKeyword("def ") += highlightValDef((if (isConstructor) "this" else name))
718+
val name1: String = if (isConstructor) "this" else splicedName(ddef.symbol).getOrElse(name)
719+
this += highlightKeyword("def ") += highlightValDef(name1)
718720
printTargsDefs(targs.zip(targs))
719721
val it = argss.iterator
720722
while (it.hasNext)
@@ -734,8 +736,11 @@ trait Printers
734736
case Ident("_") =>
735737
this += "_"
736738

737-
case IsTerm(tree @ Ident(_)) =>
738-
printType(tree.tpe)
739+
case IsIdent(tree) =>
740+
splicedName(tree.symbol) match {
741+
case Some(name) => this += name
742+
case _ => printType(tree.tpe)
743+
}
739744

740745
case Select(qual, name) =>
741746
printQualTree(qual)
@@ -1637,12 +1642,15 @@ trait Printers
16371642

16381643
def printAnnotation(annot: Term)(given elideThis: Option[Symbol]): Buffer = {
16391644
val Annotation(ref, args) = annot
1640-
this += "@"
1641-
printTypeTree(ref)
1642-
if (args.isEmpty)
1643-
this
1644-
else
1645-
inParens(printTrees(args, ", "))
1645+
if (annot.symbol.owner.fullName == "scala.internal.quoted.showName") this
1646+
else {
1647+
this += "@"
1648+
printTypeTree(ref)
1649+
if (args.isEmpty)
1650+
this
1651+
else
1652+
inParens(printTrees(args, ", "))
1653+
}
16461654
}
16471655

16481656
def printDefAnnotations(definition: Definition)(given elideThis: Option[Symbol]): Buffer = {
@@ -1809,6 +1817,14 @@ trait Printers
18091817
private def escapedString(str: String): String = str flatMap escapedChar
18101818
}
18111819

1820+
private def splicedName(sym: Symbol)(given ctx: Context): Option[String] = {
1821+
sym.annots.find(_.symbol.owner.fullName == "scala.internal.quoted.showName").flatMap {
1822+
case Apply(_, Literal(Constant(c: String)) :: Nil) => Some(c)
1823+
case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => Some(c)
1824+
case annot => None
1825+
}
1826+
}
1827+
18121828
private object SpecialOp {
18131829
def unapply(arg: Tree)(given ctx: Context): Option[(String, List[Term])] = arg match {
18141830
case IsTerm(arg @ Apply(fn, args)) =>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
((x1: scala.Double) => x1.*({
2+
val x2: scala.Double = x1.*(x1)
3+
val x4: scala.Double = x2.*(x2)
4+
x4.*({
5+
val x8: scala.Double = x4.*(x4)
6+
x8.*({
7+
val x16: scala.Double = x8.*(x8)
8+
val x32: scala.Double = x16.*(x16)
9+
val x64: scala.Double = x32.*(x32)
10+
x64
11+
})
12+
})
13+
}))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.quoted._
2+
import scala.internal.quoted.showName
3+
import scala.quoted.staging._
4+
import scala.reflect.ClassTag
5+
6+
object Test {
7+
given Toolbox = Toolbox.make(getClass.getClassLoader)
8+
def main(args: Array[String]): Unit = withQuoteContext {
9+
println(powerCode(77).show)
10+
}
11+
12+
def powerCode(n: Long)(given QuoteContext): Expr[Double => Double] =
13+
'{ x1 => ${powerCode(n, 2, 'x1)} }
14+
15+
def powerCode(n: Long, idx: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
16+
if (n == 0) '{1.0}
17+
else if (n == 1) x
18+
else if (n % 2 == 0) '{ @showName(${Expr("x" + idx)}) val y = $x * $x; ${powerCode(n / 2, idx * 2, '{y})} }
19+
else '{ $x * ${powerCode(n - 1, idx, x)} }
20+
21+
}

0 commit comments

Comments
 (0)