diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.scala index 6ea2c2b4f7f7..a9dcebd702f7 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.scala @@ -146,7 +146,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] { StableIdentPatternID, StaticFieldsShouldPrecedeNonStaticID, IllegalSuperAccessorID, - TraitParameterUsedAsParentPrefixID + TraitParameterUsedAsParentPrefixID, + UnknownNamedEnclosingClassOrObjectID def errorNumber = ordinal - 2 } diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index a523ee8ef4e1..608cd0c1584a 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -2378,4 +2378,18 @@ object messages { |than obtaining it from the parameters of ${cls.show}. |""".stripMargin } + + case class UnknownNamedEnclosingClassOrObject(name: TypeName)(implicit val ctx: Context) + extends Message(UnknownNamedEnclosingClassOrObjectID) { + val kind: String = "Reference" + val msg: String = + em"""no enclosing class or object is named '${hl(name.show)}'""" + val explanation: String = + ex""" + |The class or object named '${hl(name.show)}' was used as a visibility + |modifier, but could not be resolved. Make sure that + |'${hl(name.show)}' is not misspelled and has been imported into the + |current scope. + """.stripMargin + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index e185ce523e5f..b96cb384517d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -254,7 +254,7 @@ class Namer { typer: Typer => else { val cls = ctx.owner.enclosingClassNamed(name) if (!cls.exists) - ctx.error(s"no enclosing class or object is named $name", ctx.source.atSpan(span)) + ctx.error(UnknownNamedEnclosingClassOrObject(name), ctx.source.atSpan(span)) cls } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 01b53e5374db..b56a103468a1 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1657,4 +1657,19 @@ class ErrorMessagesTests extends ErrorMessagesTest { messages.head.msg ) } + + @Test def unknownNamedEnclosingClassOrObject() = + checkMessagesAfter(RefChecks.name) { + """ + |class TestObject { + | private[doesNotExist] def test: Int = 5 + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + assertMessageCount(1, messages) + val UnknownNamedEnclosingClassOrObject(name) :: Nil = messages + assertEquals("doesNotExist", name.show) + } }