Skip to content

Fix throw_undef_if_not error#216

Merged
penelopeysm merged 6 commits intomainfrom
py/fix-204
Feb 22, 2026
Merged

Fix throw_undef_if_not error#216
penelopeysm merged 6 commits intomainfrom
py/fix-204

Conversation

@penelopeysm
Copy link
Copy Markdown
Member

@penelopeysm penelopeysm commented Feb 22, 2026

Closes #204, and adds a regression test.

The problem is that the SSA registers inside Expr(:throw_undef_if_not, ...) were not being referenced/dereferenced in the same way that other SSA registers are. The fix itself is a one-liner, it just applies the existing SSA register handling mechanism to throw_undef_if_not.

In the original reproducer in #204, this is the original function's IR.

2 1%1 = (_2 === 1)::Bool
  └──      goto #3 if not %1
  2nothing::Nothing
5 3%4 = φ (#2 => true, #1 => false)::Bool$(Expr(:throw_undef_if_not, :y, :(%4)))::Any
  │        invoke Main.produce(3::Int64)::Int64
6$(Expr(:throw_undef_if_not, :y, :(%4)))::Any
  │        invoke Main.produce(3::Int64)::Int64
7 └──      return 1

The first throw_undef_if_not is fortuitously handled because it references %4, which is derived from a PhiNode, and the PhiNode handling ensures that the value of %4 is indeed dereferenced correctly.

The second throw_undef_if_not would be fine if it ended up in the same CFG block as the previous one. However, because there's a produce statement between the two, it ends up in a different block, which leads to the error seen in #204.

(Claude helped me work through the codebase, but the PR and fix is mine.)

@github-actions
Copy link
Copy Markdown

Libtask.jl documentation for PR #216 is available at:
https://TuringLang.github.io/Libtask.jl/previews/PR216/

@penelopeysm penelopeysm merged commit 9be93c0 into main Feb 22, 2026
22 checks passed
@penelopeysm penelopeysm deleted the py/fix-204 branch February 22, 2026 23:30
@yebai
Copy link
Copy Markdown
Member

yebai commented Feb 23, 2026

Nice improvement -- interesting to know that coding agents can help with understanding tricky code!

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.

Libtask doesn't gracefully handle sometimes-defined variables

2 participants