Skip to content
Closed
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
@@ -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};

Expand Down Expand Up @@ -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<T>
local Tree = {}
---@generic T
---@param f Holder<T>
local function take(f, ...)
end
function Tree:start(range)
---@type fun(): table<integer, TSTree>, 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
Loading