From 480f6ddba5080dfabe2fc1cfef0ec1e8b3e7885b Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Wed, 4 Nov 2026 13:42:59 +0100 Subject: [PATCH 1/3] fix(semantic_db): `local a, b = f()` gives each target its own slot `decl_type` typed every target of a multi-target assignment as the whole initializer, ignoring the decl's `multi_return_index`. The model's VM path picked the slot, but anything reading the database type got the full list: the model's fallback when the VM gave up on a call (`b` of `two('x')` was `(string,number)`), and member types built from such a local (`payload.output = output` expected `(any,string?,string)`). `TypeShell` gets `result_slot`, generalizing `first_result`; a target past a single value's only slot is `any`, as on the VM path. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUQmc3YFNEXrUcrVPxV --- .../src/semantic_db/query.rs | 13 +- .../src/semantic_db/types.rs | 55 ++++++++ .../infer/legacy_tests/multi_return.rs | 117 ++++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) diff --git a/crates/emmylua_code_analysis/src/semantic_db/query.rs b/crates/emmylua_code_analysis/src/semantic_db/query.rs index 78b4ab597..c89ddca90 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/query.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/query.rs @@ -1980,7 +1980,18 @@ pub(crate) fn decl_type( } if let Some(value_expr_syntax) = decl.value_expr_syntax { - let shell = expr_type_of(db, file, config, value_expr_syntax); + let mut shell = expr_type_of(db, file, config, value_expr_syntax); + // `local a, b = f()`: each target takes its own result slot, not the whole list. A + // target past a single value's only slot is an undeclared missing value: `any`, as the + // VM types it. + if let Some(index) = decl.multi_return_index { + let slot = shell.result_slot(index); + shell = if slot.is_unknown() && !shell.is_unknown() { + TypeShell::from_name("any") + } else { + slot + }; + } if !shell.is_unknown() { // Globals cannot carry generic parameters not instantiated inside the function body (the `a` in `function f(x) a = x end` is unknown outside). if matches!(decl.kind, DeclKind::Global) { diff --git a/crates/emmylua_code_analysis/src/semantic_db/types.rs b/crates/emmylua_code_analysis/src/semantic_db/types.rs index 9d6e5205b..ce3fa35a5 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/types.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/types.rs @@ -274,6 +274,61 @@ impl TypeShell { out } + /// The `index`th value of a result list (0-based): the `b` in `local a, b = f()` is + /// `f()`'s `result_slot(1)`. A plain value has only slot 0; past a finite list's end is nil. + pub(crate) fn result_slot(&self, index: usize) -> Self { + if index == 0 { + return self.first_result(); + } + let mut out = Self::unknown(); + for candidate in &self.candidates { + match candidate { + TypeCandidate::Variadic(base) => { + for candidate in &base.candidates { + match candidate { + TypeCandidate::Tuple(types) => { + if let Some(slot) = types.get(index) { + out.merge(slot); + } + } + _ => out.merge(&Self { + candidates: vec![candidate.clone()], + }), + } + } + } + TypeCandidate::Repeat(base) => out.merge(base), + TypeCandidate::Multi(slots) => { + let Some(last) = slots.len().checked_sub(1) else { + continue; + }; + if index < last { + out.merge(&slots[index].first_result()); + continue; + } + let tail = &slots[last]; + let expands = tail.candidates.iter().any(|candidate| { + matches!( + candidate, + TypeCandidate::Variadic(_) + | TypeCandidate::Repeat(_) + | TypeCandidate::Multi(_) + ) + }); + if expands { + out.merge(&tail.result_slot(index - last)); + } else if index == last { + out.merge(tail); + } else { + out.merge(&Self::from_primitive(PrimitiveType::Nil)); + } + } + _ => {} + } + } + out + } + /// Tuple candidate: `[number, string]`. pub fn from_tuple(types: Vec) -> Self { Self { diff --git a/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs b/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs index caba7adb8..0ebfe5d75 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs @@ -110,4 +110,121 @@ mod test { let expected = ws.ty("integer"); assert_eq!(e_ty, expected); } + + /// A call whose argument mismatches its parameter still assigns its returns slot by slot, + /// never the whole return list to every target. + #[test] + fn test_mismatched_call_assigns_return_slots() { + let mut ws = VirtualWorkspace::new(); + ws.def( + r#" + ---@param y number + ---@return string text + ---@return number count + local function two(y) return 'a', 1 end + + ---@param y number + ---@return string text + local function one(y) return 'a' end + + local a1, b1 = two('x') + local a2, b2 = one('x') + A1, B1, A2, B2 = a1, b1, a2, b2 + "#, + ); + + assert_eq!(ws.expr_ty("A1"), ws.ty("string")); + assert_eq!(ws.expr_ty("B1"), ws.ty("number")); + assert_eq!(ws.expr_ty("A2"), ws.ty("string")); + assert_eq!(ws.expr_ty("B2"), ws.ty("any")); + } + + /// A field assigned from a multi-return local has that local's slot type, not the whole list. + #[test] + fn test_field_from_multi_return_local_has_slot_type() { + let mut ws = VirtualWorkspace::new(); + ws.def( + r#" + ---@return any result + ---@return string? err + ---@return string output + local function captured() end + + local result, err, output = captured() + local payload = {} + payload.output = output + P = payload.output + "#, + ); + + assert_eq!(ws.expr_ty("P"), ws.ty("string")); + } + + /// The database's slot rule for every shape of result list, reached through mismatched calls. + #[test] + fn test_mismatched_call_slots_by_result_shape() { + let mut ws = VirtualWorkspace::new(); + ws.def( + r#" + ---@param y number + ---@return string a + ---@return number b + ---@return boolean c + local function three(y) end + + ---@param y number + ---@return string a + ---@return number b + local function two(y) end + + ---@param y number + ---@return string a + ---@return number ... + local function open_tail(y) end + + ---@param y number + ---@return number ... + local function repeated(y) end + + ---@param y number + ---@return number... + local function variadic(y) end + + ---@param y number + ---@return [string, number]... + local function tuple_variadic(y) end + + ---@param y number + ---@return [string, number] + local function tuple(y) end + + ---@param y number + ---@param ... number + local function forwards(y, ...) return true, ... end + + local _, middle, last = three('x') + local _, _, past_end = two('x') + local _, tail1, tail2 = open_tail('x') + local _, repeated2 = repeated('x') + local _, variadic2 = variadic('x') + local _, tuple_variadic2 = tuple_variadic('x') + local _, past_tuple = tuple('x') + local _, _, forwarded = forwards('x') + MIDDLE, LAST, PAST_END, TAIL1, TAIL2 = middle, last, past_end, tail1, tail2 + REPEATED2, VARIADIC2, TUPLE_VARIADIC2 = repeated2, variadic2, tuple_variadic2 + PAST_TUPLE, FORWARDED = past_tuple, forwarded + "#, + ); + + assert_eq!(ws.expr_ty("MIDDLE"), ws.ty("number")); + assert_eq!(ws.expr_ty("LAST"), ws.ty("boolean")); + assert_eq!(ws.expr_ty("PAST_END"), ws.ty("nil")); + assert_eq!(ws.expr_ty("TAIL1"), ws.ty("number")); + assert_eq!(ws.expr_ty("TAIL2"), ws.ty("number")); + assert_eq!(ws.expr_ty("REPEATED2"), ws.ty("number")); + assert_eq!(ws.expr_ty("VARIADIC2"), ws.ty("number")); + assert_eq!(ws.expr_ty("TUPLE_VARIADIC2"), ws.ty("number")); + assert_eq!(ws.expr_ty("PAST_TUPLE"), ws.ty("any")); + assert_eq!(ws.expr_ty("FORWARDED"), ws.ty("number")); + } } From 28ab07249f3befd8cadfe85f80c665ebff7168f0 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Mon, 23 Nov 2026 17:42:45 +0100 Subject: [PATCH 2/3] docs(types): say why a repeated result type ignores the slot index Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUR6W6YykYLUWo9RTMQ --- crates/emmylua_code_analysis/src/semantic_db/types.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/emmylua_code_analysis/src/semantic_db/types.rs b/crates/emmylua_code_analysis/src/semantic_db/types.rs index ce3fa35a5..968353154 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/types.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/types.rs @@ -297,6 +297,7 @@ impl TypeShell { } } } + // `T...` repeats one type, so every slot is `T`. TypeCandidate::Repeat(base) => out.merge(base), TypeCandidate::Multi(slots) => { let Some(last) = slots.len().checked_sub(1) else { From 97ce97cdc0bd86a03baca841150b7457d9ca984f Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Thu, 26 Nov 2026 18:18:44 +0100 Subject: [PATCH 3/3] fix(semantic_db): an unknown value in a result list stays unknown The `any` fallback is for a target past a single value's only slot, as its comment says, but it fired for any unknown slot of a result list too: `local x = f()` where f's first result is unknown became `any`. Merged with the same call's direct return that gave `(unknown|any)?`, which a `type(x) == 'table'` guard then narrows to member reads of `nil`: a false `param-type-mismatch expected any but found nil` in mini.icons (`icons.lua:482`, a recursive `MiniIcons.get`). The fallback now skips a shell that is a list of values. `TypeShell::is_value_list` names the predicate `result_slot` already used for an expanding tail. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUNhALyTAADUEzLeBUe --- .../src/semantic_db/query.rs | 2 +- .../src/semantic_db/types.rs | 20 ++++++++++--------- .../infer/legacy_tests/multi_return.rs | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/crates/emmylua_code_analysis/src/semantic_db/query.rs b/crates/emmylua_code_analysis/src/semantic_db/query.rs index 91e238bf0..9fcd59ffd 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/query.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/query.rs @@ -2102,7 +2102,7 @@ pub(crate) fn decl_type( // VM types it. if let Some(index) = decl.multi_return_index { let slot = shell.result_slot(index); - shell = if slot.is_unknown() && !shell.is_unknown() { + shell = if slot.is_unknown() && !shell.is_unknown() && !shell.is_value_list() { TypeShell::from_name("any") } else { slot diff --git a/crates/emmylua_code_analysis/src/semantic_db/types.rs b/crates/emmylua_code_analysis/src/semantic_db/types.rs index 968353154..fc618e528 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/types.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/types.rs @@ -274,6 +274,16 @@ impl TypeShell { out } + /// Whether some candidate is a list of values (a variadic or multi-return), not one value. + pub(crate) fn is_value_list(&self) -> bool { + self.candidates.iter().any(|candidate| { + matches!( + candidate, + TypeCandidate::Variadic(_) | TypeCandidate::Repeat(_) | TypeCandidate::Multi(_) + ) + }) + } + /// The `index`th value of a result list (0-based): the `b` in `local a, b = f()` is /// `f()`'s `result_slot(1)`. A plain value has only slot 0; past a finite list's end is nil. pub(crate) fn result_slot(&self, index: usize) -> Self { @@ -308,15 +318,7 @@ impl TypeShell { continue; } let tail = &slots[last]; - let expands = tail.candidates.iter().any(|candidate| { - matches!( - candidate, - TypeCandidate::Variadic(_) - | TypeCandidate::Repeat(_) - | TypeCandidate::Multi(_) - ) - }); - if expands { + if tail.is_value_list() { out.merge(&tail.result_slot(index - last)); } else if index == last { out.merge(tail); diff --git a/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs b/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs index 0ebfe5d75..bfd9e670e 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/infer/legacy_tests/multi_return.rs @@ -227,4 +227,24 @@ mod test { assert_eq!(ws.expr_ty("PAST_TUPLE"), ws.ty("any")); assert_eq!(ws.expr_ty("FORWARDED"), ws.ty("number")); } + + /// An unknown value inside a result list stays unknown: only a target past a single value's + /// only slot is `any`. As `any` it merged with the same call's direct return into + /// `(unknown|any)?` (mini.icons' recursive `get`). + #[test] + fn test_unknown_value_in_result_list_stays_unknown() { + let mut ws = VirtualWorkspace::new(); + ws.def( + r#" + local function first() return Undeclared[1], 2 end + local function second() return 1, Undeclared[1] end + local x = first() + local _, y = second() + X, Y = x, y + "#, + ); + + assert_eq!(ws.expr_ty("X"), ws.ty("unknown")); + assert_eq!(ws.expr_ty("Y"), ws.ty("unknown")); + } }