Skip to content

Evaluate annotations in Namer #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import annotation.tailrec
import ErrorReporting._
import tpd.ListOfTreeDecorator
import config.Printers._
import Annotations._
import language.implicitConversions

trait NamerContextOps { this: Context =>
Expand Down Expand Up @@ -495,8 +496,23 @@ class Namer { typer: Typer =>
completeInCreationContext(denot)
}

def completeInCreationContext(denot: SymDenotation): Unit =
protected def addAnnotations(denot: SymDenotation): Unit = original match {
case original: untpd.MemberDef =>
for (annotTree <- untpd.modsDeco(original).mods.annotations) {
val cls = typedAheadAnnotation(annotTree)
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
denot.addAnnotation(ann)
}
case _ =>
}

/** Intentionally left without `implicit ctx` parameter. We need
* to pick up the context at the point where the completer was created.
*/
def completeInCreationContext(denot: SymDenotation): Unit = {
denot.info = typeSig(denot.symbol)
addAnnotations(denot)
}
}

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

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

def typedAheadAnnotation(tree: Tree)(implicit ctx: Context): Symbol = tree match {
case Apply(fn, _) => typedAheadAnnotation(fn)
case TypeApply(fn, _) => typedAheadAnnotation(fn)
case Select(qual, nme.CONSTRUCTOR) => typedAheadAnnotation(qual)
case New(tpt) => typedAheadType(tpt).tpe.classSymbol
}

/** Enter and typecheck parameter list */
def completeParams(params: List[MemberDef])(implicit ctx: Context) = {
index(params)
Expand Down
4 changes: 1 addition & 3 deletions src/dotty/tools/dotc/typer/ReTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ class ReTyper extends Typer {
override def tryInsertApplyOrImplicit(tree: Tree, pt: ProtoType)(fallBack: (Tree, TyperState) => Tree)(implicit ctx: Context): Tree =
fallBack(tree, ctx.typerState)

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

override def ensureConstrCall(cls: ClassSymbol, parents: List[Tree])(implicit ctx: Context): List[Tree] =
parents
Expand Down
22 changes: 9 additions & 13 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -859,15 +859,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
assignType(cpy.Alternative(tree)(trees1), trees1)
}

def addTypedModifiersAnnotations(mdef: untpd.MemberDef, sym: Symbol)(implicit ctx: Context): Unit = {
val mods1 = typedModifiers(untpd.modsDeco(mdef).mods, sym)
for (tree <- mods1.annotations) sym.addAnnotation(Annotation(tree))
}

def typedModifiers(mods: untpd.Modifiers, sym: Symbol)(implicit ctx: Context): Modifiers = track("typedModifiers") {
val annotations1 = mods.annotations mapconserve typedAnnotation
if (annotations1 eq mods.annotations) mods.asInstanceOf[Modifiers]
else Modifiers(mods.flags, mods.privateWithin, annotations1)
def completeAnnotations(mdef: untpd.MemberDef, sym: Symbol)(implicit ctx: Context): Unit = {
// necessary to force annotation trees to be computed.
sym.annotations.foreach(_.tree)
// necessary in order to mark the typed ahead annotations as definitiely typed:
untpd.modsDeco(mdef).mods.annotations.mapconserve(typedAnnotation)
}

def typedAnnotation(annot: untpd.Tree)(implicit ctx: Context): Tree = track("typedAnnotation") {
Expand All @@ -876,7 +872,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context) = track("typedValDef") {
val ValDef(name, tpt, _) = vdef
addTypedModifiersAnnotations(vdef, sym)
completeAnnotations(vdef, sym)
val tpt1 = typedType(tpt)
val rhs1 = vdef.rhs match {
case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe
Expand All @@ -887,7 +883,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context) = track("typedDefDef") {
val DefDef(name, tparams, vparamss, tpt, _) = ddef
addTypedModifiersAnnotations(ddef, sym)
completeAnnotations(ddef, sym)
val tparams1 = tparams mapconserve (typed(_).asInstanceOf[TypeDef])
val vparamss1 = vparamss nestedMapconserve (typed(_).asInstanceOf[ValDef])
if (sym is Implicit) checkImplicitParamsNotSingletons(vparamss1)
Expand All @@ -899,7 +895,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

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

addTypedModifiersAnnotations(cdef, cls)
completeAnnotations(cdef, cls)
val constr1 = typed(constr).asInstanceOf[DefDef]
val parentsWithClass = ensureFirstIsClass(parents mapconserve typedParent, cdef.pos.toSynthetic)
val parents1 = ensureConstrCall(cls, parentsWithClass)(superCtx)
Expand Down