Skip to content

Commit 8e9873f

Browse files
committed
Improved a cyclic reference error message.
"illegal cyclic reference involving value <import>" not so useful.
1 parent 484af96 commit 8e9873f

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ trait TypeDiagnostics {
494494
def cyclicReferenceMessage(sym: Symbol, tree: Tree) = condOpt(tree) {
495495
case ValDef(_, _, tpt, _) if tpt.tpe == null => "recursive "+sym+" needs type"
496496
case DefDef(_, _, _, _, tpt, _) if tpt.tpe == null => List(cyclicAdjective(sym), sym, "needs result type") mkString " "
497+
case Import(expr, selectors) =>
498+
( "encountered unrecoverable cycle resolving import." +
499+
"\nNote: this is often due in part to a class depending on a definition nested within its companion." +
500+
"\nIf applicable, you may wish to try moving some members into another object."
501+
)
497502
}
498503

499504
/** Report a type error.
@@ -508,7 +513,11 @@ trait TypeDiagnostics {
508513

509514
ex match {
510515
case CyclicReference(sym, info: TypeCompleter) =>
511-
contextError(ex.pos, cyclicReferenceMessage(sym, info.tree) getOrElse ex.getMessage())
516+
val pos = info.tree match {
517+
case Import(expr, _) => expr.pos
518+
case _ => ex.pos
519+
}
520+
contextError(pos, cyclicReferenceMessage(sym, info.tree) getOrElse ex.getMessage())
512521

513522
if (sym == ObjectClass)
514523
throw new FatalError("cannot redefine root "+sym)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cyclics-import.scala:1: error: encountered unrecoverable cycle resolving import.
2+
Note: this is often due in part to a class depending on a definition nested within its companion.
3+
If applicable, you may wish to try moving some members into another object.
4+
import User.UserStatus._
5+
^
6+
cyclics-import.scala:12: error: not found: type Value
7+
type UserStatus = Value
8+
^
9+
cyclics-import.scala:14: error: not found: value Value
10+
val Active = Value("1")
11+
^
12+
cyclics-import.scala:15: error: not found: value Value
13+
val Disabled = Value("2")
14+
^
15+
four errors found
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import User.UserStatus._
2+
3+
class User {
4+
var id: Int = 0
5+
var email: String = null
6+
var password: String = null
7+
var userStatus: UserStatus = null
8+
}
9+
10+
object User {
11+
object UserStatus extends Enumeration {
12+
type UserStatus = Value
13+
14+
val Active = Value("1")
15+
val Disabled = Value("2")
16+
}
17+
}

test/files/neg/t1845.check

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
t1845.scala:9: error: illegal cyclic reference involving value <import>
2-
val lexical = new StdLexical
3-
^
1+
t1845.scala:6: error: encountered unrecoverable cycle resolving import.
2+
Note: this is often due in part to a class depending on a definition nested within its companion.
3+
If applicable, you may wish to try moving some members into another object.
4+
import lexical._
5+
^
46
one error found

test/files/neg/t2870.check

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
t2870.scala:1: error: not found: type Jar (similar: Jars)
22
class Jars(jar: Jar)
33
^
4-
t2870.scala:6: error: illegal cyclic reference involving value <import>
5-
val scala = fromClasspathString(javaClassPath)
6-
^
4+
t2870.scala:4: error: encountered unrecoverable cycle resolving import.
5+
Note: this is often due in part to a class depending on a definition nested within its companion.
6+
If applicable, you may wish to try moving some members into another object.
7+
import scala.util.Properties.javaClassPath
8+
^
79
two errors found

0 commit comments

Comments
 (0)