@@ -499,12 +499,31 @@ class Namer { typer: Typer =>
499
499
vd.mods.is(JavaEnumValue ) // && ownerHasEnumFlag
500
500
}
501
501
502
+ /** Add child annotation for `child` to annotations of `cls`. The annotation
503
+ * is added at the correct insertion point, so that Child annotations appear
504
+ * in reverse order of their start positions.
505
+ * @pre `child` must have a position.
506
+ */
507
+ final def addChild (cls : Symbol , child : Symbol )(implicit ctx : Context ): Unit = {
508
+ val childStart = child.pos.start
509
+ def insertInto (annots : List [Annotation ]): List [Annotation ] =
510
+ annots.find(_.symbol == defn.ChildAnnot ) match {
511
+ case Some (Annotation .Child (other)) if childStart <= other.pos.start =>
512
+ assert(childStart != other.pos.start, " duplicate child annotation $child / $other" )
513
+ val (prefix, otherAnnot :: rest) = annots.span(_.symbol != defn.ChildAnnot )
514
+ prefix ::: otherAnnot :: insertInto(rest)
515
+ case _ =>
516
+ Annotation .Child (child) :: annots
517
+ }
518
+ cls.annotations = insertInto(cls.annotations)
519
+ }
520
+
502
521
/** Add java enum constants */
503
522
def addEnumConstants (mdef : DefTree , sym : Symbol )(implicit ctx : Context ): Unit = mdef match {
504
523
case vdef : ValDef if (isEnumConstant(vdef)) =>
505
524
val enumClass = sym.owner.linkedClass
506
525
if (! (enumClass is Flags .Sealed )) enumClass.setFlag(Flags .AbstractSealed )
507
- enumClass.addAnnotation( Annotation . Child ( sym) )
526
+ addChild(enumClass, sym)
508
527
case _ =>
509
528
}
510
529
@@ -814,9 +833,9 @@ class Namer { typer: Typer =>
814
833
val cls = parent.classSymbol
815
834
if (cls.is(Sealed )) {
816
835
if ((child.isInaccessibleChildOf(cls) || child.isAnonymousClass) && ! sym.hasAnonymousChild)
817
- cls.addAnnotation( Annotation . Child ( cls) )
836
+ addChild( cls, cls )
818
837
else if (! cls.is(ChildrenQueried ))
819
- cls.addAnnotation( Annotation . Child ( child) )
838
+ addChild(cls, child)
820
839
else
821
840
ctx.error(em """ children of ${cls} were already queried before $sym was discovered.
822
841
|As a remedy, you could move $sym on the same nesting level as $cls. """ ,
0 commit comments