Skip to content

fix: FunctionDef does not emit a return for void functions#252

Merged
xmnlab merged 7 commits intoarxlang:mainfrom
Fizza-Mukhtar:fix/void-function-missing-terminator
Mar 31, 2026
Merged

fix: FunctionDef does not emit a return for void functions#252
xmnlab merged 7 commits intoarxlang:mainfrom
Fizza-Mukhtar:fix/void-function-missing-terminator

Conversation

@Fizza-Mukhtar
Copy link
Copy Markdown
Contributor

Pull Request description

Fixes #244

When visit(FunctionDef) finishes walking the function body, if no
FunctionReturn node was encountered the current basic block is left
without a terminator. llvmlite raises an AssertionError during module
verification for void functions, and silently produces malformed IR for
non-void functions that fall off the end.

Fix: after self.visit(node.body), check is_terminated on the
current block:

  • void return type → emit ret_void()
  • non-void return type → emit ret <zero> as a safe fallback

How to test these changes

pytest tests/test_void_function.py -v

Pull Request checklists

This PR is a:

  • bug-fix
  • new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more complexity.
  • New and old tests passed locally.

@Jaskirat-s7
Copy link
Copy Markdown
Contributor

Hey @yuvimittal , I tested the changes locally — all 5 tests pass .
the is_terminated check after self.visit(node.body) is a very clean approach, and it is good that it handles both void (ret_void()) and non-void (ret 0 fallback) cases. The test for explicit return not producing a duplicate terminator is a nice edge case to cover too.

hey @Fizza-Mukhtar ,same as your other PR , the branch is a bit behind main so you'll probably need a rebase to avoid conflicts

@yuvimittal
Copy link
Copy Markdown
Member

@Fizza-Mukhtar , overall tests are good, just one comment

Comment thread tests/test_void_function.py Outdated
main_fn = astx.FunctionDef(prototype=main_proto, body=main_body)
module.block.append(main_fn)

check_result("build", builder, module)
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.

This does not guarantee correct IR, only that it didn’t crash.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the test to verify ret void is present in the emitted IR
instead of only checking that the build did not crash. Also rebased
onto latest main.

@Fizza-Mukhtar Fizza-Mukhtar force-pushed the fix/void-function-missing-terminator branch from ddd0ca0 to 42ca66d Compare March 28, 2026 06:34
@xmnlab xmnlab force-pushed the fix/void-function-missing-terminator branch from 42ca66d to eecf504 Compare March 31, 2026 22:02
@xmnlab xmnlab merged commit d890f24 into arxlang:main Mar 31, 2026
13 checks passed
@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.

Fix: FunctionDef does not emit a return for void functions

4 participants