Skip to content

Include possible candidate classes in "not found" error #13

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

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 21 additions & 0 deletions src/compiler/scala/tools/nsc/backend/JavaPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ trait JavaPlatform extends Platform {

private[nsc] var currentClassPath: Option[MergedClassPath[AbstractFile]] = None

val forceIndexPackages: collection.mutable.Set[String] = collection.mutable.Set.empty
val classPathIndexByClassName: collection.mutable.Map[String, Set[String]] = collection.mutable.Map.empty

def addForceIndexPackages(packages: Set[String]) = {
forceIndexPackages ++= packages
}

def updateIndex() = {
def addIfInitialized(pkg: Symbol): Unit = {
if (pkg.hasCompleteInfo || forceIndexPackages.exists(p => p startsWith pkg.fullName)) {
for (m <- pkg.info.members if m != pkg) { // _root_ is member of _root_ it seems, so filter m != pkg
if (m.isClass)
classPathIndexByClassName(m.name.toString) = classPathIndexByClassName.getOrElse(m.name.toString, Set.empty) + m.fullName
if (m.moduleClass.isPackageClass)
addIfInitialized(m.moduleClass)
}
}
}
addIfInitialized(rootMirror.getPackage("_root_": TermName))
}

def classPath: ClassPath[AbstractFile] = {
assert(settings.YclasspathImpl.value == ClassPathRepresentationType.Recursive,
"To use recursive classpath representation you must enable it with -YclasspathImpl:recursive compiler option.")
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,15 @@ trait ContextErrors {
NormalTypeError(tree, "reference to " + name + " is ambiguous;\n" + msg)

def SymbolNotFoundError(tree: Tree, name: Name, owner: Symbol, startingIdentCx: Context) = {
NormalTypeError(tree, "not found: "+decodeWithKind(name, owner))
platform.updateIndex()
lazy val candidatesString = name match {
case tn: TypeName =>
val cs = platform.classPathIndexByClassName.getOrElse(tn.toString, Set.empty)
if (cs.nonEmpty) cs.mkString(". You may want to import one of the following classes from your classpath:\n ", "\n ", "")
else ""
case _ => ""
}
NormalTypeError(tree, "not found: "+decodeWithKind(name, owner) + candidatesString)
}

// typedAppliedTypeTree
Expand Down
2 changes: 2 additions & 0 deletions src/repl/scala/tools/nsc/interpreter/IMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import java.io.File
class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Settings, protected val out: JPrintWriter) extends AbstractScriptEngine with Compilable with Imports {
imain =>

val api = new PresentationCompilerAPI(this)

setBindings(createBindings, ScriptContext.ENGINE_SCOPE)
object replOutput extends ReplOutput(settings.Yreploutdir) { }

Expand Down
11 changes: 11 additions & 0 deletions src/repl/scala/tools/nsc/interpreter/PresentationCompilerAPI.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scala.tools.nsc.interpreter

class PresentationCompilerAPI(interpreter: IMain) {
/**
* Index the top-level classes in the the provided packages for the `not found: Class` error
* message.
*/
def scanPackagesForClassNotFoundMessage(packages: Set[String]): Unit = {
interpreter.global.platform.addForceIndexPackages(packages)
}
}