Skip to content

Commit 1dd1d24

Browse files
mlachkarsjrd
authored andcommitted
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 1dd1d24

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

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

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

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,13 @@ object Parsers {
996996

997997
private def makeIdent(tok: Token, offset: Offset, name: Name) = {
998998
val tree = Ident(name)
999-
if (tok == BACKQUOTED_IDENT) tree.pushAttachment(Backquoted, ())
999+
1000+
if tok == BACKQUOTED_IDENT then
1001+
if name == nme.CONSTRUCTOR || name == nme.STATIC_CONSTRUCTOR then
1002+
report.error(
1003+
i"""Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden""",
1004+
in.sourcePos())
1005+
tree.pushAttachment(Backquoted, ())
10001006

10011007
// Make sure that even trees with parsing errors have a offset that is within the offset
10021008
val errorOffset = offset min (in.lastOffset - 1)

tests/neg/i12729.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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

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)