Skip to content

fix(check): duplicate-set-field ignores a nil field of a required module - #1355

Open
mareklangiewicz wants to merge 3 commits into
EmmyLuaLs:mainfrom
mareklangiewicz:up-duplicate-set-field-nested-target
Open

mareklangiewicz wants to merge 3 commits into
EmmyLuaLs:mainfrom
mareklangiewicz:up-duplicate-set-field-nested-target

Conversation

@mareklangiewicz

@mareklangiewicz mareklangiewicz commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Filling a nil field of a required module from another file is reported as reimplementing it:

-- reg.lua
local M = { a = { to = nil } }
return M

-- use.lua
local reg = require("reg")
reg.a.to = function() end   -- duplicate-set-field: reimplements `to`

The cross-file check counted any module member with a value, nil included. The member now has to be function-like (a closure, or typed as a function), checked against the module's own syntax tree:

 check_cross_file_member_assign
   same_path / original_definition
-    module member has value_syntax
+    module member is_function_like(model, module_tree, member)

This PR first also fixed the same key on different tables (function M.A:init() and function 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

  • Before (main 2d2374f, this PR's tests only): test_setting_a_nil_field_of_a_required_module_is_not_duplicate fails. test_same_key_on_different_nested_tables_is_not_duplicate passes.
    After: both pass, and so do the 30 duplicate tests. 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

mareklangiewicz and others added 2 commits November 3, 2026 12:03
`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

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_map over ancestors can pick up an outer LuaIndexExpr rather than the immediate one. The subsequent get_index_key()?.get_range()? != key_range guard 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 known LuaIndexExpr, rather than searching from a token offset. If the offset approach is kept, add a comment explaining why right_biased and 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 is None, 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 that None intentionally 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_tree is correctly fetched via syntax_tree_of(module_file). Good.
  • In the main loop, tree is the current file's tree — correct for facts.members of the current file.
  • Risk: The function silently returns false when tree is None or the node can't be found, which changes behavior in the cross-file path (previously value_syntax.is_some() was the gate). A member with value_syntax set but a non-closure value (e.g. to = someFunc) will now be treated as not function-like, whereas before it passed the is_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_function on the resolved type) rather than returning false.

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.lua containing { a = { to = handler } } still behaves as expected, or broaden is_function_like to also accept resolved function types.

5. HashMap key now includes Option<String> — allocation per member

  • set_table_path allocates a String for 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_path takes &LuaSyntaxNode while get_red_root() returns an owned node; ensure the borrow doesn't outlive tree. It doesn't here, but the pattern is easy to break later.
  • Recommendation: Add a brief comment that root must outlive the loop because set_table_path borrows it.

7. Test coverage gaps

  • New tests cover the happy paths well. Missing:
    • Dynamic prefix case (getTable().A:init vs getTable().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.A vs M["A"]) — should these be considered the same table? Currently get_access_path likely differs, so they'd be treated as distinct. Add a test to pin the intended behavior.

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.

@mareklangiewicz

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I checked the main concerns against the branch:

  • Dynamic prefixes falling into a None bucket. get_access_path walks through call expressions, so getTable().A:init gets the path getTable.A rather than None. A member whose path really is None shares a bucket only with other None members. Before this change every member of an owner shared one bucket, so this can only report fewer duplicates than main did, never new ones.
  • is_function_like and a module field holding a function reference ({ a = { to = handler } }). When the value isn't a closure, is_function_like falls back to the member's type, so reg.a.to = function() end from another file is still reported.
  • M.A vs M["A"]. get_access_path turns both into M.A, so function M.A.f() end followed by M["A"].f = function() end is still reported as a duplicate.

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
@mareklangiewicz mareklangiewicz changed the title fix(check): duplicate-set-field on different tables and on nil module slots fix(check): duplicate-set-field ignores a nil field of a required module Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant