eclipse-archived / ceylon

The Ceylon compiler, language module, and command line tools
http://ceylon-lang.org
Apache License 2.0
399 stars 62 forks source link

unsound cyclic initialization of value constructors #6822

Open jvasileff opened 7 years ago

jvasileff commented 7 years ago

The following prints <null> on the JVM:

shared void run() {
    print(X.x.y.x); // JVM: <null>
}
class X {
    shared Y y;
    shared new x {
        y = Y.y;
    }
}
class Y {
    shared X x;
    shared new y {
        x = X.x;
    }
}

4474 addressed a specific, detectable case of cyclic initialization of value constructors, but for the example above, it seems a runtime check will be necessary.

I suggest that the rules in 8.2.10. Initialization of toplevel references be generalized to cover value constructors, particular with respect to throwing an InitializationError when cycles occur.

Note that I'm advocating for the same for statics in https://github.com/ceylon/ceylon/issues/6821#issuecomment-266244728 and https://github.com/ceylon/ceylon/issues/6720.

jvasileff commented 7 years ago

I think this is only an issue for value constructors of toplevel classes, at least once https://github.com/ceylon/ceylon/issues/6728 is resolved.