diff --git a/compiler/src/dotty/tools/dotc/core/Names.scala b/compiler/src/dotty/tools/dotc/core/Names.scala index acfbd411a0ce..97356c76dde8 100644 --- a/compiler/src/dotty/tools/dotc/core/Names.scala +++ b/compiler/src/dotty/tools/dotc/core/Names.scala @@ -342,8 +342,7 @@ object Names { override def encode: SimpleName = { val dontEncode = - length >= 3 && - head == '<' && last == '>' && isIdentifierStart(apply(1)) + this == StdNames.nme.CONSTRUCTOR || this == StdNames.nme.STATIC_CONSTRUCTOR if (dontEncode) this else NameTransformer.encode(this) } diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 8f0a57dc2fc8..320ce53b8713 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -978,6 +978,10 @@ object Parsers { def ident(): TermName = if (isIdent) { val name = in.name + if name == nme.CONSTRUCTOR || name == nme.STATIC_CONSTRUCTOR then + report.error( + i"""Illegal backquoted identifier: `` and `` are forbidden""", + in.sourcePos()) in.nextToken() name } diff --git a/tests/neg/i12729.scala b/tests/neg/i12729.scala new file mode 100644 index 000000000000..50f8f06d9bce --- /dev/null +++ b/tests/neg/i12729.scala @@ -0,0 +1,7 @@ +class Test(i: Int): + val `` = "init" // error: Illegal backquoted identifier: `` and `` are forbidden + val `` = "clinit" // error: Illegal backquoted identifier: `` and `` are forbidden + class ``: // error: Illegal backquoted identifier: `` and `` are forbidden + def ``(in: String) = ??? // error: Illegal backquoted identifier: `` and `` are forbidden + class ``: // error: Illegal backquoted identifier: `` and `` are forbidden + def ``(in: String) = ??? // error: Illegal backquoted identifier: `` and `` are forbidden diff --git a/tests/run/i12729.scala b/tests/run/i12729.scala new file mode 100644 index 000000000000..d7683fe72a05 --- /dev/null +++ b/tests/run/i12729.scala @@ -0,0 +1,3 @@ +object Test: + val `` = "hello!" + def main(args: Array[String]): Unit = println(``)