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

instance / static dependency granularity #483

Open
gewang opened this issue Dec 12, 2024 · 0 comments
Open

instance / static dependency granularity #483

gewang opened this issue Dec 12, 2024 · 0 comments
Assignees

Comments

@gewang
Copy link
Member

gewang commented Dec 12, 2024

given that static initialization is handled "out of band", there are subtle dependency relationships that currently aren't being fully upheld (currently the compiler will detect dependency errors more aggressively than is needed):

class Foo
{
    // non-static (pre-ctor) code that calls a static function
    bar();

    // static data (needed by static function)
    5 => static float foo;
    // static function
    fun static float bar() { return foo; }
}
<<< Foo.foo >>>;

This currently fails compilation with the dependency error (but is actually valid code, due to static initialization happening at compile-time)

c.ck:4:5: error: calling 'bar()' at this point skips initialization of a needed variable:
[4]     bar();
        ^
c.ck:7:23: error: ...(note: this skipped variable initialization is needed by 'fun float Foo.bar()')
[7]     5 => static float foo;
                          ^
c.ck:9:37: error: ...(note: this is where the variable is used within 'fun float Foo.bar()' or its subsidiaries)
[9]     fun static float bar() { return foo; }
                                        ^
c.ck: ...(hint: try calling 'bar()' after the variable initialization)

however, there is more complexity to consider in addressing the above -- while a static function cannot directly access instance data, it can implicitly depend on instance data, for example through the use of new:

public class Bar
{
    // instance data
    int var;
    // a static function that returns a new instance of Bar -- this is valid btw
    fun static Bar make() { return new Bar(); }
}

Things get extra complicated with inheritance, as seen in unit test error-depend-class-extend.ck

class Bar
{
    // this implicitly uses Bar.name...
    // also would cause a infinite loop, but won't get that far
    BarFactory.make();
    // the var
    "i am bar" => string name;
}

class BarChild extends Bar
{
    "success" => name;
}

class BarFactory
{
    fun static BarChild make()
    {
        return new BarChild;
    }
}

// shouldn't quite get to here
BarFactory.make();
@gewang gewang self-assigned this Dec 12, 2024
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