Skip to content

Commit 5063239

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 5063239

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
@@ -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 & 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

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

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

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

16801679
trait SimpleSelectorMethods:
16811680
extension (self: SimpleSelector):
1682-
def selection: Id
1681+
def name: String
1682+
def namePos: Position
16831683
end extension
16841684
end SimpleSelectorMethods
16851685

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

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

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

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

@@ -1714,15 +1716,16 @@ trait Reflection { reflection =>
17141716
val OmitSelector: OmitSelectorModule
17151717

17161718
trait OmitSelectorModule { this: OmitSelector.type =>
1717-
def unapply(x: OmitSelector): Option[Id]
1719+
def unapply(x: OmitSelector): Option[String]
17181720
}
17191721

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

17231725
trait OmitSelectorMethods:
17241726
extension (self: OmitSelector):
1725-
def omitted: Id
1727+
def name: String
1728+
def namePos: Position
17261729
end OmitSelectorMethods
17271730

17281731
///////////////
@@ -2377,34 +2380,6 @@ trait Reflection { reflection =>
23772380
end extension
23782381
}
23792382

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-
24082383
/////////////////////
24092384
// IMPLICIT SEARCH //
24102385
/////////////////////

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)