5

I was trying to fix some weird PDF Unicode remapping issues when I was stuck by this issue where adding an ENQ Unicode character to some part of the code prevents you from compiling the code and IDE will present you with what I think is a false alarm(error).

Consider this example of completely valid Kotlin code and let's call it Program A where unfortunately you can't see the actual characters here between apostrophes but believe me there's a character in there (if you copy this code into your IDE you can see those characters).

package yamin

fun main() {
    val foo = mutableListOf('')
    //val bar = mutableListOf('')
    println(foo)
}

However, you can see them in the picture.

Program A

But if you decide to compile this code somehow IDE fails to compile it and claims that there are 'Too many characters in a character literal' in line 4.

Program A false error

What is interesting is that the sheer presence of ENQ character even in a comment line prevents the IDE to compile this code and if you remove line 5, therefore, removing the presence of ENQ character then you can compile this code (let's call it Program B), observe:

Program B

Anyway if you decide to remove line 4 in Program A and uncomment line 5 then you can compile that code too (let's call it Program C).

enter image description here

So let's recap Program A is invalid and line 4 is the culprit of the invalidity but removing line 5 in that program which leaves us with Program B could be compiled successfully and this situation is repeated for Program C.

What am I missing here?

YaMiN
  • 3,194
  • 2
  • 12
  • 36
  • Do you literally have 'ESA' wrapped in single quotes? B/c I don't see that ever compiling for me. I believe character literals can only have more than one character if you begin with an escaping backslash. Otherwise, looks like something you could report to Kotlin Issue Tracker at https://youtrack.jetbrains.com/issues/KT. – Trevor Oct 25 '21 at 01:41
  • @Trevor I already did. https://youtrack.jetbrains.com/issue/IDEA-281164 – YaMiN Oct 25 '21 at 14:21

1 Answers1

3

It looks like a bug in Kotlin compiler, because the error happens even if the code is compiled with command-line: kotlinc enq.kt -include-runtime -d enq.jar

Also, this simple program gives different results on the playground (correct):

enter image description here

and locally (incorrect):

enter image description here

As a workaround you may use Unicode escape sequence syntax (at least for ENQ symbol):

enter image description here

Playground does the same thing when passing this code to the server, so the compiler gets sanitized code: enter image description here

  • You're right I can work my around it by escaping it but I don't see why original code is not compilable in IntelliJ IDEA. I don't know it's a bug in Kotlin plugin, IDE itself or even something I do is fundamentally wrong. – YaMiN Oct 25 '21 at 16:08
  • 1
    Updated my answer, to clarify that it's a bug in a Kotlin compiler (and explaining why it works on playground) – Михаил Нафталь Oct 25 '21 at 17:12