Closed lenawanel closed 8 months ago
Oh yeah lol 😅
I'm not really sure on this one. At first I was thinking of just "running" the global every single time it is used within a comptime block, but that's probably not the behavior people would expect, especially if you reference a global multiple times.
This could probably be solved if the compiler could see what the most deeply nested comptime blocks are, before running those first. This wouldn't work if comptimes have the possibility of being recursive (which I'm not sure of, and which should probably be tested).
Either way this would require a rethink of how comptime's are currently being evaluated.
If a global contains a comptime that hasn't been evaluated yet (this issue), the global will now expand to the body of the global.
e.g.
a :: comptime 42;
foo :: () -> i32 {
a // assuming `a` hasn't been evaluated yet
}
now gets compiled to
foo :: () -> i32 {
if comptime_0.initialized {
comptime_0
} else {
comptime_0.initialized = true;
// calculate the value of the comptime
42
}
}
(not technically true but gives the idea)
This expansion only happens at compile-time--the final binary will have each comptime replaced with it's constant value just as before.
This change also ensures that every comptime block gets run once, and only once.
input:
backtrace: