Kattis / problemtools

Tools to manage problem packages using the Kattis problem package format.
MIT License
101 stars 70 forks source link

Kotlin main class name is "mangled" by `kotlinc` #175

Open mpsijm opened 4 years ago

mpsijm commented 4 years ago

Follow-up of #124.

While preparing a recent contest, we found out that Kotlin does not only capitalize the first letter of the file, but also renames the main class to make it a valid Java class name. For example: test-test.kt is compiled to Test_test.class (note how the hyphen turns into an underscore). See the script below for a couple of weird filenames that I tested with.

Script used for testing names ```bash for name in test Test test-1 test-test Têśt 'test+&~$test' do echo echo $name echo 'val x = 42' > $name.kt kotlinc $name.kt ls *.class rm -f *.class *.kt done ``` ``` $ ./test.sh test TestKt.class Test TestKt.class test-1 Test_1Kt.class test-test Test_testKt.class Têśt error: source file or directory not found: T????t.kt ls: cannot access '*.class': No such file or directory test+&~$test Test____testKt.class ```

The problem with this "name mangling" is that Kotlin does document that this happens, just not how exactly. We could add in a string replace that turns every non-alphanumeric character into an underscore (_), but that only solves part of the problem: there might be characters that get a different treatment than just turning them into underscores, and files with non-ASCII characters apparently don't even compile (e.g. Têśt.kt).

One thing I did figure out: kotlinc dumps the name of the compiled class in META-INF/main.kotlin_module, so we could fetch it from there, but I'm not sure how that would fit in the problemtools pipeline.