Use proper envs for Rust functions #7623
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #7588
We use a stack for memory management. When a function is called we push a new frame onto the stack (aka a root environment). For efficiency and perhaps to catch some bugs, when we call a Rust function (i.e., a std lib function), we don't push a new frame and set the frame 'pointer' to a dummy value. However, when we call map or similar higher-order functions, the Rust definition calls other functions. This should be OK because we'll push a fresh stack frame for the callee if it is a KCL function and if it is a Rust function, it doesn't need a stack frame. However, when we type check the function call we might need to look up names in memory for which we need a real stack frame.
To fix this issue I considered working around this by special-casing the Rust envs, but it increased complexity for an unproven optimisation (since most Rust calls are leaf functions we mostly only save a single stack frame which is pretty lightweight). Instead I removed the Rust function special case and now we do create a stack frame for all functions, Rust or KCL. This is a simplification and we can optimise if it's shown to be slow.