Repository navigation
fix(infer): a short callback against a variadic tail no longer panics - #1373
Conversation
`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
There was a problem hiding this comment.
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_indexearlier (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
|
Thanks. Applied in the latest commit:
Written with help from Claude Code. |
…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
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
Summary
Three lines of Lua panic
emmylua_checkandemmylua_ls:unify_call_bindingsslices the callback's params for the variadic slot witharg_params[arg_index..arg_index + consume]. The ordinary params before that slot advancearg_indexeven when the callback declares fewer params, so the slice starts past the end.consumewas clamped; the start was not. Now one clampedstartserves both slices of that slot.A short callback then binds nothing through the variadic slot, as when
consumeis 0.Evidence
test_short_callback_against_variadic_tail_does_not_panicpanics atvm.rs:3927:65. Onb889a2d1,emmylua_checkon neovim'sruntime/panics there too.test_callback_at_and_past_variadic_prefix(exactly the fixed prefix, and extra params taken by the variadic);emmylua_code_analysislib 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.