-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #4758: Revert "New phase for entering annotations" #4776
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -687,90 +687,14 @@ class Namer { typer: Typer => | |
ctxWithStats | ||
} | ||
|
||
/** Add all annotations of definitions in `stats` to the defined symbols */ | ||
def annotate(stats: List[Tree])(implicit ctx: Context): Unit = { | ||
def recur(stat: Tree): Unit = stat match { | ||
case pcl: PackageDef => | ||
annotate(pcl.stats) | ||
case stat: untpd.MemberDef => | ||
stat.getAttachment(SymOfTree) match { | ||
case Some(sym) => | ||
sym.infoOrCompleter match { | ||
case info: Completer if !defn.isPredefClass(sym.owner) => | ||
// Annotate Predef methods only when they are completed; | ||
// This is necessary to break a cyclic dependence between `Predef` | ||
// and `deprecated` in test `compileStdLib`. | ||
addAnnotations(sym, stat)(info.creationContext) | ||
case _ => | ||
// Annotations were already added as part of the symbol's completion | ||
} | ||
case none => | ||
assert(stat.typeOpt.exists, i"no symbol for $stat") | ||
} | ||
case stat: untpd.Thicket => | ||
stat.trees.foreach(recur) | ||
case _ => | ||
} | ||
|
||
for (stat <- stats) recur(expanded(stat)) | ||
} | ||
|
||
/** Add annotations of `stat` to `sym`. | ||
* This method can be called twice on a symbol (e.g. once | ||
* during the `annotate` phase and then again during completion). | ||
* Therefore, care needs to be taken not to add annotations again | ||
* that are already added to the symbol. | ||
*/ | ||
def addAnnotations(sym: Symbol, stat: MemberDef)(implicit ctx: Context) = { | ||
// (1) The context in which an annotation of a top-level class or module is evaluated | ||
// is the closest enclosing context which has the enclosing package as owner. | ||
// (2) The context in which an annotation for any other symbol is evaluated is the | ||
// closest enclosing context which has the owner of the class enclosing the symbol as owner. | ||
// E.g in | ||
// | ||
// package p | ||
// import a.b | ||
// class C { | ||
// import d.e | ||
// @ann m() ... | ||
// } | ||
// | ||
// `@ann` is evaluated in the context just outside `C`, where the `a.b` | ||
// import is visible but the `d.e` import is forgotten. This measure is necessary | ||
// in order to avoid cycles. | ||
lazy val annotCtx = { | ||
var target = sym.owner.lexicallyEnclosingClass | ||
if (!target.is(PackageClass)) target = target.owner | ||
var c = ctx | ||
while (c.owner != target) c = c.outer | ||
c | ||
} | ||
|
||
for (annotTree <- untpd.modsDeco(stat).mods.annotations) { | ||
val cls = typedAheadAnnotationClass(annotTree)(annotCtx) | ||
if (sym.unforcedAnnotation(cls).isEmpty) { | ||
val ann = Annotation.deferred(cls, implicit ctx => typedAheadAnnotation(annotTree)) | ||
sym.addAnnotation(ann) | ||
if (cls == defn.ForceInlineAnnot && sym.is(Method, butNot = Accessor)) | ||
sym.setFlag(Inline) | ||
} | ||
} | ||
} | ||
|
||
def indexAndAnnotate(stats: List[Tree])(implicit ctx: Context): Context = { | ||
val localCtx = index(stats) | ||
annotate(stats) | ||
localCtx | ||
} | ||
|
||
/** Index and annotate symbols in `tree` while asserting the `lateCompile` flag. | ||
/** Index symbols in `tree` while asserting the `lateCompile` flag. | ||
* This will cause any old top-level symbol with the same fully qualified | ||
* name as a newly created symbol to be replaced. | ||
*/ | ||
def lateEnter(tree: Tree)(implicit ctx: Context) = { | ||
val saved = lateCompile | ||
lateCompile = true | ||
try indexAndAnnotate(tree :: Nil) finally lateCompile = saved | ||
try index(tree :: Nil) finally lateCompile = saved | ||
} | ||
|
||
def missingType(sym: Symbol, modifier: String)(implicit ctx: Context) = { | ||
|
@@ -822,6 +746,22 @@ class Namer { typer: Typer => | |
else completeInCreationContext(denot) | ||
} | ||
|
||
protected def addAnnotations(sym: Symbol): Unit = original match { | ||
case original: untpd.MemberDef => | ||
var hasInlineAnnot = false | ||
lazy val annotCtx = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're at it, you may want to make an annotCtx def that can be used both here and for https://github.com/lampepfl/dotty/blob/64bd0fe5210cacd7c17ab285c80712944157c212/compiler/src/dotty/tools/dotc/typer/Typer.scala#L1345 :) |
||
if (sym.is(Param)) ctx.outersIterator.dropWhile(_.owner == sym.owner).next | ||
else ctx | ||
for (annotTree <- untpd.modsDeco(original).mods.annotations) { | ||
val cls = typedAheadAnnotationClass(annotTree)(annotCtx) | ||
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree)) | ||
sym.addAnnotation(ann) | ||
if (cls == defn.ForceInlineAnnot && sym.is(Method, butNot = Accessor)) | ||
sym.setFlag(Inline) | ||
} | ||
case _ => | ||
} | ||
|
||
private def addInlineInfo(sym: Symbol) = original match { | ||
case original: untpd.DefDef if sym.isInlineableMethod => | ||
Inliner.registerInlineInfo( | ||
|
@@ -836,10 +776,7 @@ class Namer { typer: Typer => | |
*/ | ||
def completeInCreationContext(denot: SymDenotation): Unit = { | ||
val sym = denot.symbol | ||
original match { | ||
case original: MemberDef => addAnnotations(sym, original) | ||
case _ => | ||
} | ||
addAnnotations(sym) | ||
addInlineInfo(sym) | ||
denot.info = typeSig(sym) | ||
Checking.checkWellFormed(sym) | ||
|
@@ -955,7 +892,7 @@ class Namer { typer: Typer => | |
} | ||
} | ||
|
||
addAnnotations(denot.symbol, original) | ||
addAnnotations(denot.symbol) | ||
|
||
val selfInfo = | ||
if (self.isEmpty) NoType | ||
|
@@ -978,9 +915,7 @@ class Namer { typer: Typer => | |
// accessors, that's why the constructor needs to be completed before | ||
// the parent types are elaborated. | ||
index(constr) | ||
annotate(constr :: params) | ||
|
||
indexAndAnnotate(rest)(ctx.inClassContext(selfInfo)) | ||
index(rest)(ctx.inClassContext(selfInfo)) | ||
symbolOfTree(constr).ensureCompleted() | ||
|
||
val parentTypes = ensureFirstIsClass(parents.map(checkedParentType(_)), cls.pos) | ||
|
@@ -1025,7 +960,7 @@ class Namer { typer: Typer => | |
|
||
/** Enter and typecheck parameter list */ | ||
def completeParams(params: List[MemberDef])(implicit ctx: Context) = { | ||
indexAndAnnotate(params) | ||
index(params) | ||
for (param <- params) typedAheadExpr(param) | ||
} | ||
|
||
|
@@ -1222,7 +1157,7 @@ class Namer { typer: Typer => | |
// 3. Info of CP is computed (to be copied to DP). | ||
// 4. CP is completed. | ||
// 5. Info of CP is copied to DP and DP is completed. | ||
indexAndAnnotate(tparams) | ||
index(tparams) | ||
if (isConstructor) sym.owner.typeParams.foreach(_.ensureCompleted()) | ||
for (tparam <- tparams) typedAheadExpr(tparam) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package foo.bar | ||
|
||
class Bar extends annotation.StaticAnnotation | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package foo | ||
|
||
import foo.bar.Bar | ||
|
||
@Bar | ||
class Foo extends annotation.StaticAnnotation | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package object foo { | ||
@Foo class Hello | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead variable.