diff --git a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala index 2afd706e318f..8fa32de0db5c 100644 --- a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala +++ b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala @@ -480,15 +480,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: object This extends ThisModule: def apply(cls: Symbol): This = withDefaultPos(tpd.This(cls.asClass)) - def copy(original: Tree)(qual: Option[Id]): This = - tpd.cpy.This(original)(qual.getOrElse(untpd.EmptyTypeIdent)) - def unapply(x: This): Option[Option[Id]] = - Some(x.id) + def copy(original: Tree)(qual: Option[String]): This = + tpd.cpy.This(original)(qual.map(x => untpd.Ident(x.toTypeName)).getOrElse(untpd.EmptyTypeIdent)) + def unapply(x: This): Option[Option[String]] = + Some(optional(x.qual).map(_.name.toString)) end This object ThisMethodsImpl extends ThisMethods: extension (self: This): - def id: Option[Id] = optional(self.qual) + def id: Option[String] = optional(self.qual).map(_.name.toString) end extension end ThisMethodsImpl @@ -600,18 +600,19 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: end SuperTypeTest object Super extends SuperModule: - def apply(qual: Term, mix: Option[Id]): Super = - withDefaultPos(tpd.Super(qual, mix.getOrElse(untpd.EmptyTypeIdent), dotc.core.Symbols.NoSymbol)) - def copy(original: Tree)(qual: Term, mix: Option[Id]): Super = - tpd.cpy.Super(original)(qual, mix.getOrElse(untpd.EmptyTypeIdent)) - def unapply(x: Super): Option[(Term, Option[Id])] = + def apply(qual: Term, mix: Option[String]): Super = + withDefaultPos(tpd.Super(qual, mix.map(x => untpd.Ident(x.toTypeName)).getOrElse(untpd.EmptyTypeIdent), dotc.core.Symbols.NoSymbol)) + def copy(original: Tree)(qual: Term, mix: Option[String]): Super = + tpd.cpy.Super(original)(qual, mix.map(x => untpd.Ident(x.toTypeName)).getOrElse(untpd.EmptyTypeIdent)) + def unapply(x: Super): Option[(Term, Option[String])] = Some((x.qualifier, x.id)) end Super object SuperMethodsImpl extends SuperMethods: extension (self: Super): def qualifier: Term = self.qual - def id: Option[Id] = optional(self.mix) + def id: Option[String] = optional(self.mix).map(_.name.toString) + def idPos: Position = self.mix.sourcePos end extension end SuperMethodsImpl @@ -1509,13 +1510,14 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: end SimpleSelectorTypeTest object SimpleSelector extends SimpleSelectorModule: - def unapply(x: SimpleSelector): Option[Id] = Some(x.selection) + def unapply(x: SimpleSelector): Option[String] = Some(x.name.toString) end SimpleSelector object SimpleSelectorMethodsImpl extends SimpleSelectorMethods: extension (self: SimpleSelector): - def selection: Id = self.imported + def name: String = self.imported.name.toString + def namePos: Position = self.imported.sourcePos end extension end SimpleSelectorMethodsImpl @@ -1529,13 +1531,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: end RenameSelectorTypeTest object RenameSelector extends RenameSelectorModule: - def unapply(x: RenameSelector): Option[(Id, Id)] = Some((x.from, x.to)) + def unapply(x: RenameSelector): Option[(String, String)] = Some((x.fromName, x.toName)) end RenameSelector object RenameSelectorMethodsImpl extends RenameSelectorMethods: extension (self: RenameSelector): - def from: Id = self.imported - def to: Id = self.renamed.asInstanceOf[untpd.Ident] + def fromName: String = self.imported.name.toString + def fromPos: Position = self.imported.sourcePos + def toName: String = self.renamed.asInstanceOf[untpd.Ident].name.toString + def toPos: Position = self.renamed.asInstanceOf[untpd.Ident].sourcePos end extension end RenameSelectorMethodsImpl @@ -1553,12 +1557,13 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: end OmitSelectorTypeTest object OmitSelector extends OmitSelectorModule: - def unapply(x: OmitSelector): Option[Id] = Some(x.omitted) + def unapply(x: OmitSelector): Option[String] = Some(x.imported.name.toString) end OmitSelector object OmitSelectorMethodsImpl extends OmitSelectorMethods: extension (self: OmitSelector): - def omitted: Id = self.imported + def name: String = self.imported.toString + def namePos: Position = self.imported.sourcePos end extension end OmitSelectorMethodsImpl @@ -2105,19 +2110,6 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: end extension end ConstantMethodsImpl - type Id = untpd.Ident - - object Id extends IdModule: - def unapply(id: Id): Option[String] = Some(id.name.toString) - end Id - - object IdMethodsImpl extends IdMethods: - extension (self: Id): - def pos: Position = self.sourcePos - def name: String = self.name.toString - end extension - end IdMethodsImpl - type ImplicitSearchResult = Tree def searchImplicit(tpe: Type): ImplicitSearchResult = diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index 4cab6e126bc4..d29c479fcea2 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -101,8 +101,6 @@ import scala.tasty.reflect._ * +- RenameSelector * +- OmitSelector * - * +- Id - * * +- Signature * * +- Position @@ -586,13 +584,13 @@ trait Reflection { reflection => trait ThisModule { this: This.type => - /** Create a `this[` */ + /** Create a `this[` */ def apply(cls: Symbol): This - def copy(original: Tree)(qual: Option[Id]): This + def copy(original: Tree)(qual: Option[String]): This - /** Matches `this[` */ - def unapply(x: This): Option[Option[Id]] + /** Matches `this[` */ + def unapply(x: This): Option[Option[String]] } given ThisMethods as ThisMethods = ThisMethodsImpl @@ -600,7 +598,7 @@ trait Reflection { reflection => trait ThisMethods: extension (self: This): - def id: Option[Id] + def id: Option[String] end extension end ThisMethods @@ -735,12 +733,12 @@ trait Reflection { reflection => trait SuperModule { this: Super.type => /** Creates a `.super[` */ - def apply(qual: Term, mix: Option[Id]): Super + def apply(qual: Term, mix: Option[String]): Super - def copy(original: Tree)(qual: Term, mix: Option[Id]): Super + def copy(original: Tree)(qual: Term, mix: Option[String]): Super /** Matches a `.super[` */ - def unapply(x: Super): Option[(Term, Option[Id])] + def unapply(x: Super): Option[(Term, Option[String])] } given SuperMethods as SuperMethods = SuperMethodsImpl @@ -749,7 +747,8 @@ trait Reflection { reflection => trait SuperMethods: extension (self: Super): def qualifier: Term - def id: Option[Id] + def id: Option[String] + def idPos: Position end extension end SuperMethods @@ -1668,7 +1667,7 @@ trait Reflection { reflection => val SimpleSelector: SimpleSelectorModule trait SimpleSelectorModule { this: SimpleSelector.type => - def unapply(x: SimpleSelector): Option[Id] + def unapply(x: SimpleSelector): Option[String] } /** Simple import selector: `.bar` in `import foo.bar` */ @@ -1679,7 +1678,8 @@ trait Reflection { reflection => trait SimpleSelectorMethods: extension (self: SimpleSelector): - def selection: Id + def name: String + def namePos: Position end extension end SimpleSelectorMethods @@ -1692,7 +1692,7 @@ trait Reflection { reflection => val RenameSelector: RenameSelectorModule trait RenameSelectorModule { this: RenameSelector.type => - def unapply(x: RenameSelector): Option[(Id, Id)] + def unapply(x: RenameSelector): Option[(String, String)] } /** Omit import selector: `.{bar => _}` in `import foo.{bar => _}` */ @@ -1703,8 +1703,10 @@ trait Reflection { reflection => trait RenameSelectorMethods: extension (self: RenameSelector): - def from: Id - def to: Id + def fromName: String + def fromPos: Position + def toName: String + def toPos: Position end extension end RenameSelectorMethods @@ -1714,7 +1716,7 @@ trait Reflection { reflection => val OmitSelector: OmitSelectorModule trait OmitSelectorModule { this: OmitSelector.type => - def unapply(x: OmitSelector): Option[Id] + def unapply(x: OmitSelector): Option[String] } given OmitSelectorMethods as OmitSelectorMethods = OmitSelectorMethodsImpl @@ -1722,7 +1724,8 @@ trait Reflection { reflection => trait OmitSelectorMethods: extension (self: OmitSelector): - def omitted: Id + def name: String + def namePos: Position end OmitSelectorMethods /////////////// @@ -2377,34 +2380,6 @@ trait Reflection { reflection => end extension } - ///////// - // IDs // - ///////// - - // TODO: remove Id. Add use name and pos directly on APIs that use it. - - /** Untyped identifier */ - type Id <: AnyRef - - val Id: IdModule - - trait IdModule { this: Id.type => - def unapply(id: Id): Option[String] - } - - given IdMethods as IdMethods = IdMethodsImpl - protected val IdMethodsImpl: IdMethods - - trait IdMethods { - extension (self: Id): - /** Position in the source code */ - def pos: Position - - /** Name of the identifier */ - def name: String - end extension - } - ///////////////////// // IMPLICIT SEARCH // ///////////////////// diff --git a/library/src/scala/tasty/reflect/ExtractorsPrinter.scala b/library/src/scala/tasty/reflect/ExtractorsPrinter.scala index 81e2fac2f914..464e7a4f63be 100644 --- a/library/src/scala/tasty/reflect/ExtractorsPrinter.scala +++ b/library/src/scala/tasty/reflect/ExtractorsPrinter.scala @@ -227,11 +227,6 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print this += "NoPrefix()" } - def visitId(x: Id): Buffer = { - val Id(name) = x - this += "Id(\"" += name += "\")" - } - def visitSignature(sig: Signature): Buffer = { val Signature(params, res) = sig this += "Signature(" ++= params.map(_.toString) += ", " += res += ")" @@ -263,6 +258,10 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print def ++=(xs: List[String]): Buffer = visitList[String](xs, +=) + private implicit class StringOps(buff: Buffer) { + def +=(x: Option[String]): Buffer = { visitOption(x, y => buff += "\"" += y += "\""); buff } + } + private implicit class TreeOps(buff: Buffer) { def +=(x: Tree): Buffer = { visitTree(x); buff } def +=(x: Option[Tree]): Buffer = { visitOption(x, visitTree); buff } @@ -280,11 +279,6 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print def ++=(x: List[Type]): Buffer = { visitList(x, visitType); buff } } - private implicit class IdOps(buff: Buffer) { - def +=(x: Id): Buffer = { visitId(x); buff } - def +=(x: Option[Id]): Buffer = { visitOption(x, visitId); buff } - } - private implicit class SignatureOps(buff: Buffer) { def +=(x: Option[Signature]): Buffer = { visitOption(x, visitSignature); buff } } diff --git a/library/src/scala/tasty/reflect/SourceCodePrinter.scala b/library/src/scala/tasty/reflect/SourceCodePrinter.scala index da0775577bf9..54d13d6dd0ed 100644 --- a/library/src/scala/tasty/reflect/SourceCodePrinter.scala +++ b/library/src/scala/tasty/reflect/SourceCodePrinter.scala @@ -340,8 +340,8 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case This(id) => id match { - case Some(x) => - this += x.name.stripSuffix("$") += "." + case Some(name) => + this += name.stripSuffix("$") += "." case None => } this += "this" @@ -417,12 +417,12 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case Super(qual, idOpt) => qual match { - case This(Some(Id(name))) => this += name += "." + case This(Some(name)) => this += name += "." case This(None) => } this += "super" for (id <- idOpt) - inSquare(this += id.name) + inSquare(this += id) this case Typed(term, tpt) => @@ -1223,9 +1223,9 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } def printImportSelector(sel: ImportSelector): Buffer = sel match { - case SimpleSelector(Id(name)) => this += name - case OmitSelector(Id(name)) => this += name += " => _" - case RenameSelector(Id(name), Id(newName)) => this += name += " => " += newName + case SimpleSelector(name) => this += name + case OmitSelector(name) => this += name += " => _" + case RenameSelector(name, newName) => this += name += " => " += newName } def printDefinitionName(sym: Definition): Buffer = sym match { diff --git a/tests/run-macros/tasty-extractors-2.check b/tests/run-macros/tasty-extractors-2.check index beda96a5e14d..f03e795c3bc4 100644 --- a/tests/run-macros/tasty-extractors-2.check +++ b/tests/run-macros/tasty-extractors-2.check @@ -70,7 +70,7 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit") Inlined(None, Nil, Block(List(ClassDef("Foo6", DefDef("", Nil, List(List(ValDef("a", TypeIdent("Int"), None)), List(ValDef("b", Singleton(Ident("a")), None))), Inferred(), None), List(Apply(Select(New(Inferred()), ""), Nil)), Nil, None, List(ValDef("a", Inferred(), None), ValDef("b", Inferred(), None)))), Literal(Constant(())))) TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit") -Inlined(None, Nil, Block(List(ClassDef("Foo7", DefDef("", Nil, List(List(ValDef("a", TypeIdent("Int"), None))), Inferred(), None), List(Apply(Select(New(Inferred()), ""), Nil)), Nil, None, List(ValDef("a", Inferred(), None), DefDef("", Nil, List(Nil), Inferred(), Some(Block(List(Apply(Select(This(Some(Id("Foo7"))), ""), List(Literal(Constant(6))))), Literal(Constant(())))))))), Literal(Constant(())))) +Inlined(None, Nil, Block(List(ClassDef("Foo7", DefDef("", Nil, List(List(ValDef("a", TypeIdent("Int"), None))), Inferred(), None), List(Apply(Select(New(Inferred()), ""), Nil)), Nil, None, List(ValDef("a", Inferred(), None), DefDef("", Nil, List(Nil), Inferred(), Some(Block(List(Apply(Select(This(Some("Foo7")), ""), List(Literal(Constant(6))))), Literal(Constant(())))))))), Literal(Constant(())))) TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit") Inlined(None, Nil, Block(List(ClassDef("Foo8", DefDef("", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), ""), Nil)), Nil, None, List(Apply(Ident("println"), List(Literal(Constant(0))))))), Literal(Constant(()))))