Repository navigation
fix(check): duplicate-set-field ignores a nil field of a required module - #1355
mareklangiewicz wants to merge 3 commits into
Conversation
`duplicate-set-field` grouped members by owner and key. A member set through a local root is owned by that root (the path below it is dropped), so `function M.A:init()` and `function M.B:init()` share the owner `M` and key `init`, and every subclass's override of a method in one module was reported. Members are now grouped also by the access path of the table they are set on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUSkPvigJ1DPiNdnmbG
The cross-file `duplicate-set-field` check reported `reg.a.to = function() end` whenever the
required module had any member named `to` with a value, so a field initialized as
`{ to = nil }` (a slot meant to be filled by the caller) was reported as a reimplemented
method. The module's member now has to be function-like, checked against the module's own
syntax tree.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NxUUSkPvigJ1DPiNdnmbG
There was a problem hiding this comment.
Code Review: duplicate_field.rs changes
Summary
The change improves duplicate-field detection by (1) grouping members by the access path of the table they're set on, so M.A:init and M.B:init are no longer flagged, and (2) using the correct syntax tree when checking whether a member is function-like (fixing cross-file checks). Overall the direction is sound, but there are a few concerns.
Issues & Recommendations
1. set_table_path relies on token_at_offset + ancestor walk — fragile and O(n) per member
let token = root.token_at_offset(key_range.start()).right_biased()?;
let index_expr = token.parent_ancestors().find_map(LuaIndexExpr::cast)?;- This performs a tree lookup for every member, and
find_mapover ancestors can pick up an outerLuaIndexExprrather than the immediate one. The subsequentget_index_key()?.get_range()? != key_rangeguard mitigates this, but it's a subtle correctness dependency. - Recommendation: Prefer resolving the member's syntax node directly (e.g. via
member.value_syntax/ the member's own syntax id) and walking up from the knownLuaIndexExpr, rather than searching from a token offset. If the offset approach is kept, add a comment explaining whyright_biasedand the range check are needed.
2. set_table_path returns None for non-NameExpr/IndexExpr prefixes — silently merges unrelated groups
match index_expr.get_prefix_expr()? {
LuaExpr::NameExpr(name) => name.get_access_path(),
LuaExpr::IndexExpr(prefix) => prefix.get_access_path(),
_ => None,
}- When the prefix is e.g. a call expression (
getTable().A:init), the path isNone, so all such members collapse into the same(owner, implicit_self, None)bucket. That could reintroduce false positives or, worse, false negatives across unrelated tables. - Recommendation: Either fall back to a syntax-id-based discriminator (e.g.
Some(format!("<expr:{:?}>", prefix.get_syntax_id()))) so distinct dynamic prefixes stay distinct, or document thatNoneintentionally groups them and confirm that's desired.
3. is_function_like now takes tree: Option<&LuaSyntaxTree> — callers must pass the right tree
- In
check_cross_file_member_assign,module_treeis correctly fetched viasyntax_tree_of(module_file). Good. - In the main loop,
treeis the current file's tree — correct forfacts.membersof the current file. - Risk: The function silently returns
falsewhentreeisNoneor the node can't be found, which changes behavior in the cross-file path (previouslyvalue_syntax.is_some()was the gate). A member withvalue_syntaxset but a non-closure value (e.g.to = someFunc) will now be treated as not function-like, whereas before it passed theis_some()gate. Verify this is intended — the comment says "holds no function to reimplement", but a non-closure function reference is still a function. - Recommendation: Consider keeping a fallback: if the syntax node can't be resolved, fall back to the semantic type check (
member.decl_type/is_functionon the resolved type) rather than returningfalse.
4. is_function_like semantics changed for the cross-file gate
.any(|member| is_function_like(semantic_model, module_tree, member))- Previously: any member with a value syntax. Now: only closure-valued members. This is a behavioral change beyond the stated "nil field" fix. A module field assigned
to = handler(a name reference to a function) will no longer trigger the cross-file duplicate check. - Recommendation: Confirm with a test that
reg.luacontaining{ a = { to = handler } }still behaves as expected, or broadenis_function_liketo also accept resolved function types.
5. HashMap key now includes Option<String> — allocation per member
set_table_pathallocates aStringfor every member. For large files this is measurable overhead in a checker.- Recommendation: Consider interning or using a cheaper key (e.g.
Vec<LuaSyntaxId>of the prefix chain, or a hash of the path) if profiling shows this matters. At minimum, note the tradeoff.
6. Minor: root is Option<LuaSyntaxNode> held across the loop
let root = tree.map(|tree| tree.get_red_root());
if let Some(root) = &root { ... }
...
let table_path = root.as_ref().and_then(|root| set_table_path(root, member));- Fine, but
set_table_pathtakes&LuaSyntaxNodewhileget_red_root()returns an owned node; ensure the borrow doesn't outlivetree. It doesn't here, but the pattern is easy to break later. - Recommendation: Add a brief comment that
rootmust outlive the loop becauseset_table_pathborrows it.
7. Test coverage gaps
- New tests cover the happy paths well. Missing:
- Dynamic prefix case (
getTable().A:initvsgetTable().B:init) — see issue #2. - Cross-file case where the module field is a non-closure function reference (see issue #4).
- A case where the same nested table is reached via different syntactic paths (e.g.
M.AvsM["A"]) — should these be considered the same table? Currentlyget_access_pathlikely differs, so they'd be treated as distinct. Add a test to pin the intended behavior.
- Dynamic prefix case (
Verdict
The core fix is reasonable and the tests demonstrate the intended behavior. The main risks are (a) the None-path fallback silently merging unrelated dynamic-prefix tables, and (b) the semantic change in is_function_like for the cross-file gate. Address those two before merging; the rest are polish.
|
Thanks for the review. I checked the main concerns against the branch:
I checked the last two with throwaway tests on this branch. I can add them as regression tests if you want them pinned. Written with help from Claude Code. |
Upstream 76c776b (resolve members by full access path) groups members by their exact receiver path, which covers this branch's nested-target half: test_same_key_on_different_nested_tables_is_not_duplicate passes on main's checker. So main's grouping is kept and this branch's set_table_path is dropped. Kept: the nil-field half. A module member counts as a method to reimplement only if it is function-like (`{ to = nil }` is not), in both of main's cross-file checks (same receiver path and the original definition); is_function_like takes the declaring file's tree. test_setting_a_nil_field_of_a_required_module_is_not_duplicate fails on main's checker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUQZHt6a5njk8trY5BZ
Summary
Filling a
nilfield of a required module from another file is reported as reimplementing it:The cross-file check counted any module member with a value,
nilincluded. The member now has to be function-like (a closure, or typed as a function), checked against the module's own syntax tree:This PR first also fixed the same key on different tables (
function M.A:init()andfunction M.B:init()). Main now covers that with 76c776b (members grouped by full receiver path), so that half is dropped and its test passes on main without this change.Evidence
test_setting_a_nil_field_of_a_required_module_is_not_duplicatefails.test_same_key_on_different_nested_tables_is_not_duplicatepasses.After: both pass, and so do the 30
duplicatetests. The corpus numbers in the earlier version of this description were measured with both halves, so I removed them.Merge Danger
Door: two-way
Blast Radius: diagnostics
Only
duplicate-set-field's cross-file case changes, and it only gets quieter: assigning a function to a module member that holds a non-function value (nil, a number, a table) is no longer reported.🤖 Generated with Claude Code