Skip to content

fix(infer): a short callback against a variadic tail no longer panics - #1373

Merged
CppCXY merged 2 commits into
EmmyLuaLs:mainfrom
mareklangiewicz:fix-short-callback-variadic-panic
Nov 9, 2026
Merged

CppCXY merged 2 commits into
EmmyLuaLs:mainfrom
mareklangiewicz:fix-short-callback-variadic-panic

Conversation

@mareklangiewicz

@mareklangiewicz mareklangiewicz commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Three lines of Lua panic emmylua_check and emmylua_ls:

---@param f fun(x: integer, ...: string)
local function g(f) end
g(function() end)
panicked at crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs:3927:65:
range start index 2 out of range for slice of length 1

unify_call_bindings slices the callback's params for the variadic slot with arg_params[arg_index..arg_index + consume]. The ordinary params before that slot advance arg_index even when the callback declares fewer params, so the slice starts past the end. consume was clamped; the start was not. Now one clamped start serves both slices of that slot.

-let consume = consume.min(arg_params.len().saturating_sub(arg_index));
-let remaining: Vec<LuaType> = arg_params[arg_index..arg_index + consume]
+let start = arg_index.min(arg_params.len());
+let consume = consume.min(arg_params.len() - start);
+let remaining: Vec<LuaType> = arg_params[start..start + consume]

A short callback then binds nothing through the variadic slot, as when consume is 0.

Evidence

  • Before: test_short_callback_against_variadic_tail_does_not_panic panics at vm.rs:3927:65. On b889a2d1, emmylua_check on neovim's runtime/ panics there too.
  • After: the test passes, and so does test_callback_at_and_past_variadic_prefix (exactly the fixed prefix, and extra params taken by the variadic); emmylua_code_analysis lib tests: 1708 passed, 0 failed. The runtime run gets past it. It then hits the stack overflow fixed by fix(types): a union with a repeated member equals itself #1365.

Merge Danger

Door: two-way

Blast Radius: inference

Only calls whose callback is shorter than the fixed params before a variadic change, and those panicked before.


Written with help from Claude Code.

`unify_call_bindings` sliced `arg_params[arg_index..arg_index + consume]`.
The ordinary params before a variadic slot advance `arg_index` even when
the callback declares fewer params, so `---@PARAM f fun(x: integer, ...: string)`
with `g(function() end)` sliced `[1..1]` of an empty list and panicked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NxUUPGy5eT16LumQqytKE

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

Summary

The change fixes a real panic (slice index out of bounds) when a callback declares fewer parameters than the fixed prefix of an expected function type that has a variadic tail. The fix is minimal and correct, and a regression test is included.

Issues & Suggestions

1. unwrap_or_default() masks the root cause (minor)

Using .get(...).unwrap_or_default() silently returns an empty slice when arg_index is out of bounds. While this prevents the panic, it also hides the fact that arg_index can exceed arg_params.len(). Consider either:

  • Adding a debug assertion or comment documenting the invariant, or
  • Clamping arg_index earlier (e.g., arg_index.min(arg_params.len())) so the slice expression is always valid and the intent is clearer.

The added comment helps, but the clamping approach makes the invariant explicit and avoids relying on unwrap_or_default as a safety net.

2. arg_index + consume can still overflow in theory (very minor)

arg_index + consume is computed before the .get() call. Since consume is already clamped via saturating_sub, and arg_index is bounded by iteration, this is unlikely in practice, but using arg_index.saturating_add(consume) would be consistent with the surrounding saturating_sub style.

3. Test coverage could be broader (suggestion)

The test only covers the "fewer params than fixed prefix" case with a variadic tail. Consider adding cases where:

  • The callback has exactly the fixed prefix count (boundary).
  • The callback has more params than the fixed prefix (variadic consumption).
  • There is no variadic tail (to confirm the fix doesn't alter existing behavior).

4. Test name/comment (nit)

The doc comment is clear and useful. No change required.

Verdict

The fix is correct and low-risk. The main recommendation is to make the out-of-bounds condition explicit (clamp arg_index) rather than relying on unwrap_or_default, which improves readability and guards against future regressions.

One clamped `start` serves both slices, so the bound is explicit and
`start + consume` can't pass the end. Tests at the boundary: a callback
with exactly the fixed prefix, and one whose extra params the variadic takes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NxUUPGy5eT16LumQqytKE
@mareklangiewicz

Copy link
Copy Markdown
Contributor Author

Thanks. Applied in the latest commit:

  1. Clamp instead of unwrap_or_default: there is now one start = arg_index.min(arg_params.len()), used by both slices of the slot. consume is clamped against len - start.
  2. arg_index + consume overflow: after the clamp, start + consume <= len by construction, so the sum can't pass the end or overflow. I left it as +.
  3. Boundary tests: added test_callback_at_and_past_variadic_prefix. It covers a callback with exactly the fixed prefix, and one whose extra params, typed or untyped, the variadic takes. The no-variadic case is already covered by the existing param tests, and they all pass (1708).

Written with help from Claude Code.

@mareklangiewicz
mareklangiewicz marked this pull request as ready for review November 8, 2026 19:46
@CppCXY
CppCXY merged commit 82cd516 into EmmyLuaLs:main Nov 9, 2026
16 checks passed
mareklangiewicz added a commit to mareklangiewicz/emmylua-analyzer-rust that referenced this pull request Nov 10, 2026
…emo, EmmyLuaLs#1373 short callback vs variadic tail) into marek-main-fixes

Both PRs are this fork's own fixes, merged upstream in their reviewed form.
- vm.rs: take upstream's clamped `start` (EmmyLuaLs#1373 review) over the fork's
  `.get(..).unwrap_or_default()`; same behavior for `arg_index` past the end.
- flow.rs: keep the fork's side; it already carries EmmyLuaLs#1372 (ported in d16b712)
  plus the `access` base and cast positions upstream lacks. Result = fork's file.
- assign_type_mismatch_test.rs: keep the fork's side; upstream's three tests
  are present, the fork's `test_member_write_target_ignores_guards` has one
  more case (a cast in the file).
- param_type_check_test.rs: keep the fork's side and add upstream's boundary
  test `test_callback_at_and_past_variadic_prefix`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NxUUNXDZoFif8q99ttrdb
mareklangiewicz added a commit to mareklangiewicz/emmylua-analyzer-rust that referenced this pull request Nov 10, 2026
Upstream's `test_callback_at_and_past_variadic_prefix` (EmmyLuaLs#1373) passes
upstream only because a `---@param` above a call statement is dropped
there. This fork applies it (1d0c649, found by first-parent bisect), so
the third case is a documented callback, and upstream itself rejects the
same callback passed by name (`g(cb)`): `...` may be empty, so `a: string`
can receive nil. Keep the undocumented cases (the EmmyLuaLs#1373 panic) and a
`string?` one as clean, and pin the two documented mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NxUUNXDZoFif8q99ttrdb
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.

2 participants