ceylon / ceylon-spec

DEPRECATED
Apache License 2.0
108 stars 34 forks source link

initialization order problems merging header defaults with native implementations #1447

Open jvasileff opened 8 years ago

jvasileff commented 8 years ago

As observed in https://github.com/ceylon/ceylon-compiler/issues/2371, forward-reference checks can be circumvented. Issues with statement ordering with respect to declarations may exist as well.

This example produces surprising results where an immutable value seemingly changes, due to first being used in an uninitialized state:

shared native class C() {
    shared native Integer a = 1;
    shared native Integer b = 2;

    shared void printA() => print("a=``a``");
    shared void printB() => print("b=``b``");
}

shared native("jvm") class C() {
    shared native("jvm") Integer a = -b;
    printB();
}

shared void run() {
    value c = C(); // b=0
    c.printB();    // b=2
    c.printA();    // a=0
}

Similarly, a NullPointerException can occur:

shared native class C() {
    shared native Integer a = -1;
    shared native String b = "b";
}

shared native("jvm") class C() {
    shared native("jvm") Integer a = b.size;
    //java.lang.NullPointerException
    //  at ceylon.language.String.getSize(String.java:242)
}

ps - I realize this is not for 1.2.

quintesse commented 8 years ago

Thanks for opening an issue for this one so we won't forget it @jvasileff .

More discussion is still available in the original issue.