Skip to content

Commit 8fff089

Browse files
committed
Remove Reflection.Id
Id is the only instance of an untyped tree. It can be represented directly within the trees that use it and hence is redundant.
1 parent 9b9c9f8 commit 8fff089

File tree

5 files changed

+56
-93
lines changed

5 files changed

+56
-93
lines changed

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

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

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

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

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

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

@@ -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 & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -586,21 +586,21 @@ trait Reflection { reflection =>
586586

587587
trait ThisModule { this: This.type =>
588588

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

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

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

598598
given ThisMethods as ThisMethods = ThisMethodsImpl
599599
protected val ThisMethodsImpl: ThisMethods
600600

601601
trait ThisMethods:
602602
extension (self: This):
603-
def id: Option[Id]
603+
def id: Option[String]
604604
end extension
605605
end ThisMethods
606606

@@ -735,12 +735,12 @@ trait Reflection { reflection =>
735735
trait SuperModule { this: Super.type =>
736736

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

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

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

746746
given SuperMethods as SuperMethods = SuperMethodsImpl
@@ -749,7 +749,8 @@ trait Reflection { reflection =>
749749
trait SuperMethods:
750750
extension (self: Super):
751751
def qualifier: Term
752-
def id: Option[Id]
752+
def id: Option[String]
753+
def idPos: Position
753754
end extension
754755
end SuperMethods
755756

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

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

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

16801681
trait SimpleSelectorMethods:
16811682
extension (self: SimpleSelector):
1682-
def selection: Id
1683+
def name: String
1684+
def namePos: Position
16831685
end extension
16841686
end SimpleSelectorMethods
16851687

@@ -1692,7 +1694,7 @@ trait Reflection { reflection =>
16921694
val RenameSelector: RenameSelectorModule
16931695

16941696
trait RenameSelectorModule { this: RenameSelector.type =>
1695-
def unapply(x: RenameSelector): Option[(Id, Id)]
1697+
def unapply(x: RenameSelector): Option[(String, String)]
16961698
}
16971699

16981700
/** Omit import selector: `.{bar => _}` in `import foo.{bar => _}` */
@@ -1703,8 +1705,10 @@ trait Reflection { reflection =>
17031705

17041706
trait RenameSelectorMethods:
17051707
extension (self: RenameSelector):
1706-
def from: Id
1707-
def to: Id
1708+
def fromName: String
1709+
def fromPos: Position
1710+
def toName: String
1711+
def toPos: Position
17081712
end extension
17091713
end RenameSelectorMethods
17101714

@@ -1714,15 +1718,16 @@ trait Reflection { reflection =>
17141718
val OmitSelector: OmitSelectorModule
17151719

17161720
trait OmitSelectorModule { this: OmitSelector.type =>
1717-
def unapply(x: OmitSelector): Option[Id]
1721+
def unapply(x: OmitSelector): Option[String]
17181722
}
17191723

17201724
given OmitSelectorMethods as OmitSelectorMethods = OmitSelectorMethodsImpl
17211725
protected val OmitSelectorMethodsImpl: OmitSelectorMethods
17221726

17231727
trait OmitSelectorMethods:
17241728
extension (self: OmitSelector):
1725-
def omitted: Id
1729+
def name: String
1730+
def namePos: Position
17261731
end OmitSelectorMethods
17271732

17281733
///////////////
@@ -2377,34 +2382,6 @@ trait Reflection { reflection =>
23772382
end extension
23782383
}
23792384

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

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)