@@ -517,18 +517,96 @@ class Namer { typer: Typer =>
517
517
}
518
518
}
519
519
520
- stats foreach expand
520
+ stats. foreach( expand)
521
521
mergeCompanionDefs()
522
522
val ctxWithStats = (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx))
523
523
createCompanionLinks(ctxWithStats)
524
524
ctxWithStats
525
525
}
526
526
527
+ /** Add all annotations of definitions in `stats` to the defined symbols */
528
+ def annotate (stats : List [Tree ])(implicit ctx : Context ): Unit = {
529
+ def recur (stat : Tree ): Unit = stat match {
530
+ case pcl : PackageDef =>
531
+ annotate(pcl.stats)
532
+ case stat : untpd.MemberDef =>
533
+ stat.getAttachment(SymOfTree ) match {
534
+ case Some (sym) =>
535
+ sym.infoOrCompleter match {
536
+ case info : Completer if ! defn.isPredefClass(sym.owner) =>
537
+ // Annotate Predef methods only when they are completed;
538
+ // This is necessary to break a cyclic dependence between `Predef`
539
+ // and `deprecated` in test `compileStdLib`.
540
+ addAnnotations(sym, stat)(info.creationContext)
541
+ case _ =>
542
+ // Annotations were already added as part of the symbol's completion
543
+ }
544
+ case none =>
545
+ assert(stat.typeOpt.exists, i " no symbol for $stat" )
546
+ }
547
+ case stat : untpd.Thicket =>
548
+ stat.trees.foreach(recur)
549
+ case _ =>
550
+ }
551
+
552
+ for (stat <- stats) recur(expanded(stat))
553
+ }
554
+
555
+ /** Add annotations of `stat` to `sym`.
556
+ * This method can be called twice on a symbol (e.g. once
557
+ * during the `annotate` phase and then again during completion).
558
+ * Therefore, care needs to be taken not to add annotations again
559
+ * that are already added to the symbol.
560
+ */
561
+ def addAnnotations (sym : Symbol , stat : MemberDef )(implicit ctx : Context ) = {
562
+ // (1) The context in which an annotation of a top-evel class or module is evaluated
563
+ // is the closest enclosing context which has the enclosing package as owner.
564
+ // (2) The context in which an annotation for any other symbol is evaluated is the
565
+ // closest enclosing context which has the owner of the class enclpsing the symbol as owner.
566
+ // E.g in
567
+ //
568
+ // package p
569
+ // import a.b
570
+ // class C {
571
+ // import d.e
572
+ // @ann m() ...
573
+ // }
574
+ //
575
+ // `@ann` is evaluated in the context just outside `C`, where the `a.b`
576
+ // import is visible but the `d.e` import is forgotten. This measure is necessary
577
+ // in order to avoid cycles.
578
+ lazy val annotCtx = {
579
+ var target = sym.owner.lexicallyEnclosingClass
580
+ if (! target.is(PackageClass )) target = target.owner
581
+ var c = ctx
582
+ while (c.owner != target) c = c.outer
583
+ c
584
+ }
585
+ for (annotTree <- untpd.modsDeco(stat).mods.annotations) {
586
+ val cls = typedAheadAnnotation(annotTree)(annotCtx)
587
+ if (sym.unforcedAnnotation(cls).isEmpty) {
588
+ val ann = Annotation .deferred(cls, implicit ctx => typedAnnotation(annotTree))
589
+ sym.addAnnotation(ann)
590
+ if (cls == defn.InlineAnnot && sym.is(Method , butNot = Accessor ))
591
+ sym.setFlag(Inline )
592
+ }
593
+ }
594
+ }
595
+
596
+ def indexAndAnnotate (stats : List [Tree ])(implicit ctx : Context ): Context = {
597
+ val localCtx = index(stats)
598
+ annotate(stats)
599
+ localCtx
600
+ }
601
+
527
602
/** The completer of a symbol defined by a member def or import (except ClassSymbols) */
528
603
class Completer (val original : Tree )(implicit ctx : Context ) extends LazyType {
529
604
530
605
protected def localContext (owner : Symbol ) = ctx.fresh.setOwner(owner).setTree(original)
531
606
607
+ /** The context with which this completer was created */
608
+ def creationContext = ctx
609
+
532
610
protected def typeSig (sym : Symbol ): Type = original match {
533
611
case original : ValDef =>
534
612
if (sym is Module ) moduleValSig(sym)
@@ -561,19 +639,6 @@ class Namer { typer: Typer =>
561
639
completeInCreationContext(denot)
562
640
}
563
641
564
- protected def addAnnotations (denot : SymDenotation ): Unit = original match {
565
- case original : untpd.MemberDef =>
566
- var hasInlineAnnot = false
567
- for (annotTree <- untpd.modsDeco(original).mods.annotations) {
568
- val cls = typedAheadAnnotation(annotTree)
569
- val ann = Annotation .deferred(cls, implicit ctx => typedAnnotation(annotTree))
570
- denot.addAnnotation(ann)
571
- if (cls == defn.InlineAnnot && denot.is(Method , butNot = Accessor ))
572
- denot.setFlag(Inline )
573
- }
574
- case _ =>
575
- }
576
-
577
642
private def addInlineInfo (denot : SymDenotation ) = original match {
578
643
case original : untpd.DefDef if denot.isInlineMethod =>
579
644
Inliner .registerInlineInfo(
@@ -587,7 +652,10 @@ class Namer { typer: Typer =>
587
652
* to pick up the context at the point where the completer was created.
588
653
*/
589
654
def completeInCreationContext (denot : SymDenotation ): Unit = {
590
- addAnnotations(denot)
655
+ original match {
656
+ case original : MemberDef => addAnnotations(denot.symbol, original)
657
+ case _ =>
658
+ }
591
659
addInlineInfo(denot)
592
660
denot.info = typeSig(denot.symbol)
593
661
Checking .checkWellFormed(denot.symbol)
@@ -718,7 +786,7 @@ class Namer { typer: Typer =>
718
786
ok
719
787
}
720
788
721
- addAnnotations(denot)
789
+ addAnnotations(denot.symbol, original )
722
790
723
791
val selfInfo =
724
792
if (self.isEmpty) NoType
@@ -741,9 +809,10 @@ class Namer { typer: Typer =>
741
809
// accessors, that's why the constructor needs to be completed before
742
810
// the parent types are elaborated.
743
811
index(constr)
812
+ annotate(constr :: params)
744
813
symbolOfTree(constr).ensureCompleted()
745
814
746
- index (rest)(inClassContext(selfInfo))
815
+ indexAndAnnotate (rest)(inClassContext(selfInfo))
747
816
748
817
val tparamAccessors = decls.filter(_ is TypeParamAccessor ).toList
749
818
val parentTypes = ensureFirstIsClass(parents.map(checkedParentType(_, tparamAccessors)))
@@ -766,7 +835,7 @@ class Namer { typer: Typer =>
766
835
case Some (ttree) => ttree
767
836
case none =>
768
837
val ttree = typer.typed(tree, pt)
769
- xtree.pushAttachment (TypedAhead , ttree)
838
+ xtree.putAttachment (TypedAhead , ttree)
770
839
ttree
771
840
}
772
841
}
@@ -786,7 +855,7 @@ class Namer { typer: Typer =>
786
855
787
856
/** Enter and typecheck parameter list */
788
857
def completeParams (params : List [MemberDef ])(implicit ctx : Context ) = {
789
- index (params)
858
+ indexAndAnnotate (params)
790
859
for (param <- params) typedAheadExpr(param)
791
860
}
792
861
@@ -966,7 +1035,7 @@ class Namer { typer: Typer =>
966
1035
// 3. Info of CP is computed (to be copied to DP).
967
1036
// 4. CP is completed.
968
1037
// 5. Info of CP is copied to DP and DP is completed.
969
- index (tparams)
1038
+ indexAndAnnotate (tparams)
970
1039
if (isConstructor) sym.owner.typeParams.foreach(_.ensureCompleted())
971
1040
for (tparam <- tparams) typedAheadExpr(tparam)
972
1041
0 commit comments