Skip to content

Fix #12729: Don't encode <init> and <clinit> only. #12901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<init>` and `<clinit>` are forbidden""",
in.sourcePos())
in.nextToken()
name
}
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i12729.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Test(i: Int):
val `<init>` = "init" // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
Copy link
Member

@smarter smarter Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also test that class `<init>` and def `<init>` are rejected

val `<clinit>` = "clinit" // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
class `<init>`: // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
def `<init>`(in: String) = ??? // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
class `<clinit>`: // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
def `<clinit>`(in: String) = ??? // error: Illegal backquoted identifier: `<init>` and `<clinit>` are forbidden
3 changes: 3 additions & 0 deletions tests/run/i12729.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test:
val `<x>` = "hello!"
def main(args: Array[String]): Unit = println(`<x>`)