From f2c351c8419a6b1fdeaa2c0a7047c8c25a16c7ce Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 9 Apr 2013 16:28:56 +0200 Subject: SI-7345 Rationalize overloads of Context#make Used default arguments and removed of variations only used in one place. I couldn't eliminate them all: one remaining overload avoids allocating a new context when the scope/owner/tree don't change, but moving this optimizatin to the primary overload of make breaks things. This is noted in a TODO comment. --- .../scala/tools/nsc/typechecker/Contexts.scala | 58 +++++++++------------- .../scala/tools/nsc/typechecker/Namers.scala | 3 +- .../doc/model/ModelFactoryImplicitSupport.scala | 2 +- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index b5b77072ca..a6bdfcbdbd 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -91,10 +91,10 @@ trait Contexts { self: Analyzer => def rootContext(unit: CompilationUnit, tree: Tree, erasedTypes: Boolean): Context = { var sc = startContext for (sym <- rootImports(unit)) { - sc = sc.makeNewImport(sym) + sc = sc.makeNewImport(gen.mkWildcardImport(sym)) sc.depth += 1 } - val c = sc.make(unit, tree, sc.owner, sc.scope, sc.imports) + val c = sc.make(tree, unit = unit) if (erasedTypes) c.setThrowErrors() else c.setReportErrors() c(EnrichmentEnabled | ImplicitsEnabled) = !erasedTypes c @@ -245,8 +245,10 @@ trait Contexts { self: Analyzer => def withMacrosEnabled[T](op: => T): T = withMode(enabled = MacrosEnabled)(op) def withMacrosDisabled[T](op: => T): T = withMode(disabled = MacrosEnabled)(op) - def make(unit: CompilationUnit, tree: Tree, owner: Symbol, - scope: Scope, imports: List[ImportInfo]): Context = { + def make(tree: Tree = tree, owner: Symbol = owner, + scope: Scope = scope, imports: List[ImportInfo] = imports, + unit: CompilationUnit = unit): Context = { + val c = new Context c.unit = unit c.tree = tree @@ -256,26 +258,25 @@ trait Contexts { self: Analyzer => c.restoreState(this.state) // note: ConstructorSuffix conditionally overwritten below. - tree match { - case Template(_, _, _) | PackageDef(_, _) => - c.enclClass = c - c.prefix = c.owner.thisType - c(ConstructorSuffix) = false - case _ => - c.enclClass = this.enclClass - c.prefix = - if (c.owner != this.owner && c.owner.isTerm) NoPrefix - else this.prefix + val isTemplateOrPackage = tree match { + case _: Template | _: PackageDef => true + case _ => false } - tree match { - case DefDef(_, _, _, _, _, _) => - c.enclMethod = c - case _ => - c.enclMethod = this.enclMethod + val isDefDef = tree match { + case _: DefDef => true + case _ => false } + c.prefix = + if (isTemplateOrPackage) c.owner.thisType + else if (c.owner != this.owner && c.owner.isTerm) NoPrefix + else prefix + c.enclClass = if (isTemplateOrPackage) c else enclClass + c(ConstructorSuffix) = !isTemplateOrPackage && c(ConstructorSuffix) + c.enclMethod = if (isDefDef) c else enclMethod + c.variance = this.variance - c.depth = if (scope == this.scope) this.depth else this.depth + 1 + c.depth = this.depth + (if (scope == this.scope) 0 else 1) c.imports = imports c.diagnostic = this.diagnostic c.typingIndentLevel = typingIndentLevel @@ -287,34 +288,23 @@ trait Contexts { self: Analyzer => c } - def makeNewImport(sym: Symbol): Context = - makeNewImport(gen.mkWildcardImport(sym)) - def makeNewImport(imp: Import): Context = { val impInfo = new ImportInfo(imp, depth) if (settings.lint && imp.pos.isDefined) // pos.isDefined excludes java.lang/scala/Predef imports allImportInfos(unit) ::= impInfo - make(unit, imp, owner, scope, impInfo :: imports) + make(imp, imports = impInfo :: imports) } def make(tree: Tree, owner: Symbol, scope: Scope): Context = + // TODO Moving this optimization into the main overload of `make` causes all tests to fail. Why? if (tree == this.tree && owner == this.owner && scope == this.scope) this - else make0(tree, owner, scope) - - private def make0(tree: Tree, owner: Symbol, scope: Scope): Context = - make(unit, tree, owner, scope, imports) + else make(tree, owner, scope, imports, unit) def makeNewScope(tree: Tree, owner: Symbol): Context = make(tree, owner, newNestedScope(scope)) // IDE stuff: distinguish between scopes created for typing and scopes created for naming. - def make(tree: Tree, owner: Symbol): Context = - make0(tree, owner, scope) - - def make(tree: Tree): Context = - make(tree, owner) - def makeSilent(reportAmbiguousErrors: Boolean, newtree: Tree = tree): Context = { val c = make(newtree) c.setBufferErrors() diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala index de3010c371..bbb1dbe8d8 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala @@ -47,7 +47,6 @@ trait Namers extends MethodSynthesis { private class NormalNamer(context: Context) extends Namer(context) def newNamer(context: Context): Namer = new NormalNamer(context) - def newNamerFor(context: Context, tree: Tree): Namer = newNamer(context.makeNewScope(tree, tree.symbol)) abstract class Namer(val context: Context) extends MethodSynth with NamerContextErrors { thisNamer => // overridden by the presentation compiler @@ -1629,7 +1628,7 @@ trait Namers extends MethodSynthesis { // @M an abstract type's type parameters are entered. // TODO: change to isTypeMember ? if (defnSym.isAbstractType) - newNamerFor(ctx, tree) enterSyms tparams //@M + newNamer(ctx.makeNewScope(tree, tree.symbol)) enterSyms tparams //@M restp complete sym } } diff --git a/src/scaladoc/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala b/src/scaladoc/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala index 1f87f935f2..65b00d4673 100644 --- a/src/scaladoc/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala +++ b/src/scaladoc/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala @@ -228,7 +228,7 @@ trait ModelFactoryImplicitSupport { try { context.flushBuffer() /* any errors here should not prevent future findings */ // TODO: Not sure this is the right thing to do -- seems similar to what scalac should be doing - val context2 = context.make(context.unit, context.tree, sym.owner, context.scope, context.imports) + val context2 = context.make(owner = sym.owner) val search = inferImplicit(EmptyTree, tpe, false, false, context2, false) context.flushBuffer() /* any errors here should not prevent future findings */ -- cgit v1.2.3