From bdadba095e31c8e538a65ae8abb1d0160a5764ba Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Thu, 29 Oct 2026 23:42:55 +0100 Subject: [PATCH 1/2] fix(infer): a short callback against a variadic tail no longer panics `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 Claude-Session: https://claude.ai/code/session_018NxUUPGy5eT16LumQqytKE --- .../src/check/test/param_type_check_test.rs | 16 ++++++++++++++++ .../src/semantic_model/infer/vm.rs | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs index 80a1b6714..ec27bc5e4 100644 --- a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs @@ -3630,4 +3630,20 @@ mod test { diagnostics ); } + + /// A callback with fewer params than the expected function's fixed prefix, where a variadic + /// follows that prefix. The variadic's slice of the callback's params started past their end + /// and panicked. + #[test] + fn test_short_callback_against_variadic_tail_does_not_panic() { + let mut ws = VirtualWorkspace::new(); + assert!(ws.has_no_diagnostic( + DiagnosticCode::ParamTypeMismatch, + r#" + ---@param f fun(x: integer, ...: string) + local function g(f) end + g(function() end) + "# + )); + } } diff --git a/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs b/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs index c8935c833..950f5ce98 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs @@ -3924,7 +3924,11 @@ pub(crate) fn unify_call_bindings( .saturating_sub(trailing) }; let consume = consume.min(arg_params.len().saturating_sub(arg_index)); - let remaining: Vec = arg_params[arg_index..arg_index + consume] + // `arg_index` can be past the end: the ordinary params before this + // slot advance it even when the callback declares fewer params. + let remaining: Vec = arg_params + .get(arg_index..arg_index + consume) + .unwrap_or_default() .iter() .enumerate() .map(|(offset, (_, ty))| { From 2a060988e895f1863b6764d617f0903e4bfb59c8 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sun, 8 Nov 2026 20:39:24 +0100 Subject: [PATCH 2/2] fix(infer): clamp the variadic slot's start instead of a fallible slice 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 Claude-Session: https://claude.ai/code/session_018NxUUPGy5eT16LumQqytKE --- .../src/check/test/param_type_check_test.rs | 19 +++++++++++++++++++ .../src/semantic_model/infer/vm.rs | 17 +++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs index ec27bc5e4..40c7ad814 100644 --- a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs @@ -3646,4 +3646,23 @@ mod test { "# )); } + + /// Around the panic's boundary: a callback with exactly the fixed prefix, and one whose extra + /// params the variadic takes. + #[test] + fn test_callback_at_and_past_variadic_prefix() { + let mut ws = VirtualWorkspace::new(); + assert!(ws.has_no_diagnostic( + DiagnosticCode::ParamTypeMismatch, + r#" + ---@param f fun(x: integer, ...: string) + local function g(f) end + g(function(x) end) + g(function(x, a, b) end) + ---@param a string + ---@param b string + g(function(x, a, b) end) + "# + )); + } } diff --git a/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs b/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs index 950f5ce98..4914cf8d7 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs @@ -3923,18 +3923,17 @@ pub(crate) fn unify_call_bindings( .saturating_sub(arg_index) .saturating_sub(trailing) }; - let consume = consume.min(arg_params.len().saturating_sub(arg_index)); // `arg_index` can be past the end: the ordinary params before this // slot advance it even when the callback declares fewer params. - let remaining: Vec = arg_params - .get(arg_index..arg_index + consume) - .unwrap_or_default() + let start = arg_index.min(arg_params.len()); + let consume = consume.min(arg_params.len() - start); + let remaining: Vec = arg_params[start..start + consume] .iter() .enumerate() .map(|(offset, (_, ty))| { let ty = ty.clone().unwrap_or(Unknown); if arg_fun.is_variadic() - && arg_index + offset + 1 == arg_params.len() + && start + offset + 1 == arg_params.len() && !matches!(ty, Variadic(_)) { Variadic(Rc::new(VariadicType::Repeat(ty))) @@ -3948,11 +3947,9 @@ pub(crate) fn unify_call_bindings( && let TplRef(tpl) = base && !bindings.contains_key(&tpl.get_tpl_id()) && !remaining.is_empty() - && arg_params[arg_index..arg_index + consume] - .iter() - .any(|(_, ty)| { - ty.as_ref().is_some_and(|t| !matches!(t, Unknown | Any)) - }) + && arg_params[start..start + consume].iter().any(|(_, ty)| { + ty.as_ref().is_some_and(|t| !matches!(t, Unknown | Any)) + }) && !remaining.iter().any(|t| { matches!( t,