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..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 @@ -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,77 @@ 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(); +} + +// `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(); + } +} 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 5f6e4789e..5bfb1833b 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 @@ -455,13 +455,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, + }) } } @@ -1324,7 +1332,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() { @@ -1336,6 +1346,31 @@ 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()); + // 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(), + LuaUnionType::from_vec(vec![named("A"), named("B"), named("B")]) + ); + } + #[test] fn test_union_iterator() { assert_eq!(LuaUnionType::from_vec(vec![]).iter().next(), None);