Skip to content

Commit 169e1db

Browse files
Merge pull request #7457 from x3ro/lucas/unknown-named-class-or-object
Port 'unknown named enclosing class or object' error to new scheme
2 parents e2a4e64 + aae3e6a commit 169e1db

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
146146
StableIdentPatternID,
147147
StaticFieldsShouldPrecedeNonStaticID,
148148
IllegalSuperAccessorID,
149-
TraitParameterUsedAsParentPrefixID
149+
TraitParameterUsedAsParentPrefixID,
150+
UnknownNamedEnclosingClassOrObjectID
150151

151152
def errorNumber = ordinal - 2
152153
}

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

+14
Original file line numberDiff line numberDiff line change
@@ -2378,4 +2378,18 @@ object messages {
23782378
|than obtaining it from the parameters of ${cls.show}.
23792379
|""".stripMargin
23802380
}
2381+
2382+
case class UnknownNamedEnclosingClassOrObject(name: TypeName)(implicit val ctx: Context)
2383+
extends Message(UnknownNamedEnclosingClassOrObjectID) {
2384+
val kind: String = "Reference"
2385+
val msg: String =
2386+
em"""no enclosing class or object is named '${hl(name.show)}'"""
2387+
val explanation: String =
2388+
ex"""
2389+
|The class or object named '${hl(name.show)}' was used as a visibility
2390+
|modifier, but could not be resolved. Make sure that
2391+
|'${hl(name.show)}' is not misspelled and has been imported into the
2392+
|current scope.
2393+
""".stripMargin
2394+
}
23812395
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class Namer { typer: Typer =>
254254
else {
255255
val cls = ctx.owner.enclosingClassNamed(name)
256256
if (!cls.exists)
257-
ctx.error(s"no enclosing class or object is named $name", ctx.source.atSpan(span))
257+
ctx.error(UnknownNamedEnclosingClassOrObject(name), ctx.source.atSpan(span))
258258
cls
259259
}
260260

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

+15
Original file line numberDiff line numberDiff line change
@@ -1657,4 +1657,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
16571657
messages.head.msg
16581658
)
16591659
}
1660+
1661+
@Test def unknownNamedEnclosingClassOrObject() =
1662+
checkMessagesAfter(RefChecks.name) {
1663+
"""
1664+
|class TestObject {
1665+
| private[doesNotExist] def test: Int = 5
1666+
|}
1667+
""".stripMargin
1668+
}
1669+
.expect { (ictx, messages) =>
1670+
implicit val ctx: Context = ictx
1671+
assertMessageCount(1, messages)
1672+
val UnknownNamedEnclosingClassOrObject(name) :: Nil = messages
1673+
assertEquals("doesNotExist", name.show)
1674+
}
16601675
}

0 commit comments

Comments
 (0)