diff --git a/crates/emmylua_code_analysis/src/semantic_db/query.rs b/crates/emmylua_code_analysis/src/semantic_db/query.rs index 6a4b55480..9fcd59ffd 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/query.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/query.rs @@ -2096,7 +2096,18 @@ pub(crate) fn decl_type( } if let Some(value_expr_syntax) = decl.value_expr_syntax { - let shell = expr_type_of(db, file_id, config, value_expr_syntax); + let mut shell = expr_type_of(db, file_id, 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() && !shell.is_value_list() { + 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..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,64 @@ 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 { + 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()], + }), + } + } + } + // `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 { + continue; + }; + if index < last { + out.merge(&slots[index].first_result()); + continue; + } + let tail = &slots[last]; + if tail.is_value_list() { + 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..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 @@ -110,4 +110,141 @@ 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")); + } + + /// 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")); + } }