@@ -528,18 +528,96 @@ class Namer { typer: Typer =>
528
528
}
529
529
}
530
530
531
- stats foreach expand
531
+ stats. foreach( expand)
532
532
mergeCompanionDefs()
533
533
val ctxWithStats = (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx))
534
534
createCompanionLinks(ctxWithStats)
535
535
ctxWithStats
536
536
}
537
537
538
+ /** Add all annotations of definitions in `stats` to the defined symbols */
539
+ def annotate (stats : List [Tree ])(implicit ctx : Context ): Unit = {
540
+ def recur (stat : Tree ): Unit = stat match {
541
+ case pcl : PackageDef =>
542
+ annotate(pcl.stats)
543
+ case stat : untpd.MemberDef =>
544
+ stat.getAttachment(SymOfTree ) match {
545
+ case Some (sym) =>
546
+ sym.infoOrCompleter match {
547
+ case info : Completer if ! defn.isPredefClass(sym.owner) =>
548
+ // Annotate Predef methods only when they are completed;
549
+ // This is necessary to break a cyclic dependence between `Predef`
550
+ // and `deprecated` in test `compileStdLib`.
551
+ addAnnotations(sym, stat)(info.creationContext)
552
+ case _ =>
553
+ // Annotations were already added as part of the symbol's completion
554
+ }
555
+ case none =>
556
+ assert(stat.typeOpt.exists, i " no symbol for $stat" )
557
+ }
558
+ case stat : untpd.Thicket =>
559
+ stat.trees.foreach(recur)
560
+ case _ =>
561
+ }
562
+
563
+ for (stat <- stats) recur(expanded(stat))
564
+ }
565
+
566
+ /** Add annotations of `stat` to `sym`.
567
+ * This method can be called twice on a symbol (e.g. once
568
+ * during the `annotate` phase and then again during completion).
569
+ * Therefore, care needs to be taken not to add annotations again
570
+ * that are already added to the symbol.
571
+ */
572
+ def addAnnotations (sym : Symbol , stat : MemberDef )(implicit ctx : Context ) = {
573
+ // (1) The context in which an annotation of a top-evel class or module is evaluated
574
+ // is the closest enclosing context which has the enclosing package as owner.
575
+ // (2) The context in which an annotation for any other symbol is evaluated is the
576
+ // closest enclosing context which has the owner of the class enclpsing the symbol as owner.
577
+ // E.g in
578
+ //
579
+ // package p
580
+ // import a.b
581
+ // class C {
582
+ // import d.e
583
+ // @ann m() ...
584
+ // }
585
+ //
586
+ // `@ann` is evaluated in the context just outside `C`, where the `a.b`
587
+ // import is visible but the `d.e` import is forgotten. This measure is necessary
588
+ // in order to avoid cycles.
589
+ lazy val annotCtx = {
590
+ var target = sym.owner.lexicallyEnclosingClass
591
+ if (! target.is(PackageClass )) target = target.owner
592
+ var c = ctx
593
+ while (c.owner != target) c = c.outer
594
+ c
595
+ }
596
+ for (annotTree <- untpd.modsDeco(stat).mods.annotations) {
597
+ val cls = typedAheadAnnotation(annotTree)(annotCtx)
598
+ if (sym.unforcedAnnotation(cls).isEmpty) {
599
+ val ann = Annotation .deferred(cls, implicit ctx => typedAnnotation(annotTree))
600
+ sym.addAnnotation(ann)
601
+ if (cls == defn.InlineAnnot && sym.is(Method , butNot = Accessor ))
602
+ sym.setFlag(Inline )
603
+ }
604
+ }
605
+ }
606
+
607
+ def indexAndAnnotate (stats : List [Tree ])(implicit ctx : Context ): Context = {
608
+ val localCtx = index(stats)
609
+ annotate(stats)
610
+ localCtx
611
+ }
612
+
538
613
/** The completer of a symbol defined by a member def or import (except ClassSymbols) */
539
614
class Completer (val original : Tree )(implicit ctx : Context ) extends LazyType {
540
615
541
616
protected def localContext (owner : Symbol ) = ctx.fresh.setOwner(owner).setTree(original)
542
617
618
+ /** The context with which this completer was created */
619
+ def creationContext = ctx
620
+
543
621
protected def typeSig (sym : Symbol ): Type = original match {
544
622
case original : ValDef =>
545
623
if (sym is Module ) moduleValSig(sym)
@@ -572,19 +650,6 @@ class Namer { typer: Typer =>
572
650
completeInCreationContext(denot)
573
651
}
574
652
575
- protected def addAnnotations (denot : SymDenotation ): Unit = original match {
576
- case original : untpd.MemberDef =>
577
- var hasInlineAnnot = false
578
- for (annotTree <- untpd.modsDeco(original).mods.annotations) {
579
- val cls = typedAheadAnnotation(annotTree)
580
- val ann = Annotation .deferred(cls, implicit ctx => typedAnnotation(annotTree))
581
- denot.addAnnotation(ann)
582
- if (cls == defn.InlineAnnot && denot.is(Method , butNot = Accessor ))
583
- denot.setFlag(Inline )
584
- }
585
- case _ =>
586
- }
587
-
588
653
private def addInlineInfo (denot : SymDenotation ) = original match {
589
654
case original : untpd.DefDef if denot.isInlineMethod =>
590
655
Inliner .registerInlineInfo(
@@ -598,7 +663,10 @@ class Namer { typer: Typer =>
598
663
* to pick up the context at the point where the completer was created.
599
664
*/
600
665
def completeInCreationContext (denot : SymDenotation ): Unit = {
601
- addAnnotations(denot)
666
+ original match {
667
+ case original : MemberDef => addAnnotations(denot.symbol, original)
668
+ case _ =>
669
+ }
602
670
addInlineInfo(denot)
603
671
denot.info = typeSig(denot.symbol)
604
672
Checking .checkWellFormed(denot.symbol)
@@ -742,7 +810,7 @@ class Namer { typer: Typer =>
742
810
ok
743
811
}
744
812
745
- addAnnotations(denot)
813
+ addAnnotations(denot.symbol, original )
746
814
747
815
val selfInfo =
748
816
if (self.isEmpty) NoType
@@ -765,9 +833,10 @@ class Namer { typer: Typer =>
765
833
// accessors, that's why the constructor needs to be completed before
766
834
// the parent types are elaborated.
767
835
index(constr)
836
+ annotate(constr :: params)
768
837
symbolOfTree(constr).ensureCompleted()
769
838
770
- index (rest)(inClassContext(selfInfo))
839
+ indexAndAnnotate (rest)(inClassContext(selfInfo))
771
840
772
841
val tparamAccessors = decls.filter(_ is TypeParamAccessor ).toList
773
842
val parentTypes = ensureFirstIsClass(parents.map(checkedParentType(_, tparamAccessors)))
@@ -790,7 +859,7 @@ class Namer { typer: Typer =>
790
859
case Some (ttree) => ttree
791
860
case none =>
792
861
val ttree = typer.typed(tree, pt)
793
- xtree.pushAttachment (TypedAhead , ttree)
862
+ xtree.putAttachment (TypedAhead , ttree)
794
863
ttree
795
864
}
796
865
}
@@ -810,7 +879,7 @@ class Namer { typer: Typer =>
810
879
811
880
/** Enter and typecheck parameter list */
812
881
def completeParams (params : List [MemberDef ])(implicit ctx : Context ) = {
813
- index (params)
882
+ indexAndAnnotate (params)
814
883
for (param <- params) typedAheadExpr(param)
815
884
}
816
885
@@ -990,7 +1059,7 @@ class Namer { typer: Typer =>
990
1059
// 3. Info of CP is computed (to be copied to DP).
991
1060
// 4. CP is completed.
992
1061
// 5. Info of CP is copied to DP and DP is completed.
993
- index (tparams)
1062
+ indexAndAnnotate (tparams)
994
1063
if (isConstructor) sym.owner.typeParams.foreach(_.ensureCompleted())
995
1064
for (tparam <- tparams) typedAheadExpr(tparam)
996
1065
0 commit comments