Skip to content

Conversation

@AnsonYeung
Copy link

Previously, variables inside a loop body are treated the same as that of the function body, which leads to bad semantics when a function captures a local variable defined within a loop body.

As mentioned in #293 (comment), this PR lowers while loop into tail recursive function and rely on the scoping of the function for the correct semantics. This is done during lowering as we already know the location of inserting Continue statements and we just need to convert these into tail calls.

runtime.LoopEnd is introduced to handle the case when a return statement occur inside the loop body. This value is returned when the loop exits without early return and is tested to determine whether to return from the outer function.

@LPTK
Copy link
Contributor

LPTK commented Nov 10, 2025

Nice! Please first resolve the conflicts and then request my review.

Copy link
Contributor

@LPTK LPTK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, but let's not claim victory too fast.

case Label(lbl, loop, bod, rst) =>
scope.allocateName(lbl)

// [fixme:0] TODO check scope and allocate local variables here (see: https://github.com/hkust-taco/mlscript/pull/293#issuecomment-2792229849)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually fixed, as while loops aren't always rewritten yet...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the latest commit I've added this back, but it shouldn't be an issue after loop rewriting. If a loop does not contain any local function, loop local variables can be treated as function locals since there is no capturing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right, but it's better to keep the doc/tests in sync with what's actually implemented at the moment.

Copy link
Author

@AnsonYeung AnsonYeung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed the comments.

case Label(lbl, loop, bod, rst) =>
scope.allocateName(lbl)

// [fixme:0] TODO check scope and allocate local variables here (see: https://github.com/hkust-taco/mlscript/pull/293#issuecomment-2792229849)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the latest commit I've added this back, but it shouldn't be an issue after loop rewriting. If a loop does not contain any local function, loop local variables can be treated as function locals since there is no capturing.

Copy link
Contributor

@LPTK LPTK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

//│ lambda$here = runtime.safeCall(lambda2(foo$capture6));
//│ return lambda$here
//│ } else {
//│ foo$capture6.tmp$capture$1 = runtime.Unit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed this crazy capture of a temporary variable that just stores the unit value. It's pretty crazy. I wonder why Unit is bound to a temporary variable in the first place.

@LPTK LPTK merged commit 2fd0f16 into hkust-taco:hkmc2 Nov 11, 2025
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants