Skip to content

Remove Reflection.Id #9857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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 =
Expand Down
67 changes: 21 additions & 46 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ import scala.tasty.reflect._
* +- RenameSelector
* +- OmitSelector
*
* +- Id
*
* +- Signature
*
* +- Position
Expand Down Expand Up @@ -586,21 +584,21 @@ trait Reflection { reflection =>

trait ThisModule { this: This.type =>

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

def copy(original: Tree)(qual: Option[Id]): This
def copy(original: Tree)(qual: Option[String]): This

/** Matches `this[<id: Option[Id]>` */
def unapply(x: This): Option[Option[Id]]
/** Matches `this[<id: Option[String]>` */
def unapply(x: This): Option[Option[String]]
}

given ThisMethods as ThisMethods = ThisMethodsImpl
protected val ThisMethodsImpl: ThisMethods

trait ThisMethods:
extension (self: This):
def id: Option[Id]
def id: Option[String]
end extension
end ThisMethods

Expand Down Expand Up @@ -735,12 +733,12 @@ trait Reflection { reflection =>
trait SuperModule { this: Super.type =>

/** Creates a `<qualifier: Term>.super[<id: Option[Id]>` */
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 `<qualifier: Term>.super[<id: Option[Id]>` */
def unapply(x: Super): Option[(Term, Option[Id])]
def unapply(x: Super): Option[(Term, Option[String])]
}

given SuperMethods as SuperMethods = SuperMethodsImpl
Expand All @@ -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

Expand Down Expand Up @@ -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` */
Expand All @@ -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

Expand All @@ -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 => _}` */
Expand All @@ -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

Expand All @@ -1714,15 +1716,16 @@ 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
protected val OmitSelectorMethodsImpl: OmitSelectorMethods

trait OmitSelectorMethods:
extension (self: OmitSelector):
def omitted: Id
def name: String
def namePos: Position
end OmitSelectorMethods

///////////////
Expand Down Expand Up @@ -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 //
/////////////////////
Expand Down
14 changes: 4 additions & 10 deletions library/src/scala/tasty/reflect/ExtractorsPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ")"
Expand Down Expand Up @@ -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 }
Expand All @@ -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 }
}
Expand Down
14 changes: 7 additions & 7 deletions library/src/scala/tasty/reflect/SourceCodePrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-extractors-2.check
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
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(()))))
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")

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(()))))
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(()))))
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")

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(()))))
Expand Down