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 1db84e862..335633632 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 @@ -3598,6 +3598,55 @@ return t } } + // 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 [ + ( + "---@type { [1]: integer, [2]: integer, [3]: integer }", + ["", "", ""], + ), + ("---@type P", ["x = ", "y = ", "z = "]), + ] { + 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}"); + } + } + /// `integer? - 1` is `integer`: a nil operand raises, so a result is always an integer. /// The nil is `need-check-nil`'s to report. #[test] 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 7c3ce7692..603da63dc 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 @@ -257,7 +257,12 @@ 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 (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)| { let actual = table.member_value(key)?; check_constructor_value(&mut context, table_file, actual, expected) }));