jakt
jakt copied to clipboard
Typechecker: Enum member functions cannot see other enums' members that are declared afterwards
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 :^)
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.
fixed as of 1ea81c6