Skip to content

Commit 9d8db4d

Browse files
committed
Refactor TypeRef extractor
Make it closer to NamedTypeRef extractor
1 parent 56c877c commit 9d8db4d

File tree

10 files changed

+123
-131
lines changed

10 files changed

+123
-131
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,14 +1175,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11751175

11761176
def TypeRef_qualifier(self: TypeRef) given Context: TypeOrBounds = self.prefix
11771177

1178-
def matchTypeRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
1179-
case tpe: Types.NamedType =>
1180-
tpe.designator match {
1181-
case sym: Symbol if sym.isType => Some((sym, tpe.prefix))
1182-
case _ => None
1183-
}
1184-
case _ => None
1185-
}
1178+
def TypeRef_name(self: TypeRef) given Context: String = self.name.toString
11861179

11871180
type NamedTermRef = Types.NamedType
11881181

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,8 @@ trait CompilerInterface {
942942

943943
def matchTypeRef(tpe: TypeOrBounds) given (ctx: Context): Option[TypeRef]
944944

945-
def matchTypeRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
946-
947945
def TypeRef_qualifier(self: TypeRef) given (ctx: Context): TypeOrBounds
946+
def TypeRef_name(self: TypeRef) given Context: String
948947

949948
/** Type of a reference to a term by it's name */
950949
type NamedTermRef <: Type

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ trait Printers
310310
this += "Type.ConstantType(" += value += ")"
311311
case Type.TermRef(qual, name) =>
312312
this += "Type.TermRef(" += qual+= ", \"" += name += "\")"
313-
case Type.TypeRef(sym, qual) =>
314-
this += "Type.TypeRef(" += sym += ", " += qual += ")"
313+
case Type.TypeRef(qual, name) =>
314+
this += "Type.TypeRef(" += qual += ", \"" += name += "\")"
315315
case Type.NamedTermRef(name, qual) =>
316316
this += "Type.NamedTermRef(\"" += name += "\", " += qual += ")"
317317
case Type.NamedTypeRef(name, qual) =>
@@ -1420,7 +1420,7 @@ trait Printers
14201420
printTypeAndAnnots(tp)
14211421
this += " "
14221422
printAnnotation(annot)
1423-
case Type.TypeRef(IsClassDefSymbol(sym), _) if sym.fullName == "scala.runtime.Null$" || sym.fullName == "scala.runtime.Nothing$" =>
1423+
case Type.IsTypeRef(tpe) if tpe.typeSymbol.fullName == "scala.runtime.Null$" || tpe.typeSymbol.fullName == "scala.runtime.Nothing$" =>
14241424
// scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names
14251425
printType(tpe)
14261426
case Type.IsTermRef(tpe) if tpe.termSymbol.isClass && tpe.termSymbol.name.endsWith("$") =>
@@ -1533,7 +1533,7 @@ trait Printers
15331533
case Type.IsTermRef(prefix) if prefix.termSymbol.isClass =>
15341534
printType(prefix)
15351535
this += "#"
1536-
case IsType(prefix @ Type.TypeRef(IsClassDefSymbol(_), _)) =>
1536+
case Type.IsTypeRef(prefix) if prefix.typeSymbol.isClass =>
15371537
printType(prefix)
15381538
this += "#"
15391539
case IsType(Type.ThisType(Type.TermRef(cdef, _))) if elideThis.nonEmpty && cdef == elideThis.get =>
@@ -1617,7 +1617,7 @@ trait Printers
16171617

16181618
case Type.ThisType(tp) =>
16191619
tp match {
1620-
case Type.TypeRef(cdef, _) if !cdef.flags.is(Flags.Object) =>
1620+
case Type.IsTypeRef(tp) if !tp.typeSymbol.flags.is(Flags.Object) =>
16211621
printFullClassName(tp)
16221622
this += highlightTypeDef(".this")
16231623
case Type.NamedTypeRef(name, prefix) if name.endsWith("$") =>
@@ -1805,8 +1805,7 @@ trait Printers
18051805
def printProtectedOrPrivate(definition: Definition): Boolean = {
18061806
var prefixWasPrinted = false
18071807
def printWithin(within: Type) = within match {
1808-
case Type.TypeRef(sym, _) =>
1809-
this += sym.name
1808+
case Type.TypeRef(_, name) => this += name
18101809
case _ => printFullClassName(within)
18111810
}
18121811
if (definition.symbol.flags.is(Flags.Protected)) {
@@ -1833,14 +1832,14 @@ trait Printers
18331832

18341833
def printFullClassName(tp: TypeOrBounds): Unit = {
18351834
def printClassPrefix(prefix: TypeOrBounds): Unit = prefix match {
1836-
case Type.TypeRef(IsClassDefSymbol(sym), prefix2) =>
1835+
case Type.TypeRef(prefix2, name) =>
18371836
printClassPrefix(prefix2)
1838-
this += sym.name += "."
1837+
this += name += "."
18391838
case _ =>
18401839
}
1841-
val Type.TypeRef(sym, prefix) = tp
1840+
val Type.TypeRef(prefix, name) = tp
18421841
printClassPrefix(prefix)
1843-
this += sym.name
1842+
this += name
18441843
}
18451844

18461845
def +=(x: Boolean): this.type = { sb.append(x); this }
@@ -1872,8 +1871,8 @@ trait Printers
18721871
def unapply(arg: Tree) given (ctx: Context): Option[(String, List[Term])] = arg match {
18731872
case IsTerm(arg @ Apply(fn, args)) =>
18741873
fn.tpe match {
1875-
case tpe @ Type.TermRef(Type.ThisType(Type.TypeRef(sym2, _)), _) if sym2.name == "<special-ops>" =>
1876-
Some((tpe.termSymbol.asDefDef.tree.name, args))
1874+
case tpe @ Type.TermRef(Type.ThisType(Type.TypeRef(_, name)), name2) if name == "<special-ops>" =>
1875+
Some((name2, args))
18771876
case _ => None
18781877
}
18791878
case _ => None

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ trait TypeOrBoundsOps extends Core {
9696
}
9797

9898
object TypeRef {
99-
def unapply(typeOrBounds: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)] =
100-
internal.matchTypeRef_unapply(typeOrBounds)
99+
def unapply(typeOrBounds: TypeOrBounds) given (ctx: Context): Option[(TypeOrBounds /* Type | NoPrefix */, String)] =
100+
internal.matchTypeRef(typeOrBounds).map(x => (x.qualifier, x.name))
101101
}
102102

103103
object IsNamedTermRef {
@@ -305,6 +305,7 @@ trait TypeOrBoundsOps extends Core {
305305

306306
implicit class Type_TypeRefAPI(self: TypeRef) {
307307
def qualifier given (ctx: Context): TypeOrBounds /* Type | NoPrefix */ = internal.TypeRef_qualifier(self)
308+
def name given (ctx: Context): String = internal.TypeRef_name(self)
308309
}
309310

310311
implicit class Type_NamedTermRefAPI(self: NamedTermRef) {

tests/run-macros/i5941/macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ object Iso {
102102
val cls = tpS.classSymbol.get
103103

104104
val companion = tpS match {
105-
case Type.TypeRef(sym, prefix) => Type.NamedTermRef(prefix, sym.name)
105+
case Type.TypeRef(prefix, name) => Type.NamedTermRef(prefix, name)
106106
case Type.NamedTypeRef(name, prefix) => Type.NamedTermRef(prefix, name)
107107
}
108108

@@ -145,7 +145,7 @@ object Iso {
145145
}
146146

147147
val companion = tpS match {
148-
case Type.TypeRef(sym, prefix) => Type.NamedTermRef(prefix, sym.name)
148+
case Type.TypeRef(prefix, name) => Type.NamedTermRef(prefix, name)
149149
case Type.NamedTypeRef(name, prefix) => Type.NamedTermRef(prefix, name)
150150
}
151151

tests/run-macros/tasty-definitions-1.check

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,18 @@ Tuple21
155155
Tuple22
156156
List(Unit, Boolean, Byte, Short, Int, Long, Float, Double, Char)
157157
List(Byte, Short, Int, Long, Float, Double, Char)
158-
Type.TypeRef(IsClassDefSymbol(<scala.Unit>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
159-
Type.TypeRef(IsClassDefSymbol(<scala.Byte>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
160-
Type.TypeRef(IsClassDefSymbol(<scala.Char>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
161-
Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
162-
Type.TypeRef(IsClassDefSymbol(<scala.Long>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
163-
Type.TypeRef(IsClassDefSymbol(<scala.Float>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
164-
Type.TypeRef(IsClassDefSymbol(<scala.Double>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
165-
Type.TypeRef(IsClassDefSymbol(<scala.Boolean>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
166-
Type.TypeRef(IsClassDefSymbol(<scala.Any>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
167-
Type.TypeRef(IsClassDefSymbol(<scala.AnyVal>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
168-
Type.TypeRef(IsTypeDefSymbol(<scala.AnyRef>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
169-
Type.TypeRef(IsClassDefSymbol(<java.lang.Object>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<java.lang>), NoPrefix())))
170-
Type.TypeRef(IsClassDefSymbol(<scala.Nothing>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
171-
Type.TypeRef(IsClassDefSymbol(<scala.Null>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
172-
Type.TypeRef(IsClassDefSymbol(<java.lang.String>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<java.lang>), NoPrefix())))
158+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Unit")
159+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Byte")
160+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Char")
161+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Int")
162+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Long")
163+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Float")
164+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Double")
165+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Boolean")
166+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Any")
167+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "AnyVal")
168+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "AnyRef")
169+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "lang")), "Object")
170+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Nothing")
171+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "scala")), "Null")
172+
Type.TypeRef(Type.ThisType(Type.TypeRef(NoPrefix(), "lang")), "String")

0 commit comments

Comments
 (0)