Skip to content
Open
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
Expand Up @@ -8,7 +8,7 @@ mod test {
use lsp_types::{Diagnostic, DiagnosticSeverity, NumberOrString};
use tokio_util::sync::CancellationToken;

use crate::check::test::{check_source, check_source_with_emmyrc};
use crate::check::test::{check_source, check_source_with_emmyrc, count_by_code};
use crate::{DiagnosticCode, Emmyrc, LuaType, VirtualWorkspace};

// 普通联合参数接受其中的具体类型, 保留返回信息并拒绝不属于联合的实参.
Expand Down Expand Up @@ -4735,4 +4735,28 @@ mod test {
"delay"
);
}

/// A closure argument whose own parameter is typed by the callee's `T`: binding `T` to a
/// type that mentions `T` grew each function pair, so the solver's queue never ran dry.
#[test]
fn test_generic_bound_to_a_type_that_mentions_it_terminates() {
let diagnostics = check_source(
r#"
local _ = {}
---@generic T : fun(...)
---@param fn T
_.curry = function(fn)
return function(...) end
end
---@generic T, V
---@param fn fun(...): T
_.converge = _.curry(function(fn, fns, val) end)
"#,
);
assert_eq!(
count_by_code(&diagnostics, DiagnosticCode::ParamTypeMismatch),
0,
"{diagnostics:?}"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
//! Leaf-type decisions reuse `type_check::is_compatible` (named types/alias/union etc.);
//! this module only makes the function structure iterative.

use std::collections::VecDeque;
use std::collections::{HashSet, VecDeque};

use crate::LuaFunctionType;
use crate::LuaType;
use crate::LuaTypeNode;
use crate::semantic_model::SemanticModel;
use crate::semantic_model::type_check::{function_param_pairs, is_compatible};

Expand Down Expand Up @@ -49,7 +50,15 @@ pub fn functions_compatible(
expected: &LuaFunctionType,
bindings: &TplBindings,
) -> bool {
// Solving substitutes again and again, so a binding that mentions a bound generic would grow
// each pair and `seen_pairs` would never see one twice. Dropped, its generic stays open, also
// for a self-mention that would have stopped growing: this can lose a catch, not add one.
let mut bindings = bindings.clone();
let bound: HashSet<_> = bindings.keys().cloned().collect();
bindings.retain(|_, value| {
!value
.any_type(|ty| matches!(ty, LuaType::TplRef(tpl) if bound.contains(&tpl.get_tpl_id())))
});
let mut queue = VecDeque::new();
queue.push_back(Constraint::FnPair(ActualExpected {
actual: actual.clone(),
Expand All @@ -59,7 +68,7 @@ pub fn functions_compatible(
// Cycle guard: expand each function pair at most once.
// Type keys are immutable; their OnceCell only caches a stable hash.
#[allow(clippy::mutable_key_type)]
let mut seen_pairs = std::collections::HashSet::new();
let mut seen_pairs = HashSet::new();
while let Some(constraint) = queue.pop_front() {
match constraint {
Constraint::FnPair(pair) => {
Expand Down
Loading