Skip to content

Commit 00ffa01

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 01fc4a1 commit 00ffa01

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

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

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

15151516

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

@@ -1529,13 +1531,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15291531
end RenameSelectorTypeTest
15301532

15311533
object RenameSelector extends RenameSelectorModule:
1532-
def unapply(x: RenameSelector): Option[(Id, Id)] = Some((x.from, x.to))
1534+
def unapply(x: RenameSelector): Option[(String, String)] = Some((x.fromName, x.toName))
15331535
end RenameSelector
15341536

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

@@ -1553,12 +1557,13 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15531557
end OmitSelectorTypeTest
15541558

15551559
object OmitSelector extends OmitSelectorModule:
1556-
def unapply(x: OmitSelector): Option[Id] = Some(x.omitted)
1560+
def unapply(x: OmitSelector): Option[String] = Some(x.imported.name.toString)
15571561
end OmitSelector
15581562

15591563
object OmitSelectorMethodsImpl extends OmitSelectorMethods:
15601564
extension (self: OmitSelector):
1561-
def omitted: Id = self.imported
1565+
def name: String = self.imported.toString
1566+
def namePos: Position = self.imported.sourcePos
15621567
end extension
15631568
end OmitSelectorMethodsImpl
15641569

@@ -2105,19 +2110,6 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21052110
end extension
21062111
end ConstantMethodsImpl
21072112

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

21232115
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)