From e06244cb55797d9928a52a22a548d547555be733 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 5 Apr 2011 01:48:11 +0000 Subject: Added a long overdue warning for when people de... Added a long overdue warning for when people define a class and then an object or vice versa in the repl. scala> object Bippy { implicit val myImplicitsDontWork = 5 } defined module Bippy scala> trait Bippy { val x = "hello" } defined trait Bippy warning: previously defined object Bippy is not a companion to trait Bippy. Companions must be defined together; you may wish to use :paste mode for this. No review. --- .../scala/tools/nsc/interpreter/IMain.scala | 31 +++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala index 8698aa7890..9dbcf37557 100644 --- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala +++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala @@ -354,6 +354,22 @@ class IMain(val settings: Settings, protected val out: PrintWriter) { prevRequests += req req.referencedNames foreach (x => referencedNameMap(x) = req) + // warning about serially defining companions. It'd be easy + // enough to just redefine them together but that may not always + // be what people want so I'm waiting until I can do it better. + if (!settings.nowarnings.value) { + for { + name <- req.definedNames filterNot (x => req.definedNames contains x.companionName) + oldReq <- definedNameMap get name.companionName + newSym <- req.definedSymbols get name + oldSym <- oldReq.definedSymbols get name.companionName + } { + printMessage("warning: previously defined %s is not a companion to %s.".format(oldSym, newSym)) + printMessage("Companions must be defined together; you may wish to use :paste mode for this.") + } + } + + // Updating the defined name map req.definedNames foreach { name => if (definedNameMap contains name) { if (name.isTypeName) handleTypeRedefinition(name.toTypeName, definedNameMap(name), req) @@ -879,8 +895,10 @@ class IMain(val settings: Settings, protected val out: PrintWriter) { // lazy val definedTypes: Map[Name, Type] = { // typeNames map (x => x -> afterTyper(resultSymbol.info.nonPrivateDecl(x).tpe)) toMap // } - lazy val definedSymbols: Map[Name, Symbol] = - termNames map (x => x -> applyToResultMember(x, x => x)) toMap + lazy val definedSymbols: Map[Name, Symbol] = ( + termNames.map(x => x -> applyToResultMember(x, x => x)) ++ + typeNames.map(x => x -> compilerTypeOf.get(x).map(_.typeSymbol).getOrElse(NoSymbol)) + ).toMap lazy val typesOfDefinedTerms: Map[Name, Type] = termNames map (x => x -> applyToResultMember(x, _.tpe)) toMap @@ -1045,6 +1063,7 @@ class IMain(val settings: Settings, protected val out: PrintWriter) { def importHandlers = allHandlers collect { case x: ImportHandler => x } def definedTerms = onlyTerms(allDefinedNames) filterNot isInternalVarName def definedTypes = onlyTypes(allDefinedNames) + def definedSymbols = prevRequests.toSet flatMap ((x: Request) => x.definedSymbols.values) def importedTerms = onlyTerms(importHandlers flatMap (_.importedNames)) def importedTypes = onlyTypes(importHandlers flatMap (_.importedNames)) @@ -1083,10 +1102,10 @@ class IMain(val settings: Settings, protected val out: PrintWriter) { } def wildcardTypes = languageWildcards ++ sessionWildcards - def languageSymbols = languageWildcardSyms flatMap membersAtPickler - def sessionSymbols = importHandlers flatMap (_.importedSymbols) - def importedSymbols = languageSymbols ++ sessionSymbols - def implicitSymbols = importedSymbols filter (_.isImplicit) + def languageSymbols = languageWildcardSyms flatMap membersAtPickler + def sessionImportedSymbols = importHandlers flatMap (_.importedSymbols) + def importedSymbols = languageSymbols ++ sessionImportedSymbols + def implicitSymbols = importedSymbols filter (_.isImplicit) /** Tuples of (source, imported symbols) in the order they were imported. */ -- cgit v1.2.3