Skip to content

Commit 30191db

Browse files
committed
Fix #12729: Don't encode <init> and <clinit> only.
In addition, reject `<init>` and `<clinit>` in the parser, so that users don't write them in source code.
1 parent 8c18a71 commit 30191db

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

compiler/src/dotty/tools/dotc/core/Names.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ object Names {
342342

343343
override def encode: SimpleName = {
344344
val dontEncode =
345-
length >= 3 &&
346-
head == '<' && last == '>' && isIdentifierStart(apply(1))
345+
this == StdNames.nme.CONSTRUCTOR || this == StdNames.nme.STATIC_CONSTRUCTOR
347346
if (dontEncode) this else NameTransformer.encode(this)
348347
}
349348

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@ object Parsers {
978978
def ident(): TermName =
979979
if (isIdent) {
980980
val name = in.name
981+
if name == nme.CONSTRUCTOR || name == nme.STATIC_CONSTRUCTOR then
982+
report.error(
983+
i"""Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden""",
984+
in.sourcePos())
981985
in.nextToken()
982986
name
983987
}

tests/neg/i12729.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Test(i: Int):
2+
val `<init>` = "init" // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
3+
val `<clinit>` = "clinit" // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
4+
class `<init>`: // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
5+
def `<init>`(in: String) = ??? // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
6+
class `<clinit>`: // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
7+
def `<clinit>`(in: String) = ??? // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden

tests/run/i12729.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test:
2+
val `<x>` = "hello!"
3+
def main(args: Array[String]): Unit = println(`<x>`)

0 commit comments

Comments
 (0)