polyglot-compiler / JLang

JLang: Ahead-of-time compilation of Java programs to LLVM
http://polyglot-compiler.github.io/JLang/
Other
287 stars 30 forks source link

Static Initialization bug for Final Variables #37

Closed dz333 closed 5 years ago

dz333 commented 5 years ago

The following code produces an NPE in JLang code when accessing s.length. If s is not final then this is an NPE in both JDK and JLang. However, making s final causes this code to succeed in JDK but still produces an NPE in JLang.

private static final int a = calculate(); private static final String s = "my string3"; private static int calculate() { return s.length();}

Specifically the issue is with "constant variables" see: jlr

This also applies to ints: private static final int a = calculate(); private static final int b = 3; private static int calculate() { return b;}

After this code, a is set to 0, not 3 in JLang, since b is not initialized at compile time properly.

dz333 commented 5 years ago

Fixed with commit: 340064afbd1d79abbb5207b758835fa521b4ede3