@@ -6,14 +6,14 @@ import scala.language.existentials
66
77import scala .reflect .macros .Context
88import scala .reflect .api .Universe
9- import scala .reflect .runtime .universe ._
10- import scala .reflect .runtime .{universe => ru }
119
1210import scala .collection .mutable .{Map => MutableMap , ListBuffer => MutableList , WeakHashMap , Set => MutableSet }
1311import scala .collection .mutable .{Stack => MutableStack , Queue => MutableQueue }
1412
1513import java .lang .ref .WeakReference
1614
15+ import HasCompat ._
16+
1717object Tools {
1818 private val subclassCaches = new WeakHashMap [AnyRef , WeakReference [AnyRef ]]()
1919
@@ -44,6 +44,7 @@ object Tools {
4444class Tools [C <: Context ](val c : C ) {
4545 val u : c.universe.type = c.universe
4646 import u ._
47+ import compat ._
4748 import definitions ._
4849
4950 def blackList (sym : Symbol ) = sym == AnyClass || sym == AnyRefClass || sym == AnyValClass || sym == ObjectClass
@@ -238,9 +239,10 @@ abstract class ShareAnalyzer[U <: Universe](val u: U) {
238239 }
239240}
240241
241- abstract class Macro extends Reflection211Compat { self =>
242+ abstract class Macro { self =>
242243 val c : Context
243244 import c .universe ._
245+ import compat ._
244246 import definitions ._
245247 val RefTpe = weakTypeOf[refs.Ref ]
246248
@@ -258,7 +260,7 @@ abstract class Macro extends Reflection211Compat { self =>
258260 private def innerType (target : Tree , name : String ): Type = {
259261 def fail (msg : String ) = c.abort(c.enclosingPosition, s " $msg for ${target} of type ${target.tpe}" )
260262 // val carrier = c.typeCheck(tq"${target.tpe}#${TypeName(name)}", mode = c.TYPEmode, silent = true)
261- val carrier = c.typeCheck(q " { val x: ${target.tpe}# ${TypeName (name)} = ??? } " , silent = true )
263+ val carrier = c.typeCheck(q " { val x: ${target.tpe}# ${newTypeName (name)} = ??? } " , silent = true )
262264 carrier match {
263265 case EmptyTree => fail(s " Couldn't resolve $name" )
264266 case Block (ValDef (_, _, tpt, _) :: _, _) => tpt.tpe.normalize match {
@@ -316,10 +318,10 @@ abstract class Macro extends Reflection211Compat { self =>
316318 def syntheticPackageName : String = " scala.pickling.synthetic"
317319 def syntheticBaseName (tpe : Type ): TypeName = {
318320 val raw = tpe.key.split('.' ).map(_.capitalize).mkString(" " )
319- val encoded = TypeName (raw).encoded
320- TypeName (encoded)
321+ val encoded = newTypeName (raw).encoded
322+ newTypeName (encoded)
321323 }
322- def syntheticBaseQualifiedName (tpe : Type ): TypeName = TypeName (syntheticPackageName + " ." + syntheticBaseName(tpe).toString)
324+ def syntheticBaseQualifiedName (tpe : Type ): TypeName = newTypeName (syntheticPackageName + " ." + syntheticBaseName(tpe).toString)
323325
324326 def syntheticPicklerName (tpe : Type ): TypeName = syntheticBaseName(tpe) + syntheticPicklerSuffix()
325327 def syntheticPicklerQualifiedName (tpe : Type ): TypeName = syntheticBaseQualifiedName(tpe) + syntheticPicklerSuffix()
@@ -334,35 +336,44 @@ abstract class Macro extends Reflection211Compat { self =>
334336 def syntheticPicklerUnpicklerSuffix (): String = " PicklerUnpickler"
335337
336338 def preferringAlternativeImplicits (body : => Tree ): Tree = {
339+ import Compat ._
340+
341+ val candidates = c.enclosingImplicits
342+ val ourPt = candidates.head.pt
343+
337344 def debug (msg : Any ) = {
338- val padding = " " * (c.enclosingImplicits .length - 1 )
345+ val padding = " " * (candidates .length - 1 )
339346 // Console.err.println(padding + msg)
340347 }
341- debug(" can we enter " + c.enclosingImplicits.head._1 + " ?" )
342- debug(c.enclosingImplicits)
343- c.enclosingImplicits match {
344- case (ourPt, _) :: (theirPt, _) :: _ if ourPt =:= theirPt =>
345- debug(s " no, because: ourPt = $ourPt, theirPt = $theirPt" )
346- // c.diverge()
347- c.abort(c.enclosingPosition, " stepping aside: repeating itself" )
348- case _ =>
349- debug(s " not sure, need to explore alternatives " )
350- c.inferImplicitValue(c.enclosingImplicits.head._1, silent = true ) match {
351- case success if success != EmptyTree =>
352- debug(s " no, because there's $success" )
353- c.abort(c.enclosingPosition, " stepping aside: there are other candidates" )
354- // c.diverge()
355- case _ =>
356- debug(" yes, there are no obstacles. entering " + c.enclosingImplicits.head._1)
357- val result = body
358- debug(" result: " + result)
359- result
360- }
348+
349+ debug(" can we enter " + ourPt + " ?" )
350+ debug(candidates)
351+
352+ if ((candidates.size >= 2 ) && {
353+ val theirPt = candidates.tail.head.pt
354+ ourPt =:= theirPt
355+ }) {
356+ debug(s " no, because: ourPt = $ourPt, theirPt = ${candidates.tail.head.pt}" )
357+ // c.diverge()
358+ c.abort(c.enclosingPosition, " stepping aside: repeating itself" )
359+ } else {
360+ debug(s " not sure, need to explore alternatives " )
361+ c.inferImplicitValue(ourPt, silent = true ) match {
362+ case success if success != EmptyTree =>
363+ debug(s " no, because there's $success" )
364+ c.abort(c.enclosingPosition, " stepping aside: there are other candidates" )
365+ // c.diverge()
366+ case _ =>
367+ debug(" yes, there are no obstacles. entering " + ourPt)
368+ val result = body
369+ debug(" result: " + result)
370+ result
371+ }
361372 }
362373 }
363374
364375 private var reflectivePrologueEmitted = false // TODO: come up with something better
365- def reflectively (target : String , fir : FieldIR )(body : Tree => Tree ): List [Tree ] = reflectively(TermName (target), fir)(body)
376+ def reflectively (target : String , fir : FieldIR )(body : Tree => Tree ): List [Tree ] = reflectively(newTermName (target), fir)(body)
366377
367378 /**
368379 * requires: !fir.accessor.isEmpty
@@ -382,8 +393,8 @@ abstract class Macro extends Reflection211Compat { self =>
382393 }
383394 }
384395 val field = fir.field.get
385- val ownerSymbol = TermName (fir.name + " Owner" )
386- val firSymbol = TermName (fir.name + " Symbol" )
396+ val ownerSymbol = newTermName (fir.name + " Owner" )
397+ val firSymbol = newTermName (fir.name + " Symbol" )
387398 // TODO: make sure this works for:
388399 // 1) private[this] fields
389400 // 2) inherited private[this] fields
@@ -427,6 +438,8 @@ trait PickleTools {
427438}
428439
429440trait CurrentMirrorMacro extends Macro {
441+ import scala .reflect .runtime .{universe => ru }
442+
430443 def impl : c.Tree = {
431444 import c .universe ._
432445 c.inferImplicitValue(typeOf[ru.Mirror ], silent = true ) orElse {
0 commit comments