Skip to content

fix(api): deflua/2 preserves function name for guarded heads - #344

Merged
davydog187 merged 1 commit into
mainfrom
fix/deflua2-guard-name
Jun 10, 2026
Merged

davydog187 merged 1 commit into
mainfrom
fix/deflua2-guard-name

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Bug

deflua/2 (a guarded head with no state arg) registered the function under the name :when instead of its real name, making it unreachable from Lua. For example:

deflua clamp(a) when is_integer(a) do
  a
end

would register clamp as :when in __lua_functions__/0, so calling clamp(...) from a Lua chunk raised an undefined-function error.

Root cause

In lib/lua/api.ex, the 2-arity deflua macro extracted the name with a single binding:

{name, _, _} = fa

For a guarded head the AST is {:when, _, [{name, _, _} | _]}, so the match against the outer tuple bound name to :when. deflua/3 (the state-arg variant) already handled this correctly with a case, which is why guarded heads with a state arg were never affected.

Fix

Use the same case-based extraction deflua/3 already uses to unwrap the :when node and reach the real function name, falling back to the plain head for non-guarded definitions. Name-only change; the def unquote(fa) body and @variadic handling are untouched.

Regression test

The existing "deflua functions can have guards" test only asserted the generated Elixir function worked (module.has_a_guard(1) == 1) and never checked the Lua registration name, which is exactly why the bug slipped through. A new test "guarded deflua heads register under their real name, not :when" asserts:

  • __lua_functions__/0 contains the real names (:has_a_guard, :with_state) and not :when
  • the guarded function is callable end-to-end from Lua via Lua.load_api + Lua.eval! (return has_a_guard(1))

Confirmed the new test fails against the unfixed macro (registers :when) and passes with the fix.

Validation

  • mix test test/lua/api_test.exs — 15 passed
  • mix test (full suite) — 2247 passed, 19 skipped, 1 excluded
  • mix format --check-formatted — clean

Closes #343

For a guarded head like `deflua clamp(a) when is_integer(a) do ... end`,
the AST passed to deflua/2 is `{:when, _, [{:clamp, _, _} | _]}`. The
single binding `{name, _, _} = fa` matched the outer tuple and bound
`name` to `:when`, so the function was registered in `@lua_function`
(and `__lua_functions__/0`) as `:when`, making it unreachable from Lua.

Use the same case-based extraction deflua/3 already uses to unwrap the
`:when` node and reach the real function name, falling back to the plain
head for non-guarded definitions. The existing "deflua functions can
have guards" test only asserted the generated Elixir function worked and
never checked the Lua registration name, which is why this slipped
through; a new test asserts `__lua_functions__/0` contains the real name
(not `:when`) and that the guarded function is callable from Lua.

Closes #343
@davydog187

Copy link
Copy Markdown
Contributor Author

🤖 Ultracode workflow review

Ran a 3-lens adversarial review (correctness · test-quality · regression-risk) over the diff. Overall: approve-with-nits.

Findings

nit · test-quality — test/lua/api_test.exs (the with_state clause in the new regression test)
The new test correctly pins the bug via the has_a_guard clauses + refute :when in names + the Lua.eval! call-through (confirmed: it fails on unfixed main with left: :when, right: [:when, :has_a_guard, :with_state]). However the with_state(a) when is_integer(a), state clause registers via deflua/3, which already had the correct name extraction before this PR — so assert :with_state in names always passed and doesn't guard the changed path. Harmless extra coverage, not a false-positive guard.

Optional: drop the with_state clause from this test to keep it tightly scoped to the deflua/2 path it protects. Leaving it is also fine.

What was verified

  • Fix mirrors the deflua/3 case-based name extraction exactly; deflua/2 injects no state, so the change is name-only (no prewalk needed).
  • Edge cases all handled: multiple guarded clauses, mixed guarded/unguarded clauses, compound guards, @variadic + guard, zero-arity guarded heads.
  • Regression test fails without the fix and passes with it. Full suite: 2247 passed, 19 skipped. mix format --check-formatted clean.

No blocking issues.

@davydog187
davydog187 merged commit 4ef8377 into main Jun 10, 2026
5 checks passed
@davydog187
davydog187 deleted the fix/deflua2-guard-name branch June 10, 2026 19:58
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.

deflua/2 (no-state) drops the function name for guarded heads, registering it as when

1 participant