@@ -107,6 +107,24 @@ trait MacroCommons { bundle =>
107107 def indent (str : String , indent : String ): String =
108108 str.replaceAllLiterally(" \n " , s " \n $indent" )
109109
110+ private def annotations (s : Symbol ): List [Annotation ] = {
111+ s.info // srsly scalac, load these goddamned annotations
112+ s.annotations
113+ }
114+
115+ def treeAsSeenFrom (tree : Tree , seenFrom : Type ): Tree = seenFrom match {
116+ case NoType => tree
117+ case TypeRef (_, sym, Nil ) if sym.isStatic => tree
118+ case _ =>
119+ val res = tree.duplicate
120+ res.foreach { t =>
121+ if (t.tpe != null ) {
122+ internal.setType(t, t.tpe.asSeenFrom(seenFrom, seenFrom.typeSymbol))
123+ }
124+ }
125+ res
126+ }
127+
110128 class Annot (annotTree : Tree , val subject : Symbol , val directSource : Symbol , val aggregate : Option [Annot ]) {
111129 def aggregationChain : List [Annot ] =
112130 aggregate.fold(List .empty[Annot ])(a => a :: a.aggregationChain)
@@ -137,7 +155,17 @@ trait MacroCommons { bundle =>
137155 case _ => annotTree
138156 }
139157
140- def tpe : Type = annotTree.tpe
158+ def tpe : Type =
159+ annotTree.tpe
160+
161+ def symbol : ClassSymbol =
162+ tpe.typeSymbol.asClass
163+
164+ lazy val argsByName : Map [Name , Tree ] = {
165+ val Apply (_, args) = tree
166+ val paramNames = primaryConstructorOf(tpe).typeSignature.paramLists.head.map(_.name)
167+ (paramNames zip args).toMap
168+ }
141169
142170 def findArg [T : ClassTag ](valSym : Symbol ): T =
143171 findArg[T ](valSym, abort(s " (bug) no default value for ${tree.tpe} parameter ${valSym.name} provided by macro " ))
@@ -157,20 +185,28 @@ trait MacroCommons { bundle =>
157185 case t if param.asTerm.isParamWithDefault && t.symbol.isSynthetic &&
158186 t.symbol.name.decodedName.toString.contains(" $default$" ) => whenDefault
159187 case t if classTag[T ] == classTag[Tree ] => t.asInstanceOf [T ]
160- case _ =>
161- abort( s " Expected literal ${classTag[ T ].runtimeClass} as ${valSym.name} parameter of $clsTpe annotation " )
188+ case _ => abort( s " Expected literal ${classTag[ T ].runtimeClass.getSimpleName} " +
189+ s " as ${valSym.name} parameter of $clsTpe annotation " )
162190 }
163191 }
164192 .getOrElse(abort(s " Could not find argument corresponding to constructor parameter ${subSym.name}" ))
165193 case _ => abort(s " Not a primary constructor call tree: $tree" )
166194 }
167195
196+ private object argsInliner extends Transformer {
197+ override def transform (tree : Tree ): Tree = tree match {
198+ case Select (th@ This (_), name) if th.symbol == symbol && tree.symbol.asTerm.isParamAccessor =>
199+ argsByName.get(name).map(_.duplicate).getOrElse(tree)
200+ case _ => super .transform(tree)
201+ }
202+ }
203+
168204 lazy val aggregated : List [Annot ] = {
169205 if (tpe <:< AnnotationAggregateType ) {
170- val argsInliner = new AnnotationArgInliner (tree)
171206 val impliedMember = tpe.member(TypeName (" Implied" ))
172- impliedMember.annotations.map(a =>
173- new Annot (argsInliner.transform(a.tree), subject, impliedMember, Some (this )))
207+ annotations(impliedMember).map { a =>
208+ new Annot (argsInliner.transform(treeAsSeenFrom(a.tree, tpe)), subject, impliedMember, Some (this ))
209+ }
174210 } else Nil
175211 }
176212
@@ -184,21 +220,6 @@ trait MacroCommons { bundle =>
184220 }
185221 }
186222
187- class AnnotationArgInliner (baseAnnot : Tree ) extends Transformer {
188- private val argsByName : Map [Name , Tree ] = {
189- val Apply (_, args) = baseAnnot
190- val paramNames = primaryConstructorOf(baseAnnot.tpe).typeSignature.paramLists.head.map(_.name)
191- (paramNames zip args).toMap
192- }
193-
194- override def transform (tree : Tree ): Tree = tree match {
195- case Select (th@ This (_), name) if th.symbol == baseAnnot.tpe.typeSymbol
196- && tree.symbol.asTerm.isParamAccessor =>
197- argsByName.get(name).map(_.duplicate).getOrElse(tree)
198- case _ => super .transform(tree)
199- }
200- }
201-
202223 private def orConstructorParam (applyParam : Symbol ): Symbol = {
203224 val owner = applyParam.owner
204225 if (owner.name == TermName (" apply" ) && owner.isSynthetic)
@@ -210,19 +231,24 @@ trait MacroCommons { bundle =>
210231 private def maybeWithSuperSymbols (s : Symbol , withSupers : Boolean ): Iterator [Symbol ] =
211232 if (withSupers) withSuperSymbols(s) else Iterator (s)
212233
213- def allAnnotations (s : Symbol , tpeFilter : Type , withInherited : Boolean = true , fallback : List [Tree ] = Nil ): List [Annot ] = {
234+ def allAnnotations (s : Symbol , tpeFilter : Type ,
235+ seenFrom : Type = NoType , withInherited : Boolean = true , fallback : List [Tree ] = Nil ): List [Annot ] = {
236+
214237 val initSym = orConstructorParam(s)
215238 def inherited (annot : Annotation , superSym : Symbol ): Boolean =
216239 ! (superSym != initSym && isSealedHierarchyRoot(superSym) && annot.tree.tpe <:< NotInheritedFromSealedTypes )
217240
218241 val nonFallback = maybeWithSuperSymbols(initSym, withInherited)
219- .flatMap(ss => ss.annotations.filter(inherited(_, ss)).map(a => new Annot (a.tree, s, ss, None )))
242+ .flatMap(ss => annotations(ss).filter(inherited(_, ss))
243+ .map(a => new Annot (treeAsSeenFrom(a.tree, seenFrom), s, ss, None )))
220244
221245 (nonFallback ++ fallback.iterator.map(t => new Annot (t, s, s, None )))
222246 .flatMap(_.withAllAggregated).filter(_.tpe <:< tpeFilter).toList
223247 }
224248
225- def findAnnotation (s : Symbol , tpe : Type , withInherited : Boolean = true , fallback : List [Tree ] = Nil ): Option [Annot ] = {
249+ def findAnnotation (s : Symbol , tpe : Type ,
250+ seenFrom : Type = NoType , withInherited : Boolean = true , fallback : List [Tree ] = Nil ): Option [Annot ] = {
251+
226252 val initSym = orConstructorParam(s)
227253 def find (annots : List [Annot ]): Option [Annot ] = annots match {
228254 case head :: tail =>
@@ -245,7 +271,8 @@ trait MacroCommons { bundle =>
245271 ! (superSym != initSym && isSealedHierarchyRoot(superSym) && annot.tree.tpe <:< NotInheritedFromSealedTypes )
246272
247273 maybeWithSuperSymbols(initSym, withInherited)
248- .map(ss => find(ss.annotations.filter(inherited(_, ss)).map(a => new Annot (a.tree, s, ss, None ))))
274+ .map(ss => find(annotations(ss).filter(inherited(_, ss))
275+ .map(a => new Annot (treeAsSeenFrom(a.tree, seenFrom), s, ss, None ))))
249276 .collectFirst { case Some (annot) => annot }
250277 .orElse(find(fallback.map(t => new Annot (t, s, s, None ))))
251278 }
@@ -393,7 +420,7 @@ trait MacroCommons { bundle =>
393420 symbolImplicitNotFoundMsg(tpe, tpe.typeSymbol, tpe.typeSymbol.typeSignature.typeParams, tpe.typeArgs)
394421
395422 private def symbolImplicitNotFoundMsg (tpe : Type , sym : Symbol , tparams : List [Symbol ], typeArgs : List [Type ]): String =
396- sym. annotations.find(_.tree.tpe <:< ImplicitNotFoundAT )
423+ annotations(sym) .find(_.tree.tpe <:< ImplicitNotFoundAT )
397424 .map(_.tree.children.tail.head).collect { case StringLiteral (error) => error }
398425 .map { error =>
399426 val tpNames = tparams.map(_.name.decodedName.toString)
@@ -759,19 +786,19 @@ trait MacroCommons { bundle =>
759786 def paramSymbolToValDef (sym : Symbol ): ValDef = {
760787 val ts = sym.asTerm
761788 val implicitFlag = if (sym.isImplicit) Flag .IMPLICIT else NoFlags
762- val mods = Modifiers (Flag .PARAM | implicitFlag, typeNames.EMPTY , ts. annotations.map(_.tree))
789+ val mods = Modifiers (Flag .PARAM | implicitFlag, typeNames.EMPTY , annotations(ts) .map(_.tree))
763790 ValDef (mods, ts.name, treeForType(sym.typeSignature), EmptyTree )
764791 }
765792
766793 def getterSymbolToValDef (sym : Symbol ): ValDef = {
767794 val ms = sym.asMethod
768795 val mutableFlag = if (ms.isVar) Flag .MUTABLE else NoFlags
769- val mods = Modifiers (Flag .DEFERRED | mutableFlag, typeNames.EMPTY , ms. annotations.map(_.tree))
796+ val mods = Modifiers (Flag .DEFERRED | mutableFlag, typeNames.EMPTY , annotations(ms) .map(_.tree))
770797 ValDef (mods, ms.name, treeForType(sym.typeSignature), EmptyTree )
771798 }
772799
773800 def existentialSingletonToValDef (sym : Symbol , name : TermName , tpe : Type ): ValDef = {
774- val mods = Modifiers (Flag .DEFERRED , typeNames.EMPTY , sym. annotations.map(_.tree))
801+ val mods = Modifiers (Flag .DEFERRED , typeNames.EMPTY , annotations(sym) .map(_.tree))
775802 ValDef (mods, name, treeForType(tpe), EmptyTree )
776803 }
777804
@@ -790,7 +817,7 @@ trait MacroCommons { bundle =>
790817 else NoFlags
791818
792819 val flags = paramOrDeferredFlag | syntheticFlag | varianceFlag
793- val mods = Modifiers (flags, typeNames.EMPTY , ts. annotations.map(_.tree))
820+ val mods = Modifiers (flags, typeNames.EMPTY , annotations(ts) .map(_.tree))
794821 val (typeParams, signature) = sym.typeSignature match {
795822 case PolyType (polyParams, resultType) => (polyParams, resultType)
796823 case sig => (ts.typeParams, sig)
@@ -1024,7 +1051,7 @@ trait MacroCommons { bundle =>
10241051 def positionPoint (sym : Symbol ): Int =
10251052 if (c.enclosingPosition.source == sym.pos.source) sym.pos.point
10261053 else positionCache.getOrElseUpdate(sym,
1027- sym. annotations.find(_.tree.tpe <:< PositionedAT ).map(_.tree).map {
1054+ annotations(sym) .find(_.tree.tpe <:< PositionedAT ).map(_.tree).map {
10281055 case Apply (_, List (MaybeTyped (Lit (point : Int ), _))) => point
10291056 case t => abort(s " expected literal int as argument of @positioned annotation on $sym, got $t" )
10301057 } getOrElse {
0 commit comments