Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3630,4 +3630,39 @@ 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)
"#
));
}

/// 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)
"#
));
}
}
17 changes: 9 additions & 8 deletions crates/emmylua_code_analysis/src/semantic_model/infer/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3923,14 +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));
let remaining: Vec<LuaType> = 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 start = arg_index.min(arg_params.len());
let consume = consume.min(arg_params.len() - start);
let remaining: Vec<LuaType> = 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)))
Expand All @@ -3944,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,
Expand Down
Loading