Skip to content

Commit 47d2084

Browse files
authored
Merge pull request #1693 from dotty-staging/add-annotations-phase
Add annotations phase
2 parents b9350f4 + e1d79a2 commit 47d2084

17 files changed

+227
-85
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object Trees {
3232
/** Property key for trees with documentation strings attached */
3333
val DocComment = new Property.Key[Comment]
3434

35-
@sharable private var nextId = 0 // for debugging
35+
@sharable private var nextId = 0 // for debugging
3636

3737
type LazyTree = AnyRef /* really: Tree | Lazy[Tree] */
3838
type LazyTreeList = AnyRef /* really: List[Tree] | Lazy[List[Tree]] */

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ object Contexts {
216216
else if (isNonEmptyScopeContext) scope.implicitDecls
217217
else Nil
218218
val outerImplicits =
219-
if (isImportContext && importInfo.hiddenRoot.exists)
220-
outer.implicits exclude importInfo.hiddenRoot
219+
if (isImportContext && importInfo.unimported.exists)
220+
outer.implicits exclude importInfo.unimported
221221
else
222222
outer.implicits
223223
if (implicitRefs.isEmpty) outerImplicits
@@ -422,9 +422,18 @@ object Contexts {
422422
final def withOwner(owner: Symbol): Context =
423423
if (owner ne this.owner) fresh.setOwner(owner) else this
424424

425-
override def toString =
425+
final def withProperty[T](key: Key[T], value: Option[T]): Context =
426+
if (property(key) == value) this
427+
else value match {
428+
case Some(v) => fresh.setProperty(key, v)
429+
case None => fresh.dropProperty(key)
430+
}
431+
432+
override def toString = {
433+
def iinfo(implicit ctx: Context) = if (ctx.importInfo == null) "" else i"${ctx.importInfo.selectors}%, %"
426434
"Context(\n" +
427-
(outersIterator map ( ctx => s" owner = ${ctx.owner}, scope = ${ctx.scope}") mkString "\n")
435+
(outersIterator map ( ctx => s" owner = ${ctx.owner}, scope = ${ctx.scope}, import = ${iinfo(ctx)}") mkString "\n")
436+
}
428437
}
429438

430439
/** A condensed context provides only a small memory footprint over
@@ -468,6 +477,9 @@ object Contexts {
468477
def setProperty[T](key: Key[T], value: T): this.type =
469478
setMoreProperties(moreProperties.updated(key, value))
470479

480+
def dropProperty(key: Key[_]): this.type =
481+
setMoreProperties(moreProperties - key)
482+
471483
def setPhase(pid: PhaseId): this.type = setPeriod(Period(runId, pid))
472484
def setPhase(phase: Phase): this.type = setPeriod(Period(runId, phase.start, phase.end))
473485

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,13 @@ class Definitions {
650650
def isTupleClass(cls: Symbol) = isVarArityClass(cls, tpnme.Tuple)
651651
def isProductClass(cls: Symbol) = isVarArityClass(cls, tpnme.Product)
652652

653+
val predefClassNames: Set[Name] =
654+
Set("Predef$", "DeprecatedPredef", "LowPriorityImplicits").map(_.toTypeName)
655+
656+
/** Is `cls` the predef module class, or a class inherited by Predef? */
657+
def isPredefClass(cls: Symbol) =
658+
(cls.owner eq ScalaPackageClass) && predefClassNames.contains(cls.name)
659+
653660
val StaticRootImportFns = List[() => TermRef](
654661
() => JavaLangPackageVal.termRef,
655662
() => ScalaPackageVal.termRef

compiler/src/dotty/tools/dotc/core/Phases.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ trait Phases {
2626

2727
def phasesStack: List[Phase] =
2828
if ((this eq NoContext) || !phase.exists) Nil
29-
else phase :: outersIterator.dropWhile(_.phase == phase).next.phasesStack
29+
else {
30+
val rest = outersIterator.dropWhile(_.phase == phase)
31+
phase :: (if (rest.hasNext) rest.next.phasesStack else Nil)
32+
}
3033

3134
/** Execute `op` at given phase */
3235
def atPhase[T](phase: Phase)(op: Context => T): T =

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
614614
(sym.allOverriddenSymbols exists (_ is TypeParam))
615615

616616
override def toText(sym: Symbol): Text = {
617-
if (sym.isImport) {
618-
def importString(tree: untpd.Tree) = s"import ${tree.show}"
617+
if (sym.isImport)
619618
sym.infoOrCompleter match {
620-
case info: Namer#Completer => return importString(info.original)
621-
case info: ImportType => return importString(info.expr)
619+
case info: Namer#Completer => return info.original.show
620+
case info: ImportType => return s"import $info.expr.show"
622621
case _ =>
623622
}
624-
}
625623
if (sym.is(ModuleClass))
626624
kindString(sym) ~~ (nameString(sym.name.stripModuleClassSuffix) + idString(sym))
627625
else

compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.io.{
66
File, PrintWriter, PrintStream, StringWriter, Writer, OutputStream,
77
ByteArrayOutputStream => ByteOutputStream
88
}
9-
import java.lang.{Class, ClassLoader}
9+
import java.lang.{Class, ClassLoader, Thread, System, StringBuffer}
1010
import java.net.{URL, URLClassLoader}
1111

1212
import scala.collection.immutable.ListSet

compiler/src/dotty/tools/dotc/repl/InterpreterLoop.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package repl
44

55
import java.io.{BufferedReader, File, FileReader, PrintWriter}
66
import java.io.IOException
7-
import java.lang.{ClassLoader, System}
7+
import java.lang.{ClassLoader, System, Thread}
88
import scala.concurrent.{Future, Await}
99
import scala.concurrent.duration.Duration
1010
import reporting.Reporter

compiler/src/dotty/tools/dotc/transform/CollectEntryPoints.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import dotty.tools.dotc.core.Symbols.NoSymbol
99
import scala.annotation.tailrec
1010
import dotty.tools.dotc.core._
1111
import Symbols._
12-
import scala.Some
1312
import dotty.tools.dotc.transform.TreeTransforms.{NXTransformations, TransformerInfo, TreeTransform, TreeTransformer}
1413
import dotty.tools.dotc.ast.tpd
1514
import dotty.tools.dotc.core.Contexts.Context

compiler/src/dotty/tools/dotc/typer/FrontEnd.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class FrontEnd extends Phase {
4444
typr.println("entered: " + unit.source)
4545
}
4646

47+
def enterAnnotations(implicit ctx: Context) = monitor("annotating") {
48+
val unit = ctx.compilationUnit
49+
ctx.typer.annotate(unit.untpdTree :: Nil)
50+
typr.println("annotated: " + unit.source)
51+
}
52+
4753
def typeCheck(implicit ctx: Context) = monitor("typechecking") {
4854
val unit = ctx.compilationUnit
4955
unit.tpdTree = ctx.typer.typedExpr(unit.untpdTree)
@@ -69,8 +75,9 @@ class FrontEnd extends Phase {
6975
}
7076
unitContexts foreach (parse(_))
7177
record("parsedTrees", ast.Trees.ntrees)
72-
unitContexts foreach (enterSyms(_))
73-
unitContexts foreach (typeCheck(_))
78+
unitContexts.foreach(enterSyms(_))
79+
unitContexts.foreach(enterAnnotations(_))
80+
unitContexts.foreach(typeCheck(_))
7481
record("total trees after typer", ast.Trees.ntrees)
7582
unitContexts.map(_.compilationUnit).filterNot(discardAfterTyper)
7683
}

compiler/src/dotty/tools/dotc/typer/ImportInfo.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,22 @@ class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree], val isRootImp
9595
/** The root import symbol hidden by this symbol, or NoSymbol if no such symbol is hidden.
9696
* Note: this computation needs to work even for un-initialized import infos, and
9797
* is not allowed to force initialization.
98+
*
99+
* TODO: Once we have fully bootstrapped, I would prefer if we expressed
100+
* unimport with an `override` modifier, and generalized it to all imports.
101+
* I believe this would be more transparent than the curren set of conditions. E.g.
102+
*
103+
* override import Predef.{any2stringAdd => _, StringAdd => _, _} // disables String +
104+
* override import java.lang.{} // disables all imports
98105
*/
99-
lazy val hiddenRoot: Symbol = {
100-
val sym = site.termSymbol
101-
def hasMaskingSelector = selectors exists {
106+
lazy val unimported: Symbol = {
107+
lazy val sym = site.termSymbol
108+
val hasMaskingSelector = selectors exists {
102109
case Thicket(_ :: Ident(nme.WILDCARD) :: Nil) => true
103110
case _ => false
104111
}
105-
if ((defn.RootImportTypes exists (_.symbol == sym)) && hasMaskingSelector) sym else NoSymbol
112+
if (hasMaskingSelector && defn.RootImportTypes.exists(_.symbol == sym)) sym
113+
else NoSymbol
106114
}
107115

108116
override def toString = {

0 commit comments

Comments
 (0)