From 9fd67e95f60947b1a6538df17c9fa808e95f404a Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Fri, 6 Nov 2026 03:00:20 +0100 Subject: [PATCH 1/2] fix(semantic): a binding that mentions a bound generic does not enter the function solver Solving substitutes repeatedly, so such a binding grew each function pair and the `seen_pairs` guard never saw one twice: mason.nvim's functional/function.lua took about 11 GB. Dropped, the generic stays open, which only ever accepts more. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUURL8eZPTs95zm47G6C --- .../src/check/test/param_type_check_test.rs | 19 +++++++++++++++++++ .../semantic_model/infer/function_solver.rs | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs index 3621bc7bc..b5316849e 100644 --- a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs @@ -3561,4 +3561,23 @@ mod test { diagnostics ); } + + /// 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() { + 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) + "#, + ); + } } diff --git a/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs b/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs index 8ee7b81dd..159f72934 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs @@ -19,6 +19,7 @@ use std::collections::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}; @@ -49,7 +50,14 @@ 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. let mut bindings = bindings.clone(); + let bound: Vec<_> = 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(), From 31054fb89274f6b4f86d30d4aa953a1f001909a6 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Fri, 6 Nov 2026 14:07:15 +0100 Subject: [PATCH 2/2] refactor(semantic): a set for the bound generics, and a test that checks the result From the bot review. The comment now names the trade-off: a dropped binding can lose a catch, not add one. The test also asserts the call stays free of `param-type-mismatch`. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUPAKyiJnPD57aeKcPG --- .../src/check/test/param_type_check_test.rs | 9 +++++++-- .../src/semantic_model/infer/function_solver.rs | 9 +++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs index b5316849e..b76a84f66 100644 --- a/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/param_type_check_test.rs @@ -5,7 +5,7 @@ mod test { use lsp_types::{Diagnostic, 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, VirtualWorkspace}; #[test] @@ -3566,7 +3566,7 @@ mod test { /// 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() { - check_source( + let diagnostics = check_source( r#" local _ = {} ---@generic T : fun(...) @@ -3579,5 +3579,10 @@ mod test { _.converge = _.curry(function(fn, fns, val) end) "#, ); + assert_eq!( + count_by_code(&diagnostics, DiagnosticCode::ParamTypeMismatch), + 0, + "{diagnostics:?}" + ); } } diff --git a/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs b/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs index 159f72934..1da3980d8 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/infer/function_solver.rs @@ -15,7 +15,7 @@ //! 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; @@ -51,9 +51,10 @@ pub fn functions_compatible( 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. + // 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: Vec<_> = bindings.keys().cloned().collect(); + 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()))) @@ -67,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) => {