Skip to content

Commit 79f7e05

Browse files
committed
Merge pull request scala#3631 from adriaanm/t4492
SI-4492 More informative error when class not found on classpath
2 parents 492624d + b310d8c commit 79f7e05

File tree

7 files changed

+44
-19
lines changed

7 files changed

+44
-19
lines changed

src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ trait ContextErrors {
359359
//setError(sel)
360360
}
361361

362+
def SelectWithUnderlyingError(sel: Tree, err: AbsTypeError) = {
363+
// if there's no position, this is likely the result of a MissingRequirementError
364+
// use the position of the selection we failed to type check to report the original message
365+
if (err.errPos == NoPosition) issueNormalTypeError(sel, err.errMsg)
366+
else issueTypeError(err)
367+
setError(sel)
368+
}
369+
362370
//typedNew
363371
def IsAbstractError(tree: Tree, sym: Symbol) = {
364372
issueNormalTypeError(tree, sym + " is abstract; cannot be instantiated")

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ trait Contexts { self: Analyzer =>
103103

104104
// there must be a scala.xml package when xml literals were parsed in this unit
105105
if (unit.hasXml && ScalaXmlPackage == NoSymbol)
106-
unit.error(unit.firstXmlPos, "To compile XML syntax, the scala.xml package must be on the classpath.\nPlease see https://github.com/scala/scala/wiki/Scala-2.11#xml.")
106+
unit.error(unit.firstXmlPos, "To compile XML syntax, the scala.xml package must be on the classpath.\nPlease see http://docs.scala-lang.org/overviews/core/scala-2.11.html#scala-xml.")
107107

108108
// scala-xml needs `scala.xml.TopScope` to be in scope globally as `$scope`
109109
// We detect `scala-xml` by looking for `scala.xml.TopScope` and
@@ -544,6 +544,8 @@ trait Contexts { self: Analyzer =>
544544
if (checking) onTreeCheckerError(pos, msg) else unit.error(pos, msg)
545545

546546
@inline private def issueCommon(err: AbsTypeError)(pf: PartialFunction[AbsTypeError, Unit]) {
547+
// TODO: are errors allowed to have pos == NoPosition??
548+
// if not, Jason suggests doing: val pos = err.errPos.orElse( { devWarning("Que?"); context.tree.pos })
547549
if (settings.Yissuedebug) {
548550
log("issue error: " + err.errMsg)
549551
(new Exception).printStackTrace()

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4670,8 +4670,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
46704670
case SilentTypeError(err: AccessTypeError) =>
46714671
(tree1, Some(err))
46724672
case SilentTypeError(err) =>
4673-
context issue err
4674-
return setError(tree)
4673+
SelectWithUnderlyingError(tree, err)
4674+
return tree
46754675
case SilentResultValue(treeAndPre) =>
46764676
(stabilize(treeAndPre._1, treeAndPre._2, mode, pt), None)
46774677
}

src/reflect/scala/reflect/internal/pickling/UnPickler.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,20 @@ abstract class UnPickler {
229229
NoSymbol
230230
}
231231

232+
def moduleAdvice(missing: String): String = {
233+
val module =
234+
if (missing.startsWith("scala.xml")) Some(("org.scala-lang.modules", "scala-xml"))
235+
else if (missing.startsWith("scala.util.parsing")) Some(("org.scala-lang.modules", "scala-parser-combinators"))
236+
else if (missing.startsWith("scala.swing")) Some(("org.scala-lang.modules", "scala-swing"))
237+
else if (missing.startsWith("scala.util.continuations")) Some(("org.scala-lang.plugins", "scala-continuations-library"))
238+
else None
239+
240+
(module map { case (group, art) =>
241+
s"""\n(NOTE: It looks like the $art module is missing; try adding a dependency on "$group" : "$art".
242+
| See http://docs.scala-lang.org/overviews/core/scala-2.11.html for more information.)""".stripMargin
243+
} getOrElse "")
244+
}
245+
232246
// (1) Try name.
233247
fromName(name) orElse {
234248
// (2) Try with expanded name. Can happen if references to private
@@ -240,11 +254,12 @@ abstract class UnPickler {
240254
// (4) Call the mirror's "missing" hook.
241255
adjust(mirrorThatLoaded(owner).missingHook(owner, name)) orElse {
242256
// (5) Create a stub symbol to defer hard failure a little longer.
257+
val fullName = s"${owner.fullName}.$name"
243258
val missingMessage =
244-
s"""|bad symbolic reference. A signature in $filename refers to ${name.longString}
245-
|in ${owner.kindString} ${owner.fullName} which is not available.
246-
|It may be completely missing from the current classpath, or the version on
247-
|the classpath might be incompatible with the version used when compiling $filename.""".stripMargin
259+
s"""|bad symbolic reference to $fullName encountered in class file '$filename'.
260+
|Cannot access ${name.longString} in ${owner.kindString} ${owner.fullName}. The current classpath may be
261+
|missing a definition for $fullName, or $filename may have been compiled against a version that's
262+
|incompatible with the one found on the current classpath.${moduleAdvice(fullName)}""".stripMargin
248263
owner.newStubSymbol(name, missingMessage)
249264
}
250265
}

test/files/neg/t5148.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error: bad symbolic reference. A signature in Imports.class refers to type Request
2-
in class scala.tools.nsc.interpreter.IMain which is not available.
3-
It may be completely missing from the current classpath, or the version on
4-
the classpath might be incompatible with the version used when compiling Imports.class.
1+
error: bad symbolic reference to scala.tools.nsc.interpreter.IMain.Request encountered in class file 'Imports.class'.
2+
Cannot access type Request in class scala.tools.nsc.interpreter.IMain. The current classpath may be
3+
missing a definition for scala.tools.nsc.interpreter.IMain.Request, or Imports.class may have been compiled against a version that's
4+
incompatible with the one found on the current classpath.
55
one error found

test/files/run/t6440.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pos: source-newSource1.scala,line-9,offset=109 bad symbolic reference. A signature in U.class refers to term pack1
2-
in package <root> which is not available.
3-
It may be completely missing from the current classpath, or the version on
4-
the classpath might be incompatible with the version used when compiling U.class. ERROR
1+
pos: source-newSource1.scala,line-9,offset=109 bad symbolic reference to <root>.pack1 encountered in class file 'U.class'.
2+
Cannot access term pack1 in package <root>. The current classpath may be
3+
missing a definition for <root>.pack1, or U.class may have been compiled against a version that's
4+
incompatible with the one found on the current classpath. ERROR

test/files/run/t6440b.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pos: NoPosition bad symbolic reference. A signature in U.class refers to type T
2-
in package pack1 which is not available.
3-
It may be completely missing from the current classpath, or the version on
4-
the classpath might be incompatible with the version used when compiling U.class. ERROR
1+
pos: NoPosition bad symbolic reference to pack1.T encountered in class file 'U.class'.
2+
Cannot access type T in package pack1. The current classpath may be
3+
missing a definition for pack1.T, or U.class may have been compiled against a version that's
4+
incompatible with the one found on the current classpath. ERROR

0 commit comments

Comments
 (0)