From 5294c222877b05aa0bc60bf0e936465807f1d09b Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Thu, 5 Nov 2026 22:25:27 +0100 Subject: [PATCH 1/3] fix(types): a union with a repeated member equals itself LuaUnionMembers::eq collected one side into a HashSet and removed the other side's members from it, so a repeated member failed its second remove: `a == a` was false. unify_call_bindings takes `expanded_arg != arg` as progress, so such a union passed to a generic class param never settled and overflowed the stack. Count members instead, which matches the hash, a sum over members. An in-order compare answers first, so the usual equal case allocates nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUN2Yt6d9cgKQzcoC84 --- .../src/check/test/type_mismatch_test.rs | 37 ++++++++++++++++++- .../semantic_db/def/lua_type/types/complex.rs | 37 +++++++++++++++---- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs b/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs index d8d4b754f..f4722f922 100644 --- a/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs @@ -1,6 +1,7 @@ //! Tests for the param_type_check / return_type_mismatch checkers. -use crate::DiagnosticCode; +use crate::{DiagnosticCode, VirtualWorkspace}; +use tokio_util::sync::CancellationToken; use super::{check_source, count_by_code}; @@ -210,3 +211,37 @@ fn test_alias_recursive_no_crash() { // Recursive alias cannot be decided: just ensure no crash (diagnostic count is not important). let _ = diagnostics; } + +// `parse` reads as a union with a repeated member. That used to compare unequal to itself, so +// expanding it as an arg to a generic class param never settled and overflowed the stack. +#[test] +fn test_union_arg_with_repeated_member_no_crash() { + let mut ws = VirtualWorkspace::new_with_init_std_lib(); + let file_id = ws.def( + r#" + ---@class Holder + local Tree = {} + ---@generic T + ---@param f Holder + local function take(f, ...) + end + function Tree:start(range) + ---@type fun(): table, boolean + local parse = coroutine.wrap(self._parse) + local function step() + if type(source) == 'number' then + if buf.changedtick ~= ct then + parse = coroutine.wrap(self._parse) + end + end + take(parse, self, range) + end + end + function Tree:_parse(range) + end + "#, + ); + ws.analysis + .diagnose_file(file_id, CancellationToken::new()) + .unwrap(); +} diff --git a/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs b/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs index afe5df1df..2bfe0654e 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs @@ -447,13 +447,21 @@ impl PartialEq for LuaUnionMembers { { return false; } - let mut types: HashSet<_> = self.types.iter().collect(); - for ty in &other.types { - if !types.remove(ty) { - return false; - } + if self.types == other.types { + return true; + } + // Counted, not a set: a member can repeat, and a union must equal itself. + let mut counts = HashMap::<_, usize>::with_capacity(self.types.len()); + for ty in &self.types { + *counts.entry(ty).or_default() += 1; } - types.is_empty() + other.types.iter().all(|ty| match counts.get_mut(ty) { + Some(count) if *count > 0 => { + *count -= 1; + true + } + _ => false, + }) } } @@ -1299,7 +1307,9 @@ mod tests { use rustc_hash::FxBuildHasher; - use crate::{LuaArrayType, LuaMultiLineUnion, LuaObjectType, LuaType, LuaUnionType}; + use crate::{ + LuaArrayType, LuaMultiLineUnion, LuaObjectType, LuaType, LuaTypeDeclId, LuaUnionType, + }; #[test] fn array_types_keep_structural_identity_after_hashing() { @@ -1311,6 +1321,19 @@ mod tests { assert_eq!(FxBuildHasher.hash_one(&equal), hash); } + #[test] + fn union_with_a_repeated_member_equals_itself() { + let named = |name| LuaType::Ref(LuaTypeDeclId::global(name)); + let union = || LuaUnionType::from_vec(vec![named("A"), named("A"), named("B")]); + assert!(matches!(union(), LuaUnionType::Multi(_))); + assert_eq!(union(), union()); + // Same length and members, other counts: their hashes differ, so they must not be equal. + assert_ne!( + union(), + LuaUnionType::from_vec(vec![named("A"), named("B"), named("B")]) + ); + } + #[test] fn test_union_iterator() { assert_eq!(LuaUnionType::from_vec(vec![]).iter().next(), None); From b25bc0d334acd2373b35d22d773b10b681416cce Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sun, 8 Nov 2026 20:19:41 +0100 Subject: [PATCH 2/3] test(types): a reordered union is equal with the same hash, a repeat changes the hash Answers the review: Eq counts members and the hash sums over them, so order does not count and repeats do, on both sides. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUPGy5eT16LumQqytKE --- .../src/semantic_db/def/lua_type/types/complex.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs b/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs index 2bfe0654e..6506513cb 100644 --- a/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs +++ b/crates/emmylua_code_analysis/src/semantic_db/def/lua_type/types/complex.rs @@ -1327,6 +1327,18 @@ mod tests { let union = || LuaUnionType::from_vec(vec![named("A"), named("A"), named("B")]); assert!(matches!(union(), LuaUnionType::Multi(_))); assert_eq!(union(), union()); + // Eq is a multiset compare, and the hash is a sum over members: order does not count, + // repeats do. + let reordered = LuaUnionType::from_vec(vec![named("B"), named("A"), named("A")]); + assert_eq!(union(), reordered); + assert_eq!( + FxBuildHasher.hash_one(union()), + FxBuildHasher.hash_one(&reordered) + ); + assert_ne!( + FxBuildHasher.hash_one(union()), + FxBuildHasher.hash_one(LuaUnionType::from_vec(vec![named("A"), named("B")])) + ); // Same length and members, other counts: their hashes differ, so they must not be equal. assert_ne!( union(), From 0ed22c1d2fcd6a330f555204cde585755b9292dc Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Mon, 23 Nov 2026 22:08:48 +0100 Subject: [PATCH 3/3] test(types): a return union with a repeated member no longer hangs the check Reduced from snacks.nvim: since the need-check-nil rewrite (#1418), checking the second file compares `table|X|X` against a closure signature. Without the fix that union is unequal to itself, so Comparisons::intern never finds its own key and the check never ends (main 48ef52a0: >90 s on the two files). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUURcPwXMM3gTzFZqTDa --- .../src/check/test/type_mismatch_test.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs b/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs index f4722f922..22a12208c 100644 --- a/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/type_mismatch_test.rs @@ -245,3 +245,43 @@ fn test_union_arg_with_repeated_member_no_crash() { .diagnose_file(file_id, CancellationToken::new()) .unwrap(); } + +// `layout` returns `table|X|X` (two casts to the undeclared `X`), a union with a repeated member. +// Checking the second file compares that return against the closure's signature; the union +// compared unequal to itself, so each read interned a fresh comparison and the check never ended. +#[test] +#[ntest::timeout(5000)] +fn test_return_union_with_repeated_member_no_hang() { + let mut ws = VirtualWorkspace::new(); + let file_ids = ws.def_files(vec![ + ( + "a.lua", + r#" + ---@class G + ---@field cfg Cfg + ---@class Cfg + local M = {} + function M.layout(opts) + local layout = M.resolve(opts) + ---@cast layout X + if not layout.layout then + ---@cast layout X|{preview: boolean} + if layout.preview == false then + end + end + return layout + end + ---@generic T + ---@return T + function M.resolve(v) + end + "#, + ), + ("b.lua", "local layout = G.cfg.layout()"), + ]); + for file_id in file_ids { + ws.analysis + .diagnose_file(file_id, CancellationToken::new()) + .unwrap(); + } +}