jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Typechecker: Enum member functions cannot see other enums' members that are declared afterwards

Open greytdepression opened this issue 2 years ago • 1 comments

The following code

enum FooBar {
    A

    function make_foo() -> Foo {
        return Foo::Bar
    }
}

enum Foo {
    Bar
}

function main() {
    let b = FooBar::make_foo()
}

throws compile error

Error: variable 'Bar' not found
-----
 6  |     function make_foo() -> Foo { 
 7  |         return Foo::Bar 
                     ^- variable 'Bar' not found
 8  |     } 
-----

On the other hand, if we replace enum FooBar with struct FooBar the code does compile. To fix this we should adapt the code for parsing enum member functions to work similarly to how struct member functions are parsed :^)

greytdepression avatar Jun 17 '22 12:06 greytdepression

I played around with it some more and this problem is not unique to enums. One can provoke the same problem with structs:

struct FooBar {
    a: i64

    function make_foo() -> Foo {
        return Foo(bar: 0)
    }
}

struct Foo {
    b: i64
}

The reason it works when we mix structs and enums seems to be that enum function declarations get resolved before struct ones do. So the bug seems to be in the logic of how predeclaration is handled.

greytdepression avatar Jun 17 '22 16:06 greytdepression

fixed as of 1ea81c6

lanmonster avatar Sep 01 '22 16:09 lanmonster