Skip to content

Fix bug that caused error in type inference when returning from if #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/kirin/dialects/scf/absint.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ def _infer_if_else_cond(
if isinstance(body_term, func.Return):
frame.worklist.append(interp.Successor(body_block, frame.get(stmt.cond)))
return
return interp_.frame_call_region(frame, stmt, body, frame.get(stmt.cond))

with interp_.new_frame(stmt, has_parent_access=True) as body_frame:
ret = interp_.frame_call_region(
body_frame, stmt, body, frame.get(stmt.cond)
)
frame.entries.update(body_frame.entries)
return ret
19 changes: 17 additions & 2 deletions test/analysis/dataflow/typeinfer/test_inter_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def foo(x: int):
return x - 1.0


@basic(typeinfer=True)
@basic(typeinfer=True, no_raise=False)
def main(x: int):
return foo(x)


@basic(typeinfer=True)
@basic(typeinfer=True, no_raise=False)
def moo(x):
return foo(x)

Expand All @@ -28,3 +28,18 @@ def test_inter_method_infer():
assert foo.arg_types[0] == types.Int
assert foo.inferred is False
assert foo.return_type is types.Any


def test_infer_if_return():
from kirin.prelude import structural

@structural(typeinfer=True, fold=True, no_raise=False)
def test(b: bool):
if b:
return False
else:
b = not b

return b

test.print()
Loading