diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index 1877f68df87d..af682ef9398b 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -141,7 +141,8 @@ public enum ErrorMessageID { LazyStaticFieldID, StaticOverridingNonStaticMembersID, OverloadInRefinementID, - NoMatchingOverloadID + NoMatchingOverloadID, + StableIdentPatternID ; public int errorNumber() { diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 2b852e5cf6ca..9211bbcd33bd 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -2191,4 +2191,11 @@ object messages { val kind: String = "Type Mismatch" val explanation: String = "" } + case class StableIdentPattern(tree: untpd.Tree, pt: Type)(implicit val ctx: Context) + extends Message(StableIdentPatternID) { + override def kind: String = "Syntax" + override def msg: String = + hl"""Stable identifier required, but ${tree.show} found""" + override def explanation: String = "" + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 59b151e604e2..351adfb98839 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -414,7 +414,7 @@ class Typer extends Namer !pt.isInstanceOf[ApplyingProto] && !tree.tpe.isStable && !isWildcardArg(tree)) - ctx.error(s"stable identifier required, but ${tree.show} found", tree.pos) + ctx.error(StableIdentPattern(tree, pt), tree.pos) tree } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 4710e410c71e..037430b7cdd0 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1605,4 +1605,24 @@ class ErrorMessagesTests extends ErrorMessagesTest { message.msg ) } + + @Test def StableIdentifiers() = + checkMessagesAfter(FrontEnd.name) { + """ + | object Test { + | var x = 2 + | def test = 2 match { + | case `x` => x + 1 + | } + | } + """.stripMargin + }.expect { (_, messages) => + assertMessageCount(1, messages) + val message = messages.head + assertTrue(message.isInstanceOf[StableIdentPattern]) + assertEquals( + "Stable identifier required, but `x` found", + message.msg + ) + } }