Skip to content

fix(codegen): use safe_pop for loop body lowering; use fadd/fsub for float unary inc and dec#264

Merged
xmnlab merged 8 commits intoarxlang:mainfrom
omsherikar:fix/while-stmt-empty-stack
Apr 1, 2026
Merged

fix(codegen): use safe_pop for loop body lowering; use fadd/fsub for float unary inc and dec#264
xmnlab merged 8 commits intoarxlang:mainfrom
omsherikar:fix/while-stmt-empty-stack

Conversation

@omsherikar
Copy link
Copy Markdown
Contributor

@omsherikar omsherikar commented Mar 23, 2026

  • Replace raw .pop() with safe_pop() in WhileStmt, ForCountLoopStmt, and ForRangeLoopStmt body handling
  • Remove broken if not body_val: return guard in WhileStmt that skipped branch-back on falsy values
  • Add regression test with a void-only loop body
    solves Fix: WhileStmt body pop crashes on empty result stack #241

Copilot AI review requested due to automatic review settings March 23, 2026 17:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses codegen crashes and incorrect control-flow in loop lowering by ensuring loop bodies don’t underflow result_stack and by removing a guard that incorrectly treated valid “falsy” LLVM values as missing results.

Changes:

  • Replace raw result_stack.pop() with safe_pop() for loop body result handling in WhileStmt, ForCountLoopStmt, and ForRangeLoopStmt.
  • Remove the if not body_val: return guard in WhileStmt so codegen doesn’t skip the back-edge on falsy values.
  • Add a regression test intended to cover loops whose body doesn’t leave a value on the stack.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/irx/builders/llvmliteir.py Makes loop codegen tolerant to empty body results by using safe_pop() and modernizes some type annotations.
tests/test_while.py Adds a new regression test aimed at preventing WhileStmt crashes when the loop body doesn’t produce a value.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/irx/builders/llvmliteir.py Outdated
Comment thread tests/test_while.py Outdated
@omsherikar
Copy link
Copy Markdown
Contributor Author

@yuvimittal I have made the changes can you have a look?

@Jaskirat-s7
Copy link
Copy Markdown
Contributor

Hey @omsherikar, i was going through your PR and i analyzed that the safe_pop change in all three loop types makes good sense, and good call removing the if not body_val: return guard ->that would also break loops when the body produced a falsy value like 0, not just on empty stacks.

but there were a couple of things I noticed:

first is that regression test only covers WhileStmt — since ForCountLoopStmt and ForRangeLoopStmt were also changed to use safe_pop,i think it would be good to add similar void-body tests for those two as well , what do you think and the Optional → X | None type hint changes are valid modernization but maybe unrelated to the bug fix — might be cleaner as a separate commit to keep the fix-focused diff small.

rest of the code change looks good overall 👍

@omsherikar omsherikar changed the title Fix/while stmt empty stack fix:while stmt empty stack by replacing with safe_pop() Mar 27, 2026
@omsherikar
Copy link
Copy Markdown
Contributor Author

Hii @yuvimittal can you review this pr please

Comment thread tests/test_while.py Outdated

self.visit(node.body)
_ = self.result_stack.pop()
safe_pop(self.result_stack)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i am sorry @omsherikar , i missed that these are the exact changes you made!

@omsherikar
Copy link
Copy Markdown
Contributor Author

hii @yuvimittal I have made the changes as discussed, resolved the merge conflicts . ready for review

@xmnlab xmnlab changed the title fix:while stmt empty stack by replacing with safe_pop() fix(codegen): use safe_pop for loop body lowering; use fadd/fsub for float unary inc and dec Apr 1, 2026
@xmnlab xmnlab merged commit 326a0db into arxlang:main Apr 1, 2026
13 checks passed
@xmnlab
Copy link
Copy Markdown
Contributor

xmnlab commented Apr 1, 2026

thanks @omsherikar

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

🎉 This PR is included in version 1.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants