Skip to content

Commit 894d779

Browse files
committed
Improve access error messages + test case
1 parent cad41d2 commit 894d779

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,30 @@ trait TypeAssigner {
195195
TryDynamicCallType
196196
} else {
197197
val alts = tpe.denot.alternatives.map(_.symbol).filter(_.exists)
198+
var packageAccess = false
198199
val what = alts match {
199200
case Nil =>
200-
name.toString
201+
i"$name cannot be accessed as a member of $pre"
201202
case sym :: Nil =>
202-
if (sym.owner == pre.typeSymbol) sym.show else sym.showLocated
203+
if (sym.owner.is(Package)) {
204+
packageAccess = true
205+
i"${sym.showLocated} cannot be accessed"
206+
}
207+
else {
208+
val symStr = if (sym.owner == pre.typeSymbol) sym.show else sym.showLocated
209+
i"$symStr cannot be accessed as a member of $pre"
210+
}
203211
case _ =>
204-
em"none of the overloaded alternatives named $name"
212+
em"none of the overloaded alternatives named $name can be accessed as members of $pre"
205213
}
206-
val where = if (ctx.owner.exists) s" from ${ctx.owner.enclosingClass}" else ""
214+
val where =
215+
if (!ctx.owner.exists) ""
216+
else if (packageAccess) i" from nested ${ctx.owner.enclosingPackageClass}"
217+
else i" from ${ctx.owner.enclosingClass}"
207218
val whyNot = new StringBuffer
208219
alts foreach (_.isAccessibleFrom(pre, superAccess, whyNot))
209220
if (tpe.isError) tpe
210-
else errorType(ex"$what cannot be accessed as a member of $pre$where.$whyNot", pos)
221+
else errorType(ex"$what$where.$whyNot", pos)
211222
}
212223
}
213224
else ctx.makePackageObjPrefixExplicit(tpe withDenot d)

tests/neg/i3339.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package outer {
2+
private class A
3+
4+
package inner {
5+
object Test {
6+
def main(args: Array[String]): Unit = {
7+
println(new A) // error: cannot access
8+
}
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)