Skip to content
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

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

Open
mpsijm opened this issue Jun 23, 2020 · 0 comments
Open

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

mpsijm opened this issue Jun 23, 2020 · 0 comments

Comments

@mpsijm
Copy link
Contributor

mpsijm commented Jun 23, 2020

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
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant