Skip to content

Commit 07d3e2d

Browse files
authored
Merge pull request #9857 from dotty-staging/remove-Reflection-Id
Remove Reflection.Id
2 parents 042ba93 + 00ffa01 commit 07d3e2d

File tree

5 files changed

+56
-95
lines changed

5 files changed

+56
-95
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
480480
object This extends ThisModule:
481481
def apply(cls: Symbol): This =
482482
withDefaultPos(tpd.This(cls.asClass))
483-
def copy(original: Tree)(qual: Option[Id]): This =
484-
tpd.cpy.This(original)(qual.getOrElse(untpd.EmptyTypeIdent))
485-
def unapply(x: This): Option[Option[Id]] =
486-
Some(x.id)
483+
def copy(original: Tree)(qual: Option[String]): This =
484+
tpd.cpy.This(original)(qual.map(x => untpd.Ident(x.toTypeName)).getOrElse(untpd.EmptyTypeIdent))
485+
def unapply(x: This): Option[Option[String]] =
486+
Some(optional(x.qual).map(_.name.toString))
487487
end This
488488

489489
object ThisMethodsImpl extends ThisMethods:
490490
extension (self: This):
491-
def id: Option[Id] = optional(self.qual)
491+
def id: Option[String] = optional(self.qual).map(_.name.toString)
492492
end extension
493493
end ThisMethodsImpl
494494

@@ -600,18 +600,19 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
600600
end SuperTypeTest
601601

602602
object Super extends SuperModule:
603-
def apply(qual: Term, mix: Option[Id]): Super =
604-
withDefaultPos(tpd.Super(qual, mix.getOrElse(untpd.EmptyTypeIdent), dotc.core.Symbols.NoSymbol))
605-
def copy(original: Tree)(qual: Term, mix: Option[Id]): Super =
606-
tpd.cpy.Super(original)(qual, mix.getOrElse(untpd.EmptyTypeIdent))
607-
def unapply(x: Super): Option[(Term, Option[Id])] =
603+
def apply(qual: Term, mix: Option[String]): Super =
604+
withDefaultPos(tpd.Super(qual, mix.map(x => untpd.Ident(x.toTypeName)).getOrElse(untpd.EmptyTypeIdent), dotc.core.Symbols.NoSymbol))
605+
def copy(original: Tree)(qual: Term, mix: Option[String]): Super =
606+
tpd.cpy.Super(original)(qual, mix.map(x => untpd.Ident(x.toTypeName)).getOrElse(untpd.EmptyTypeIdent))
607+
def unapply(x: Super): Option[(Term, Option[String])] =
608608
Some((x.qualifier, x.id))
609609
end Super
610610

611611
object SuperMethodsImpl extends SuperMethods:
612612
extension (self: Super):
613613
def qualifier: Term = self.qual
614-
def id: Option[Id] = optional(self.mix)
614+
def id: Option[String] = optional(self.mix).map(_.name.toString)
615+
def idPos: Position = self.mix.sourcePos
615616
end extension
616617
end SuperMethodsImpl
617618

@@ -1510,13 +1511,14 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15101511
end SimpleSelectorTypeTest
15111512

15121513
object SimpleSelector extends SimpleSelectorModule:
1513-
def unapply(x: SimpleSelector): Option[Id] = Some(x.selection)
1514+
def unapply(x: SimpleSelector): Option[String] = Some(x.name.toString)
15141515
end SimpleSelector
15151516

15161517

15171518
object SimpleSelectorMethodsImpl extends SimpleSelectorMethods:
15181519
extension (self: SimpleSelector):
1519-
def selection: Id = self.imported
1520+
def name: String = self.imported.name.toString
1521+
def namePos: Position = self.imported.sourcePos
15201522
end extension
15211523
end SimpleSelectorMethodsImpl
15221524

@@ -1530,13 +1532,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15301532
end RenameSelectorTypeTest
15311533

15321534
object RenameSelector extends RenameSelectorModule:
1533-
def unapply(x: RenameSelector): Option[(Id, Id)] = Some((x.from, x.to))
1535+
def unapply(x: RenameSelector): Option[(String, String)] = Some((x.fromName, x.toName))
15341536
end RenameSelector
15351537

15361538
object RenameSelectorMethodsImpl extends RenameSelectorMethods:
15371539
extension (self: RenameSelector):
1538-
def from: Id = self.imported
1539-
def to: Id = self.renamed.asInstanceOf[untpd.Ident]
1540+
def fromName: String = self.imported.name.toString
1541+
def fromPos: Position = self.imported.sourcePos
1542+
def toName: String = self.renamed.asInstanceOf[untpd.Ident].name.toString
1543+
def toPos: Position = self.renamed.asInstanceOf[untpd.Ident].sourcePos
15401544
end extension
15411545
end RenameSelectorMethodsImpl
15421546

@@ -1554,12 +1558,13 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15541558
end OmitSelectorTypeTest
15551559

15561560
object OmitSelector extends OmitSelectorModule:
1557-
def unapply(x: OmitSelector): Option[Id] = Some(x.omitted)
1561+
def unapply(x: OmitSelector): Option[String] = Some(x.imported.name.toString)
15581562
end OmitSelector
15591563

15601564
object OmitSelectorMethodsImpl extends OmitSelectorMethods:
15611565
extension (self: OmitSelector):
1562-
def omitted: Id = self.imported
1566+
def name: String = self.imported.toString
1567+
def namePos: Position = self.imported.sourcePos
15631568
end extension
15641569
end OmitSelectorMethodsImpl
15651570

@@ -2106,19 +2111,6 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21062111
end extension
21072112
end ConstantMethodsImpl
21082113

2109-
type Id = untpd.Ident
2110-
2111-
object Id extends IdModule:
2112-
def unapply(id: Id): Option[String] = Some(id.name.toString)
2113-
end Id
2114-
2115-
object IdMethodsImpl extends IdMethods:
2116-
extension (self: Id):
2117-
def pos: Position = self.sourcePos
2118-
def name: String = self.name.toString
2119-
end extension
2120-
end IdMethodsImpl
2121-
21222114
type ImplicitSearchResult = Tree
21232115

21242116
def searchImplicit(tpe: Type): ImplicitSearchResult =

library/src/scala/tasty/Reflection.scala

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ import scala.tasty.reflect._
101101
* +- RenameSelector
102102
* +- OmitSelector
103103
*
104-
* +- Id
105-
*
106104
* +- Signature
107105
*
108106
* +- Position
@@ -586,21 +584,21 @@ trait Reflection { reflection =>
586584

587585
trait ThisModule { this: This.type =>
588586

589-
/** Create a `this[<id: Id]>` */
587+
/** Create a `this[<id: String]>` */
590588
def apply(cls: Symbol): This
591589

592-
def copy(original: Tree)(qual: Option[Id]): This
590+
def copy(original: Tree)(qual: Option[String]): This
593591

594-
/** Matches `this[<id: Option[Id]>` */
595-
def unapply(x: This): Option[Option[Id]]
592+
/** Matches `this[<id: Option[String]>` */
593+
def unapply(x: This): Option[Option[String]]
596594
}
597595

598596
given ThisMethods as ThisMethods = ThisMethodsImpl
599597
protected val ThisMethodsImpl: ThisMethods
600598

601599
trait ThisMethods:
602600
extension (self: This):
603-
def id: Option[Id]
601+
def id: Option[String]
604602
end extension
605603
end ThisMethods
606604

@@ -735,12 +733,12 @@ trait Reflection { reflection =>
735733
trait SuperModule { this: Super.type =>
736734

737735
/** Creates a `<qualifier: Term>.super[<id: Option[Id]>` */
738-
def apply(qual: Term, mix: Option[Id]): Super
736+
def apply(qual: Term, mix: Option[String]): Super
739737

740-
def copy(original: Tree)(qual: Term, mix: Option[Id]): Super
738+
def copy(original: Tree)(qual: Term, mix: Option[String]): Super
741739

742740
/** Matches a `<qualifier: Term>.super[<id: Option[Id]>` */
743-
def unapply(x: Super): Option[(Term, Option[Id])]
741+
def unapply(x: Super): Option[(Term, Option[String])]
744742
}
745743

746744
given SuperMethods as SuperMethods = SuperMethodsImpl
@@ -749,7 +747,8 @@ trait Reflection { reflection =>
749747
trait SuperMethods:
750748
extension (self: Super):
751749
def qualifier: Term
752-
def id: Option[Id]
750+
def id: Option[String]
751+
def idPos: Position
753752
end extension
754753
end SuperMethods
755754

@@ -1669,7 +1668,7 @@ trait Reflection { reflection =>
16691668
val SimpleSelector: SimpleSelectorModule
16701669

16711670
trait SimpleSelectorModule { this: SimpleSelector.type =>
1672-
def unapply(x: SimpleSelector): Option[Id]
1671+
def unapply(x: SimpleSelector): Option[String]
16731672
}
16741673

16751674
/** Simple import selector: `.bar` in `import foo.bar` */
@@ -1680,7 +1679,8 @@ trait Reflection { reflection =>
16801679

16811680
trait SimpleSelectorMethods:
16821681
extension (self: SimpleSelector):
1683-
def selection: Id
1682+
def name: String
1683+
def namePos: Position
16841684
end extension
16851685
end SimpleSelectorMethods
16861686

@@ -1693,7 +1693,7 @@ trait Reflection { reflection =>
16931693
val RenameSelector: RenameSelectorModule
16941694

16951695
trait RenameSelectorModule { this: RenameSelector.type =>
1696-
def unapply(x: RenameSelector): Option[(Id, Id)]
1696+
def unapply(x: RenameSelector): Option[(String, String)]
16971697
}
16981698

16991699
/** Omit import selector: `.{bar => _}` in `import foo.{bar => _}` */
@@ -1704,8 +1704,10 @@ trait Reflection { reflection =>
17041704

17051705
trait RenameSelectorMethods:
17061706
extension (self: RenameSelector):
1707-
def from: Id
1708-
def to: Id
1707+
def fromName: String
1708+
def fromPos: Position
1709+
def toName: String
1710+
def toPos: Position
17091711
end extension
17101712
end RenameSelectorMethods
17111713

@@ -1715,15 +1717,16 @@ trait Reflection { reflection =>
17151717
val OmitSelector: OmitSelectorModule
17161718

17171719
trait OmitSelectorModule { this: OmitSelector.type =>
1718-
def unapply(x: OmitSelector): Option[Id]
1720+
def unapply(x: OmitSelector): Option[String]
17191721
}
17201722

17211723
given OmitSelectorMethods as OmitSelectorMethods = OmitSelectorMethodsImpl
17221724
protected val OmitSelectorMethodsImpl: OmitSelectorMethods
17231725

17241726
trait OmitSelectorMethods:
17251727
extension (self: OmitSelector):
1726-
def omitted: Id
1728+
def name: String
1729+
def namePos: Position
17271730
end OmitSelectorMethods
17281731

17291732
///////////////
@@ -2378,34 +2381,6 @@ trait Reflection { reflection =>
23782381
end extension
23792382
}
23802383

2381-
/////////
2382-
// IDs //
2383-
/////////
2384-
2385-
// TODO: remove Id. Add use name and pos directly on APIs that use it.
2386-
2387-
/** Untyped identifier */
2388-
type Id <: AnyRef
2389-
2390-
val Id: IdModule
2391-
2392-
trait IdModule { this: Id.type =>
2393-
def unapply(id: Id): Option[String]
2394-
}
2395-
2396-
given IdMethods as IdMethods = IdMethodsImpl
2397-
protected val IdMethodsImpl: IdMethods
2398-
2399-
trait IdMethods {
2400-
extension (self: Id):
2401-
/** Position in the source code */
2402-
def pos: Position
2403-
2404-
/** Name of the identifier */
2405-
def name: String
2406-
end extension
2407-
}
2408-
24092384
/////////////////////
24102385
// IMPLICIT SEARCH //
24112386
/////////////////////

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,6 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print
227227
this += "NoPrefix()"
228228
}
229229

230-
def visitId(x: Id): Buffer = {
231-
val Id(name) = x
232-
this += "Id(\"" += name += "\")"
233-
}
234-
235230
def visitSignature(sig: Signature): Buffer = {
236231
val Signature(params, res) = sig
237232
this += "Signature(" ++= params.map(_.toString) += ", " += res += ")"
@@ -263,6 +258,10 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print
263258

264259
def ++=(xs: List[String]): Buffer = visitList[String](xs, +=)
265260

261+
private implicit class StringOps(buff: Buffer) {
262+
def +=(x: Option[String]): Buffer = { visitOption(x, y => buff += "\"" += y += "\""); buff }
263+
}
264+
266265
private implicit class TreeOps(buff: Buffer) {
267266
def +=(x: Tree): Buffer = { visitTree(x); buff }
268267
def +=(x: Option[Tree]): Buffer = { visitOption(x, visitTree); buff }
@@ -280,11 +279,6 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print
280279
def ++=(x: List[Type]): Buffer = { visitList(x, visitType); buff }
281280
}
282281

283-
private implicit class IdOps(buff: Buffer) {
284-
def +=(x: Id): Buffer = { visitId(x); buff }
285-
def +=(x: Option[Id]): Buffer = { visitOption(x, visitId); buff }
286-
}
287-
288282
private implicit class SignatureOps(buff: Buffer) {
289283
def +=(x: Option[Signature]): Buffer = { visitOption(x, visitSignature); buff }
290284
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
340340

341341
case This(id) =>
342342
id match {
343-
case Some(x) =>
344-
this += x.name.stripSuffix("$") += "."
343+
case Some(name) =>
344+
this += name.stripSuffix("$") += "."
345345
case None =>
346346
}
347347
this += "this"
@@ -417,12 +417,12 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
417417

418418
case Super(qual, idOpt) =>
419419
qual match {
420-
case This(Some(Id(name))) => this += name += "."
420+
case This(Some(name)) => this += name += "."
421421
case This(None) =>
422422
}
423423
this += "super"
424424
for (id <- idOpt)
425-
inSquare(this += id.name)
425+
inSquare(this += id)
426426
this
427427

428428
case Typed(term, tpt) =>
@@ -1223,9 +1223,9 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12231223
}
12241224

12251225
def printImportSelector(sel: ImportSelector): Buffer = sel match {
1226-
case SimpleSelector(Id(name)) => this += name
1227-
case OmitSelector(Id(name)) => this += name += " => _"
1228-
case RenameSelector(Id(name), Id(newName)) => this += name += " => " += newName
1226+
case SimpleSelector(name) => this += name
1227+
case OmitSelector(name) => this += name += " => _"
1228+
case RenameSelector(name, newName) => this += name += " => " += newName
12291229
}
12301230

12311231
def printDefinitionName(sym: Definition): Buffer = sym match {

tests/run-macros/tasty-extractors-2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
7070
Inlined(None, Nil, Block(List(ClassDef("Foo6", DefDef("<init>", Nil, List(List(ValDef("a", TypeIdent("Int"), None)), List(ValDef("b", Singleton(Ident("a")), None))), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil)), Nil, None, List(ValDef("a", Inferred(), None), ValDef("b", Inferred(), None)))), Literal(Constant(()))))
7171
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
7272

73-
Inlined(None, Nil, Block(List(ClassDef("Foo7", DefDef("<init>", Nil, List(List(ValDef("a", TypeIdent("Int"), None))), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil)), Nil, None, List(ValDef("a", Inferred(), None), DefDef("<init>", Nil, List(Nil), Inferred(), Some(Block(List(Apply(Select(This(Some(Id("Foo7"))), "<init>"), List(Literal(Constant(6))))), Literal(Constant(())))))))), Literal(Constant(()))))
73+
Inlined(None, Nil, Block(List(ClassDef("Foo7", DefDef("<init>", Nil, List(List(ValDef("a", TypeIdent("Int"), None))), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil)), Nil, None, List(ValDef("a", Inferred(), None), DefDef("<init>", Nil, List(Nil), Inferred(), Some(Block(List(Apply(Select(This(Some("Foo7")), "<init>"), List(Literal(Constant(6))))), Literal(Constant(())))))))), Literal(Constant(()))))
7474
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
7575

7676
Inlined(None, Nil, Block(List(ClassDef("Foo8", DefDef("<init>", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil)), Nil, None, List(Apply(Ident("println"), List(Literal(Constant(0))))))), Literal(Constant(()))))

0 commit comments

Comments
 (0)