Skip to content

Commit 9f0594c

Browse files
committed
Merge pull request scala#3279 from xeno-by/topic/cosmetics
cosmetic changes to liftables
2 parents ed9b95b + 3ef5837 commit 9f0594c

File tree

6 files changed

+58
-44
lines changed

6 files changed

+58
-44
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scala
2+
package reflect
3+
package api
4+
5+
// TODO: needs a Scaladoc
6+
trait Liftables { self: Universe =>
7+
8+
// TODO: needs a Scaladoc
9+
trait Liftable[T] {
10+
def apply(value: T): Tree
11+
}
12+
13+
// TODO: needs a Scaladoc
14+
object Liftable extends StandardLiftableInstances {
15+
def apply[T](f: T => Tree): Liftable[T] =
16+
new Liftable[T] { def apply(value: T): Tree = f(value) }
17+
}
18+
19+
// TODO: needs a Scaladoc
20+
trait Unliftable[T] {
21+
def unapply(tree: Tree): Option[T]
22+
}
23+
24+
// TODO: needs a Scaladoc
25+
object Unliftable extends StandardUnliftableInstances {
26+
def apply[T](pf: PartialFunction[Tree, T]): Unliftable[T] = new Unliftable[T] {
27+
def unapply(value: Tree): Option[T] = pf.lift(value)
28+
}
29+
}
30+
}

src/reflect/scala/reflect/api/StandardLiftables.scala

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@ package api
44
trait StandardLiftables { self: Universe =>
55
import build.{SyntacticTuple, ScalaDot}
66

7-
trait Liftable[T] {
8-
def apply(value: T): Tree
9-
}
10-
11-
object Liftable {
12-
def apply[T](f: T => Tree): Liftable[T] =
13-
new Liftable[T] { def apply(value: T): Tree = f(value) }
14-
7+
trait StandardLiftableInstances {
158
private def lift[T: Liftable](value: T): Tree = implicitly[Liftable[T]].apply(value)
169
private def selectScala(names: Name*) = names.tail.foldLeft(ScalaDot(names.head)) { Select(_, _) }
1710
private def callScala(names: Name*)(args: List[Tree]) = Apply(selectScala(names: _*), args)
@@ -122,15 +115,7 @@ trait StandardLiftables { self: Universe =>
122115
}
123116
}
124117

125-
trait Unliftable[T] {
126-
def unapply(tree: Tree): Option[T]
127-
}
128-
129-
object Unliftable {
130-
def apply[T](pf: PartialFunction[Tree, T]): Unliftable[T] = new Unliftable[T] {
131-
def unapply(value: Tree): Option[T] = pf.lift(value)
132-
}
133-
118+
trait StandardUnliftableInstances {
134119
private def unliftPrimitive[Unboxed: ClassTag, Boxed: ClassTag] = Unliftable[Unboxed] {
135120
case Literal(Constant(value))
136121
if value.getClass == implicitly[ClassTag[Boxed]].runtimeClass
@@ -149,7 +134,7 @@ trait StandardLiftables { self: Universe =>
149134
implicit def unliftString: Unliftable[String] = Unliftable { case Literal(Constant(s: String)) => s }
150135

151136
implicit def unliftScalaSymbol: Unliftable[scala.Symbol] = Unliftable {
152-
case Apply(ScalaDot(nme.Symbol), List(Literal(Constant(name: String)))) => scala.Symbol(name)
137+
case Apply(ScalaDot(symbol), List(Literal(Constant(name: String)))) if symbol == nme.Symbol => scala.Symbol(name)
153138
}
154139

155140
implicit def unliftName[T <: Name : ClassTag]: Unliftable[T] = Unliftable[T] { case Ident(name: T) => name; case Bind(name: T, Ident(nme.WILDCARD)) => name}
@@ -223,4 +208,23 @@ trait StandardLiftables { self: Universe =>
223208
case SyntacticTuple(UnliftT1(v1) :: UnliftT2(v2) :: UnliftT3(v3) :: UnliftT4(v4) :: UnliftT5(v5) :: UnliftT6(v6) :: UnliftT7(v7) :: UnliftT8(v8) :: UnliftT9(v9) :: UnliftT10(v10) :: UnliftT11(v11) :: UnliftT12(v12) :: UnliftT13(v13) :: UnliftT14(v14) :: UnliftT15(v15) :: UnliftT16(v16) :: UnliftT17(v17) :: UnliftT18(v18) :: UnliftT19(v19) :: UnliftT20(v20) :: UnliftT21(v21) :: UnliftT22(v22) :: Nil) => Tuple22(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)
224209
}
225210
}
211+
212+
// names used internally by implementations of standard liftables and unliftables
213+
import scala.language.implicitConversions
214+
private implicit def cachedNames(nme: self.nme.type): CachedNames.type = CachedNames
215+
private object CachedNames {
216+
val Array = TermName("Array")
217+
val collection = TermName("collection")
218+
val immutable = TermName("immutable")
219+
val Left = TermName("Left")
220+
val List = TermName("List")
221+
val Map = TermName("Map")
222+
val None = TermName("None")
223+
val Right = TermName("Right")
224+
val Set = TermName("Set")
225+
val Some = TermName("Some")
226+
val Symbol = TermName("Symbol")
227+
val Vector = TermName("Vector")
228+
val util = TermName("util")
229+
}
226230
}

src/reflect/scala/reflect/api/StandardNames.scala

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,6 @@ trait StandardNames {
9696
* of non-private vals and vars are renamed using `LOCAL_SUFFIX_STRING`.
9797
*/
9898
val LOCAL_SUFFIX_STRING: String
99-
100-
protected[reflect] val Array: NameType
101-
protected[reflect] val collection: NameType
102-
protected[reflect] val immutable: NameType
103-
protected[reflect] val Left: NameType
104-
protected[reflect] val List: NameType
105-
protected[reflect] val Map: NameType
106-
protected[reflect] val None: NameType
107-
protected[reflect] val Right: NameType
108-
protected[reflect] val Set: NameType
109-
protected[reflect] val Some: NameType
110-
protected[reflect] val Symbol: NameType
111-
protected[reflect] val util: NameType
112-
protected[reflect] val Vector: NameType
11399
}
114100

115101
/** Defines standard type names that can be accessed via the [[tpnme]] member.

src/reflect/scala/reflect/api/Universe.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ abstract class Universe extends Symbols
7878
with Mirrors
7979
with Printers
8080
with Importers
81+
with Liftables
8182
with Quasiquotes
8283
{
8384
/** Use `refiy` to produce the abstract syntax tree representing a given Scala expression.

src/reflect/scala/reflect/internal/StdNames.scala

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,8 @@ trait StdNames {
124124
final val AnyRef: NameType = "AnyRef"
125125
final val Array: NameType = "Array"
126126
final val List: NameType = "List"
127-
final val Left: NameType = "Left"
128-
final val Right: NameType = "Right"
129-
final val Vector: NameType = "Vector"
130127
final val Seq: NameType = "Seq"
131-
final val Set: NameType = "Set"
132-
final val Some: NameType = "Some"
133128
final val Symbol: NameType = "Symbol"
134-
final val Map: NameType = "Map"
135-
final val None: NameType = "None"
136129
final val WeakTypeTag: NameType = "WeakTypeTag"
137130
final val TypeTag : NameType = "TypeTag"
138131
final val Expr: NameType = "Expr"
@@ -763,7 +756,6 @@ trait StdNames {
763756
val typedProductIterator: NameType = "typedProductIterator"
764757
val TypeName: NameType = "TypeName"
765758
val typeTagToManifest: NameType = "typeTagToManifest"
766-
val util: NameType = "util"
767759
val unapply: NameType = "unapply"
768760
val unapplySeq: NameType = "unapplySeq"
769761
val unbox: NameType = "unbox"
@@ -799,7 +791,7 @@ trait StdNames {
799791
final val STAR : NameType = "*"
800792
final val TILDE: NameType = "~"
801793

802-
final val isUnary: Set[Name] = scala.collection.immutable.Set(MINUS, PLUS, TILDE, BANG)
794+
final val isUnary: Set[Name] = Set(MINUS, PLUS, TILDE, BANG)
803795
}
804796

805797
// value-conversion methods
@@ -852,8 +844,8 @@ trait StdNames {
852844
val UNARY_! = encode("unary_!")
853845

854846
// Grouped here so Cleanup knows what tests to perform.
855-
val CommonOpNames = scala.collection.immutable.Set[Name](OR, XOR, AND, EQ, NE)
856-
val BooleanOpNames = scala.collection.immutable.Set[Name](ZOR, ZAND, UNARY_!) ++ CommonOpNames
847+
val CommonOpNames = Set[Name](OR, XOR, AND, EQ, NE)
848+
val BooleanOpNames = Set[Name](ZOR, ZAND, UNARY_!) ++ CommonOpNames
857849

858850
val add: NameType = "add"
859851
val complement: NameType = "complement"

src/reflect/scala/reflect/runtime/JavaUniverseForce.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ trait JavaUniverseForce { self: runtime.JavaUniverse =>
204204
// inaccessible: this.TypeHistory
205205
this.TermName
206206
this.TypeName
207-
this.BooleanFlag
208207
this.Liftable
209208
this.Unliftable
209+
this.BooleanFlag
210+
// inaccessible: this.CachedNames
210211
this.WeakTypeTag
211212
this.TypeTag
212213
this.Expr

0 commit comments

Comments
 (0)