Skip to content

Commit 478be16

Browse files
committed
Merge pull request #641 from dotty-staging/change/early-annotations
Evaluate annotations in Namer
2 parents a654d20 + f121d8a commit 478be16

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import annotation.tailrec
1515
import ErrorReporting._
1616
import tpd.ListOfTreeDecorator
1717
import config.Printers._
18+
import Annotations._
1819
import language.implicitConversions
1920

2021
trait NamerContextOps { this: Context =>
@@ -495,8 +496,23 @@ class Namer { typer: Typer =>
495496
completeInCreationContext(denot)
496497
}
497498

498-
def completeInCreationContext(denot: SymDenotation): Unit =
499+
protected def addAnnotations(denot: SymDenotation): Unit = original match {
500+
case original: untpd.MemberDef =>
501+
for (annotTree <- untpd.modsDeco(original).mods.annotations) {
502+
val cls = typedAheadAnnotation(annotTree)
503+
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
504+
denot.addAnnotation(ann)
505+
}
506+
case _ =>
507+
}
508+
509+
/** Intentionally left without `implicit ctx` parameter. We need
510+
* to pick up the context at the point where the completer was created.
511+
*/
512+
def completeInCreationContext(denot: SymDenotation): Unit = {
499513
denot.info = typeSig(denot.symbol)
514+
addAnnotations(denot)
515+
}
500516
}
501517

502518
class ClassCompleter(cls: ClassSymbol, original: TypeDef)(ictx: Context) extends Completer(original)(ictx) {
@@ -563,6 +579,7 @@ class Namer { typer: Typer =>
563579

564580
index(rest)(inClassContext(selfInfo))
565581
denot.info = ClassInfo(cls.owner.thisType, cls, parentRefs, decls, selfInfo)
582+
addAnnotations(denot)
566583
cls.setApplicableFlags(
567584
(NoInitsInterface /: impl.body)((fs, stat) => fs & defKind(stat)))
568585
}
@@ -586,6 +603,13 @@ class Namer { typer: Typer =>
586603
def typedAheadExpr(tree: Tree, pt: Type = WildcardType)(implicit ctx: Context): tpd.Tree =
587604
typedAheadImpl(tree, pt)(ctx retractMode Mode.PatternOrType)
588605

606+
def typedAheadAnnotation(tree: Tree)(implicit ctx: Context): Symbol = tree match {
607+
case Apply(fn, _) => typedAheadAnnotation(fn)
608+
case TypeApply(fn, _) => typedAheadAnnotation(fn)
609+
case Select(qual, nme.CONSTRUCTOR) => typedAheadAnnotation(qual)
610+
case New(tpt) => typedAheadType(tpt).tpe.classSymbol
611+
}
612+
589613
/** Enter and typecheck parameter list */
590614
def completeParams(params: List[MemberDef])(implicit ctx: Context) = {
591615
index(params)

src/dotty/tools/dotc/typer/ReTyper.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ class ReTyper extends Typer {
7272
override def tryInsertApplyOrImplicit(tree: Tree, pt: ProtoType)(fallBack: (Tree, TyperState) => Tree)(implicit ctx: Context): Tree =
7373
fallBack(tree, ctx.typerState)
7474

75-
override def addTypedModifiersAnnotations(mdef: untpd.MemberDef, sym: Symbol)(implicit ctx: Context): Unit =
76-
() // was: typedModifiers(Modifiers(sym), sym)
77-
// but annotations are not transformed after typer, so no use to check them.
75+
override def completeAnnotations(mdef: untpd.MemberDef, sym: Symbol)(implicit ctx: Context): Unit = ()
7876

7977
override def ensureConstrCall(cls: ClassSymbol, parents: List[Tree])(implicit ctx: Context): List[Tree] =
8078
parents

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
860860
assignType(cpy.Alternative(tree)(trees1), trees1)
861861
}
862862

863-
def addTypedModifiersAnnotations(mdef: untpd.MemberDef, sym: Symbol)(implicit ctx: Context): Unit = {
864-
val mods1 = typedModifiers(untpd.modsDeco(mdef).mods, sym)
865-
for (tree <- mods1.annotations) sym.addAnnotation(Annotation(tree))
866-
}
867-
868-
def typedModifiers(mods: untpd.Modifiers, sym: Symbol)(implicit ctx: Context): Modifiers = track("typedModifiers") {
869-
val annotations1 = mods.annotations mapconserve typedAnnotation
870-
if (annotations1 eq mods.annotations) mods.asInstanceOf[Modifiers]
871-
else Modifiers(mods.flags, mods.privateWithin, annotations1)
863+
def completeAnnotations(mdef: untpd.MemberDef, sym: Symbol)(implicit ctx: Context): Unit = {
864+
// necessary to force annotation trees to be computed.
865+
sym.annotations.foreach(_.tree)
866+
// necessary in order to mark the typed ahead annotations as definitiely typed:
867+
untpd.modsDeco(mdef).mods.annotations.mapconserve(typedAnnotation)
872868
}
873869

874870
def typedAnnotation(annot: untpd.Tree)(implicit ctx: Context): Tree = track("typedAnnotation") {
@@ -877,7 +873,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
877873

878874
def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context) = track("typedValDef") {
879875
val ValDef(name, tpt, _) = vdef
880-
addTypedModifiersAnnotations(vdef, sym)
876+
completeAnnotations(vdef, sym)
881877
val tpt1 = typedType(tpt)
882878
val rhs1 = vdef.rhs match {
883879
case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe
@@ -888,7 +884,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
888884

889885
def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context) = track("typedDefDef") {
890886
val DefDef(name, tparams, vparamss, tpt, _) = ddef
891-
addTypedModifiersAnnotations(ddef, sym)
887+
completeAnnotations(ddef, sym)
892888
val tparams1 = tparams mapconserve (typed(_).asInstanceOf[TypeDef])
893889
val vparamss1 = vparamss nestedMapconserve (typed(_).asInstanceOf[ValDef])
894890
if (sym is Implicit) checkImplicitParamsNotSingletons(vparamss1)
@@ -900,7 +896,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
900896

901897
def typedTypeDef(tdef: untpd.TypeDef, sym: Symbol)(implicit ctx: Context): Tree = track("typedTypeDef") {
902898
val TypeDef(name, rhs) = tdef
903-
addTypedModifiersAnnotations(tdef, sym)
899+
completeAnnotations(tdef, sym)
904900
val _ = typedType(rhs) // unused, typecheck only to remove from typedTree
905901
assignType(cpy.TypeDef(tdef)(name, TypeTree(sym.info), Nil), sym)
906902
}
@@ -916,7 +912,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
916912
result
917913
}
918914

919-
addTypedModifiersAnnotations(cdef, cls)
915+
completeAnnotations(cdef, cls)
920916
val constr1 = typed(constr).asInstanceOf[DefDef]
921917
val parentsWithClass = ensureFirstIsClass(parents mapconserve typedParent, cdef.pos.toSynthetic)
922918
val parents1 = ensureConstrCall(cls, parentsWithClass)(superCtx)

0 commit comments

Comments
 (0)