Repository navigation
fix(check): a record literal's first mismatching field is the same every run - #1437
mareklangiewicz wants to merge 5 commits into
Conversation
…ery 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUSXZWf4hp8CP3dKREA
There was a problem hiding this comment.
Code Review
Overview
The change fixes a non-deterministic diagnostic ordering bug by sorting record fields before checking them. The test verifies the fix. Overall the change is sound, but there are a few points worth addressing.
Issues & Suggestions
1. Test comment overstates the guarantee (test file)
"so 16 runs would almost surely see two of three lines if the order leaked"
This is a probabilistic argument. With 3 candidate lines, the probability that 16 independent runs all land on the same line is (1/3)^15 ≈ 7e-8 per target — fine in practice, but any claim of "almost surely" is informal. More importantly, if the underlying ordering is deterministic-but-wrong (e.g., insertion order changed), the test won't catch it — it only asserts the result is stable, then separately asserts [10]. The [10] assertion is the real check; consider making that the primary claim and treating stability as secondary, or document the flakiness risk explicitly. 16 iterations × 2 targets is also somewhat slow for a unit test; 8 would give (1/3)^7 ≈ 4.6e-4, still acceptable, though 16 is defensible if runtime permits.
2. Fragile line-number detection in test
.map(|d| source[..usize::from(d.range.start())].matches('\n').count())This assumes range.start() is a valid char/byte boundary into source and that it's an index into source. Slicing by a diagnostic offset is only safe if the offset is guaranteed to be at a UTF-8 boundary; if it isn't, this panics. It's fine here (ASCII source), but a helper like line_index_of(source, offset) would be more robust and self-documenting.
3. Magic number [10]
The expected line is derived from the raw string's indentation layout, which is easy to break by reformatting the literals. A comment mapping line 10 to "the first entry (a / x = a)" would save future maintainers a debugging cycle. Also note format! preserves the leading newline, so line 10 corresponds to the second element of the table — verify this is actually the entry the record's fields put first, and not an accident of layout.
4. Sorting by key requires Ord — check key semantics
sorted.sort_unstable_by_key(|(key, _)| *key);*key is copied out of the tuple; confirm the key type is Copy/Ord and that the resulting ascending order matches the intended "fields put first" convention. If the record definition order (source order) is the intended authority, sorting by key value may not be correct — e.g., for {[3]: ..., [1]: ...} declared in that order, sorting by key yields [1] first, which may differ from what the user expects to be reported. Confirm the deterministic order chosen is the definition order the comment claims, not merely a stable-but-arbitrary key order.
5. Performance: allocates a Vec on every check
let mut sorted: Vec<_> = fields.get_fields().iter().collect();This allocates for every table-literal-vs-record check. For small records it's negligible, but this is on a hot path for type checking. If get_fields() returns a map, consider documenting why sorting is necessary (done) or, if the field count is typically small, this is fine — just be aware. Alternative: iterate in key order via a BTreeMap-backed field storage if that's feasible, avoiding the per-check sort entirely.
6. Comment clarity
The comment "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." is good. Minor: "a hash map's order changes per map" — consider clarifying whether it's per-map-instance or per-call; if the map's iteration order is stable for a given instance, the run-to-run difference is actually a per-instance seed, which the wording should reflect.
Summary
- ✅ Fix is correct in spirit and the test guards against regression.
⚠️ Verify sorted key order matches intended reporting convention (point 4).⚠️ Make test line-number/slice handling more robust (points 2, 3).- ℹ️ Comment on the probabilistic nature of the test's stability check (point 1) and the per-check allocation (point 5).
The keys were derived from the target string (`ends_with('P')`).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NxUUSXZWf4hp8CP3dKREA
…rder is by key Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUR6W6YykYLUWo9RTMQ
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUR6W6YykYLUWo9RTMQ
|
Thanks for the review. I took items 1, 3, 4 and 6 in a7b738f (comments only), and merged main 48ef52a in. 1, 3. The test comment now says which entry is pinned: the lowest key, Not taken:
Re-measured on main 48ef52a (the body is updated): on the five plugin trees where main reports different lines from run to run, 4 runs of main vary on 16 lines in 4 files, and 4 runs of this branch vary on 5 lines in 1 file (blink.cmp |
Conflict in assign_type_mismatch_test.rs only: both sides appended a test at the end of the module; kept both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUQZHt6a5njk8trY5BZ
Summary
A table literal with several entries that don't match the target record gets one
assign-type-mismatch, placed on the first failing entry.check_table_recordwalks the record's fields inHashMaporder, andcheck_allstops at the first failure. foldhash seeds every map anew, so which entry counts as "first" changes from run to run. On an unchanged tree, the same binary reports the mismatch on a different line, with a different reason, each run. This happens for---@type {…}objects and for---@classtargets alike, sinceclass_fieldsgoes through the same function.The fields are now walked in
LuaMemberKeyorder: integer keys ascending, then names. It's still one diagnostic per literal, now at the lowest key every time. This addresses one source of the run-to-run variation in #1091, but doesn't fix it all (see the end).Evidence
emmylua_check30 times on this file. main reports line 6 ×11, 7 ×8, 8 ×11, while this PR reports line 6 ×30. The same file with---@class P(fieldsx,y,z) andx = a, y = a, z = aspreads over all three lines the same way on main. With only one mismatching entry, main is stable (control).Before:
test_record_mismatch_reports_the_same_entry_every_run(new,assign_type_mismatch_test.rs) checks both target shapes in 16 fresh databases each. On main it fails with lines{10, 11, 12}reported where{10}was expected. After: it passes. Allemmylua_code_analysistests pass (1986 on main 48ef52a), as docargo fmtandclippy.Plugin corpus (
emmylua_check, empty config, the 54 Neovim plugin trees that finish on main): main 48ef52a → this branch, one run each, 57302 → 57302 hits. All 6 changed lines areassign-type-mismatchin diffview.nvim and gitsigns.nvim, each a mismatch that moved to a neighbouring entry of the same literal. Four more runs of each build on the five trees where main varies (blink.cmp, diffview.nvim, gitsigns.nvim, nui.nvim, nvim-lspconfig):The one file that still varies on this branch is blink.cmp
lua/blink/cmp/config/sources.lua:76–81. There the inferred type of the literal itself changes between runs (opts: blink.cmp.SourceConfigvsopts: blink.cmp.CompleteFuncOpts). That's a separate source, not touched here.Merge Danger
Door: two-way
Iteration order in one loop. Reverting it restores the current behaviour.
Blast Radius: diagnostics
Which entry of a literal carries the one
assign-type-mismatch, and its reason text. Whether a literal is reported at all doesn't change. The sort costs one smallVecper record check.🤖 Generated with Claude Code
https://claude.ai/code/session_018NxUUSXZWf4hp8CP3dKREA