From 4f031f18cb0e34246fe43cc89f687ed655e9d63b Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Thu, 19 Nov 2026 11:19:45 +0100 Subject: [PATCH 1/3] fix(check): a record literal's first mismatching field is the same every run check_table_record walked the target record's fields in HashMap order, and check_all stops at the first failure, which add_mismatch reports at that field's entry. foldhash seeds each map anew, so a literal with several mismatching entries was reported on a different line from run to run, for object and class targets alike. Walk the fields in key order instead. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUSXZWf4hp8CP3dKREA --- .../check/test/assign_type_mismatch_test.rs | 49 +++++++++++++++++++ .../semantic_model/type_check/simple_type.rs | 6 ++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs b/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs index 5d54a12dd..3baefeed3 100644 --- a/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs @@ -3336,4 +3336,53 @@ return t } } } + + // A literal with several mismatching entries is reported once, at the entry the record's + // fields put first. The fields were walked in hash order, which differs per map, so the + // reported line moved between runs of the same binary. Each `check_source` builds a fresh + // database, so 16 runs would almost surely see two of three lines if the order leaked. + #[test] + fn test_record_mismatch_reports_the_same_entry_every_run() { + for target in [ + "---@type { [1]: integer, [2]: integer, [3]: integer }", + "---@type P", + ] { + let keys: [&str; 3] = if target.ends_with('P') { + ["x = ", "y = ", "z = "] + } else { + ["", "", ""] + }; + let source = format!( + r#" + ---@class P + ---@field x integer + ---@field y integer + ---@field z integer + + ---@param a number + local function f(a) + {target} + local t = {{ + {}a, + {}a, + {}a, + }} + return t + end + return f + "#, + keys[0], keys[1], keys[2] + ); + let lines: std::collections::BTreeSet = (0..16) + .flat_map(|_| { + check_source(&source) + .into_iter() + .filter(|d| d.code == DiagnosticCode::AssignTypeMismatch) + .map(|d| source[..usize::from(d.range.start())].matches('\n').count()) + .collect::>() + }) + .collect(); + assert_eq!(lines, [10].into(), "{target}"); + } + } } diff --git a/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs b/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs index ac4bb5987..2186c10b0 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs @@ -256,7 +256,11 @@ fn check_table_record( strict_literals: context.strict_literals || !table.is_constructor(), ..context.clone() }; - let field_checks = check_all(fields.get_fields().iter().map(|(key, expected)| { + // Key order, not hash order: the first failing field is the one reported, and a hash map's + // order changes per map, so the reported line moved between runs. + let mut sorted: Vec<_> = fields.get_fields().iter().collect(); + sorted.sort_unstable_by_key(|(key, _)| *key); + let field_checks = check_all(sorted.into_iter().map(|(key, expected)| { let actual = table.member_value(key)?; check_constructor_value(&mut context, table_file, actual, expected) })); From 64f5ff0d143e62f0df298a69e1e2daef126b1b05 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Thu, 19 Nov 2026 12:37:58 +0100 Subject: [PATCH 2/3] test: the record-order test iterates (target, keys) pairs The keys were derived from the target string (`ends_with('P')`). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUSXZWf4hp8CP3dKREA --- .../src/check/test/assign_type_mismatch_test.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs b/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs index 3baefeed3..78e859db2 100644 --- a/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs @@ -3343,15 +3343,13 @@ return t // database, so 16 runs would almost surely see two of three lines if the order leaked. #[test] fn test_record_mismatch_reports_the_same_entry_every_run() { - for target in [ - "---@type { [1]: integer, [2]: integer, [3]: integer }", - "---@type P", + for (target, keys) in [ + ( + "---@type { [1]: integer, [2]: integer, [3]: integer }", + ["", "", ""], + ), + ("---@type P", ["x = ", "y = ", "z = "]), ] { - let keys: [&str; 3] = if target.ends_with('P') { - ["x = ", "y = ", "z = "] - } else { - ["", "", ""] - }; let source = format!( r#" ---@class P From a7b738f3e302d630362f1f62854b7010e45bd373 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Mon, 23 Nov 2026 17:23:39 +0100 Subject: [PATCH 3/3] docs(check): say which entry the record-order test pins and why the order is by key Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018NxUUR6W6YykYLUWo9RTMQ --- .../src/check/test/assign_type_mismatch_test.rs | 10 ++++++---- .../src/semantic_model/type_check/simple_type.rs | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs b/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs index 78e859db2..081af785c 100644 --- a/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/assign_type_mismatch_test.rs @@ -3337,10 +3337,12 @@ return t } } - // A literal with several mismatching entries is reported once, at the entry the record's - // fields put first. The fields were walked in hash order, which differs per map, so the - // reported line moved between runs of the same binary. Each `check_source` builds a fresh - // database, so 16 runs would almost surely see two of three lines if the order leaked. + // A literal with several mismatching entries is reported once, at the entry with the lowest + // key: `[1]` for the tuple-like target, `x` for the class. That is line 10 of `source` (the + // `format!` string starts with a newline), the first `a`. The fields used to be walked in + // hash order, which is seeded per map, so the reported line moved between runs of the same + // binary. The main check is `[10]`; the 16 fresh databases per target are the stability half: + // with the order leaking, each run picked one of the three lines. #[test] fn test_record_mismatch_reports_the_same_entry_every_run() { for (target, keys) in [ diff --git a/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs b/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs index 2186c10b0..e87875a90 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/type_check/simple_type.rs @@ -256,8 +256,9 @@ fn check_table_record( strict_literals: context.strict_literals || !table.is_constructor(), ..context.clone() }; - // Key order, not hash order: the first failing field is the one reported, and a hash map's - // order changes per map, so the reported line moved between runs. + // Key order (integers ascending, then names), not hash order: the first failing field is the + // one reported, and each map's hash order is seeded anew, so the reported line moved between + // runs. The map keeps no definition order to use instead. let mut sorted: Vec<_> = fields.get_fields().iter().collect(); sorted.sort_unstable_by_key(|(key, _)| *key); let field_checks = check_all(sorted.into_iter().map(|(key, expected)| {