Skip to content

Commit 9d7f2bb

Browse files
authored
Merge pull request #8210 from dotty-staging/update-reflection-given-syntax
Update reflection given syntax
2 parents bfb223b + 90cbdcd commit 9d7f2bb

File tree

6 files changed

+1405
-1406
lines changed

6 files changed

+1405
-1406
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 422 additions & 422 deletions
Large diffs are not rendered by default.

library/src/scala/tasty/Reflection.scala

Lines changed: 525 additions & 526 deletions
Large diffs are not rendered by default.

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 413 additions & 413 deletions
Large diffs are not rendered by default.

library/src/scala/tasty/reflect/ExtractorsPrinter.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ package reflect
44
class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Printer[R] {
55
import tasty.{_, given}
66

7-
def showTree(tree: Tree)(given ctx: Context): String =
7+
def showTree(tree: Tree)(using ctx: Context): String =
88
new Buffer().visitTree(tree).result()
99

10-
def showTypeOrBounds(tpe: TypeOrBounds)(given ctx: Context): String =
10+
def showTypeOrBounds(tpe: TypeOrBounds)(using ctx: Context): String =
1111
new Buffer().visitType(tpe).result()
1212

13-
def showConstant(const: Constant)(given ctx: Context): String =
13+
def showConstant(const: Constant)(using ctx: Context): String =
1414
new Buffer().visitConstant(const).result()
1515

16-
def showSymbol(symbol: Symbol)(given ctx: Context): String =
16+
def showSymbol(symbol: Symbol)(using ctx: Context): String =
1717
new Buffer().visitSymbol(symbol).result()
1818

19-
def showFlags(flags: Flags)(given ctx: Context): String = {
19+
def showFlags(flags: Flags)(using ctx: Context): String = {
2020
val flagList = List.newBuilder[String]
2121
if (flags.is(Flags.Private)) flagList += "Flags.Private"
2222
if (flags.is(Flags.Protected)) flagList += "Flags.Protected"
@@ -54,7 +54,7 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print
5454
flagList.result().mkString(" | ")
5555
}
5656

57-
private class Buffer(given ctx: Context) { self =>
57+
private class Buffer(using ctx: Context) { self =>
5858

5959
private val sb: StringBuilder = new StringBuilder
6060

library/src/scala/tasty/reflect/Printer.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ trait Printer[R <: Reflection & Singleton] {
77
val tasty: R
88

99
/** Show a String representation of a tasty.Tree */
10-
def showTree(tree: tasty.Tree)(given ctx: tasty.Context): String
10+
def showTree(tree: tasty.Tree)(using ctx: tasty.Context): String
1111

1212
/** Show a String representation of a tasty.TypeOrBounds */
13-
def showTypeOrBounds(tpe: tasty.TypeOrBounds)(given ctx: tasty.Context): String
13+
def showTypeOrBounds(tpe: tasty.TypeOrBounds)(using ctx: tasty.Context): String
1414

1515
/** Show a String representation of a tasty.Constant */
16-
def showConstant(const: tasty.Constant)(given ctx: tasty.Context): String
16+
def showConstant(const: tasty.Constant)(using ctx: tasty.Context): String
1717

1818
/** Show a String representation of a tasty.Symbol */
19-
def showSymbol(symbol: tasty.Symbol)(given ctx: tasty.Context): String
19+
def showSymbol(symbol: tasty.Symbol)(using ctx: tasty.Context): String
2020

2121
/** Show a String representation of a tasty.Flags */
22-
def showFlags(flags: tasty.Flags)(given ctx: tasty.Context): String
22+
def showFlags(flags: tasty.Flags)(using ctx: tasty.Context): String
2323
}

library/src/scala/tasty/reflect/SourceCodePrinter.scala

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
88
import tasty.{_, given}
99
import syntaxHighlight._
1010

11-
def showTree(tree: Tree)(given ctx: Context): String =
11+
def showTree(tree: Tree)(using ctx: Context): String =
1212
(new Buffer).printTree(tree).result()
1313

14-
def showTypeOrBounds(tpe: TypeOrBounds)(given ctx: Context): String =
14+
def showTypeOrBounds(tpe: TypeOrBounds)(using ctx: Context): String =
1515
(new Buffer).printTypeOrBound(tpe)(given None).result()
1616

17-
def showConstant(const: Constant)(given ctx: Context): String =
17+
def showConstant(const: Constant)(using ctx: Context): String =
1818
(new Buffer).printConstant(const).result()
1919

20-
def showSymbol(symbol: Symbol)(given ctx: Context): String =
20+
def showSymbol(symbol: Symbol)(using ctx: Context): String =
2121
symbol.fullName
2222

23-
def showFlags(flags: Flags)(given ctx: Context): String = {
23+
def showFlags(flags: Flags)(using ctx: Context): String = {
2424
val flagList = List.newBuilder[String]
2525
if (flags.is(Flags.Private)) flagList += "private"
2626
if (flags.is(Flags.Protected)) flagList += "protected"
@@ -58,7 +58,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
5858
flagList.result().mkString("/*", " ", "*/")
5959
}
6060

61-
private class Buffer(given ctx: Context) {
61+
private class Buffer(using ctx: Context) {
6262

6363
private[this] val sb: StringBuilder = new StringBuilder
6464

@@ -95,7 +95,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
9595
def lineBreak(): String = "\n" + (" " * indent)
9696
def doubleLineBreak(): String = "\n\n" + (" " * indent)
9797

98-
def printTree(tree: Tree)(given elideThis: Option[Symbol] = None): Buffer = tree match {
98+
def printTree(tree: Tree)(using elideThis: Option[Symbol] = None): Buffer = tree match {
9999
case PackageObject(body)=>
100100
printTree(body) // Print package object
101101

@@ -570,7 +570,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
570570
(flatStats.result(), flatExpr)
571571
}
572572

573-
def printFlatBlock(stats: List[Statement], expr: Term)(given elideThis: Option[Symbol]): Buffer = {
573+
def printFlatBlock(stats: List[Statement], expr: Term)(using elideThis: Option[Symbol]): Buffer = {
574574
val (stats1, expr1) = flatBlock(stats, expr)
575575
val stats2 = stats1.filter {
576576
case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.owner == ctx.requiredClass("scala.internal.Quoted.quoteTypeTag"))
@@ -587,7 +587,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
587587
}
588588
}
589589

590-
def printStats(stats: List[Tree], expr: Tree)(given eliseThis: Option[Symbol]): Unit = {
590+
def printStats(stats: List[Tree], expr: Tree)(using eliseThis: Option[Symbol]): Unit = {
591591
def printSeparator(next: Tree): Unit = {
592592
// Avoid accidental application of opening `{` on next line with a double break
593593
def rec(next: Tree): Unit = next match {
@@ -635,13 +635,13 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
635635
this
636636
}
637637

638-
def printTrees(trees: List[Tree], sep: String)(given elideThis: Option[Symbol]): Buffer =
638+
def printTrees(trees: List[Tree], sep: String)(using elideThis: Option[Symbol]): Buffer =
639639
printList(trees, sep, (t: Tree) => printTree(t))
640640

641-
def printTypeTrees(trees: List[TypeTree], sep: String)(given elideThis: Option[Symbol] = None): Buffer =
641+
def printTypeTrees(trees: List[TypeTree], sep: String)(using elideThis: Option[Symbol] = None): Buffer =
642642
printList(trees, sep, (t: TypeTree) => printTypeTree(t))
643643

644-
def printTypes(trees: List[Type], sep: String)(given elideThis: Option[Symbol]): Buffer = {
644+
def printTypes(trees: List[Type], sep: String)(using elideThis: Option[Symbol]): Buffer = {
645645
def printSeparated(list: List[Type]): Unit = list match {
646646
case Nil =>
647647
case x :: Nil => printType(x)
@@ -707,7 +707,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
707707
this
708708
}
709709

710-
def printTypesOrBounds(types: List[TypeOrBounds], sep: String)(given elideThis: Option[Symbol]): Buffer = {
710+
def printTypesOrBounds(types: List[TypeOrBounds], sep: String)(using elideThis: Option[Symbol]): Buffer = {
711711
def printSeparated(list: List[TypeOrBounds]): Unit = list match {
712712
case Nil =>
713713
case x :: Nil => printTypeOrBound(x)
@@ -720,7 +720,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
720720
this
721721
}
722722

723-
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true)(given elideThis: Option[Symbol]): Unit = {
723+
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true)(using elideThis: Option[Symbol]): Unit = {
724724
if (!targs.isEmpty) {
725725
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
726726
case Nil =>
@@ -735,7 +735,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
735735
}
736736
}
737737

738-
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true)(given elideThis: Option[Symbol]): Buffer = {
738+
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true)(using elideThis: Option[Symbol]): Buffer = {
739739
val (argDef, argCons) = arg
740740

741741
if (isDef) {
@@ -785,7 +785,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
785785
}
786786
}
787787

788-
def printArgsDefs(args: List[ValDef])(given elideThis: Option[Symbol]): Unit = {
788+
def printArgsDefs(args: List[ValDef])(using elideThis: Option[Symbol]): Unit = {
789789
val argFlags = args match {
790790
case Nil => Flags.EmptyFlags
791791
case arg :: _ => arg.symbol.flags
@@ -811,7 +811,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
811811
}
812812
}
813813

814-
def printAnnotations(trees: List[Term])(given elideThis: Option[Symbol]): Buffer = {
814+
def printAnnotations(trees: List[Term])(using elideThis: Option[Symbol]): Buffer = {
815815
def printSeparated(list: List[Term]): Unit = list match {
816816
case Nil =>
817817
case x :: Nil => printAnnotation(x)
@@ -824,7 +824,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
824824
this
825825
}
826826

827-
def printParamDef(arg: ValDef)(given elideThis: Option[Symbol]): Unit = {
827+
def printParamDef(arg: ValDef)(using elideThis: Option[Symbol]): Unit = {
828828
val name = arg.name
829829
val sym = arg.symbol.owner
830830
if sym.isDefDef && sym.name == "<init>" then
@@ -943,7 +943,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
943943
inSquare(printType(v))
944944
}
945945

946-
def printTypeOrBoundsTree(tpt: Tree)(given elideThis: Option[Symbol] = None): Buffer = tpt match {
946+
def printTypeOrBoundsTree(tpt: Tree)(using elideThis: Option[Symbol] = None): Buffer = tpt match {
947947
case TypeBoundsTree(lo, hi) =>
948948
this += "_ >: "
949949
printTypeTree(lo)
@@ -963,7 +963,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
963963
* Self type annotation and types in parent list should elide current class
964964
* prefix `C.this` to avoid type checking errors.
965965
*/
966-
def printTypeTree(tree: TypeTree)(given elideThis: Option[Symbol] = None): Buffer = tree match {
966+
def printTypeTree(tree: TypeTree)(using elideThis: Option[Symbol] = None): Buffer = tree match {
967967
case Inferred() =>
968968
// TODO try to move this logic into `printType`
969969
def printTypeAndAnnots(tpe: Type): Buffer = tpe match {
@@ -1049,7 +1049,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
10491049

10501050
}
10511051

1052-
def printTypeOrBound(tpe: TypeOrBounds)(given elideThis: Option[Symbol]): Buffer = tpe match {
1052+
def printTypeOrBound(tpe: TypeOrBounds)(using elideThis: Option[Symbol]): Buffer = tpe match {
10531053
case tpe@TypeBounds(lo, hi) =>
10541054
this += "_ >: "
10551055
printType(lo)
@@ -1066,7 +1066,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
10661066
* Self type annotation and types in parent list should elide current class
10671067
* prefix `C.this` to avoid type checking errors.
10681068
*/
1069-
def printType(tpe: Type)(given elideThis: Option[Symbol] = None): Buffer = tpe match {
1069+
def printType(tpe: Type)(using elideThis: Option[Symbol] = None): Buffer = tpe match {
10701070
case ConstantType(const) =>
10711071
printConstant(const)
10721072

@@ -1226,7 +1226,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12261226
case PackageDef(name, _) => this += highlightTypeDef(name)
12271227
}
12281228

1229-
def printAnnotation(annot: Term)(given elideThis: Option[Symbol]): Buffer = {
1229+
def printAnnotation(annot: Term)(using elideThis: Option[Symbol]): Buffer = {
12301230
val Annotation(ref, args) = annot
12311231
if (annot.symbol.maybeOwner == ctx.requiredClass("scala.internal.quoted.showName")) this
12321232
else {
@@ -1239,7 +1239,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12391239
}
12401240
}
12411241

1242-
def printDefAnnotations(definition: Definition)(given elideThis: Option[Symbol]): Buffer = {
1242+
def printDefAnnotations(definition: Definition)(using elideThis: Option[Symbol]): Buffer = {
12431243
val annots = definition.symbol.annots.filter {
12441244
case Annotation(annot, _) =>
12451245
val sym = annot.tpe.typeSymbol
@@ -1252,7 +1252,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12521252
else this
12531253
}
12541254

1255-
def printRefinement(tpe: Type)(given elideThis: Option[Symbol]): Buffer = {
1255+
def printRefinement(tpe: Type)(using elideThis: Option[Symbol]): Buffer = {
12561256
def printMethodicType(tp: TypeOrBounds): Unit = tp match {
12571257
case tp @ MethodType(paramNames, params, res) =>
12581258
inParens(printMethodicTypeParams(paramNames, params))
@@ -1292,7 +1292,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12921292
this += lineBreak() += "}"
12931293
}
12941294

1295-
def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds])(given elideThis: Option[Symbol]): Unit = {
1295+
def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds])(using elideThis: Option[Symbol]): Unit = {
12961296
def printInfo(info: TypeOrBounds) = info match {
12971297
case info: TypeBounds => printBounds(info)
12981298
case info: Type =>
@@ -1313,7 +1313,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
13131313
printSeparated(paramNames.zip(params))
13141314
}
13151315

1316-
def printBoundsTree(bounds: TypeBoundsTree)(given elideThis: Option[Symbol]): Buffer = {
1316+
def printBoundsTree(bounds: TypeBoundsTree)(using elideThis: Option[Symbol]): Buffer = {
13171317
bounds.low match {
13181318
case Inferred() =>
13191319
case low =>
@@ -1328,7 +1328,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
13281328
}
13291329
}
13301330

1331-
def printBounds(bounds: TypeBounds)(given elideThis: Option[Symbol]): Buffer = {
1331+
def printBounds(bounds: TypeBounds)(using elideThis: Option[Symbol]): Buffer = {
13321332
this += " >: "
13331333
printType(bounds.low)
13341334
this += " <: "
@@ -1400,7 +1400,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14001400
private def escapedString(str: String): String = str flatMap escapedChar
14011401
}
14021402

1403-
private def splicedName(sym: Symbol)(given ctx: Context): Option[String] = {
1403+
private def splicedName(sym: Symbol)(using ctx: Context): Option[String] = {
14041404
sym.annots.find(_.symbol.owner == ctx.requiredClass("scala.internal.quoted.showName")).flatMap {
14051405
case Apply(_, Literal(Constant(c: String)) :: Nil) => Some(c)
14061406
case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => Some(c)
@@ -1409,7 +1409,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14091409
}
14101410

14111411
private object SpecialOp {
1412-
def unapply(arg: Tree)(given ctx: Context): Option[(String, List[Term])] = arg match {
1412+
def unapply(arg: Tree)(using ctx: Context): Option[(String, List[Term])] = arg match {
14131413
case arg @ Apply(fn, args) =>
14141414
fn.tpe match {
14151415
case tpe @ TermRef(ThisType(TypeRef(_, name)), name2) if name == "<special-ops>" =>
@@ -1421,7 +1421,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14211421
}
14221422

14231423
private object Annotation {
1424-
def unapply(arg: Tree)(given ctx: Context): Option[(TypeTree, List[Term])] = arg match {
1424+
def unapply(arg: Tree)(using ctx: Context): Option[(TypeTree, List[Term])] = arg match {
14251425
case New(annot) => Some((annot, Nil))
14261426
case Apply(Select(New(annot), "<init>"), args) => Some((annot, args))
14271427
case Apply(TypeApply(Select(New(annot), "<init>"), targs), args) => Some((annot, args))
@@ -1433,14 +1433,14 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14331433
private object Types {
14341434

14351435
object Sequence {
1436-
def unapply(tpe: Type)(given ctx: Context): Option[Type] = tpe match {
1436+
def unapply(tpe: Type)(using ctx: Context): Option[Type] = tpe match {
14371437
case AppliedType(seq, (tp: Type) :: Nil) if seq.typeSymbol == ctx.requiredClass("scala.collection.Seq") => Some(tp)
14381438
case _ => None
14391439
}
14401440
}
14411441

14421442
object Repeated {
1443-
def unapply(tpe: Type)(given ctx: Context): Option[Type] = tpe match {
1443+
def unapply(tpe: Type)(using ctx: Context): Option[Type] = tpe match {
14441444
case AppliedType(rep, (tp: Type) :: Nil) if rep.typeSymbol == ctx.requiredClass("scala.<repeated>") => Some(tp)
14451445
case _ => None
14461446
}
@@ -1449,7 +1449,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14491449
}
14501450

14511451
object PackageObject {
1452-
def unapply(tree: Tree)(given ctx: Context): Option[Tree] = tree match {
1452+
def unapply(tree: Tree)(using ctx: Context): Option[Tree] = tree match {
14531453
case PackageClause(_, ValDef("package", _, _) :: body :: Nil) => Some(body)
14541454
case _ => None
14551455
}

0 commit comments

Comments
 (0)