fix: return single definition for anonymous-function field assignment - #3450
Open
tomlau10 wants to merge 1 commit into
Open
fix: return single definition for anonymous-function field assignment#3450tomlau10 wants to merge 1 commit into
tomlau10 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Notice: It looks like you're using hustcer/deepseek-review, but the CHAT_TOKEN hasn't been configured in your repo's Variables/Secrets. Please ensure this token is set for proper functionality. For step-by-step guidance, refer to the CHAT_TOKEN Config section of README.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2451
Summary
Go to definition on a field assigned an anonymous function returned two candidates on the same line — the field name and the function value.
local f = function() endbehaves the same way. After this PR, only the field name is returned, consistent with how literal values already behave (e.g.X.y = 1jumps toy).Root cause (click to expand)
vm.getDefsproduces two independent defs through two paths:Name candidate (
c):compileByNodeChainresolves thesetfieldLHS, and the definition provider unwraps it viasrc.field.Function-value candidate (
function):searchByNode→compileNodemerges the field's value into the compiled type. Thefunctionnode survives the literal filter because function literals are explicitly exempted there — an exemption meant for generic type inference (e.g.v2 = f(function() end)), not for direct assignments.Neither existing dedup catches it:
node-identity dedup in
getDefstreats them as different nodes;the nested-range dedup in
sortResultsonly removes a target whose range contains another — these two are same-line siblings.Fix (click to expand)
In
script/core/definition.lua, after collecting targets, drop a function-value target when its owning assignment's name node is also a target:Why this shape:
Conditioned on the owner also being a target, so generic-inference defs (where the function literal is the only candidate) are left untouched.
Scoped to the definition provider only.
type-definitionandimplementationalready return a single candidate through their own filters (a type whitelist andgetRefs+isAssignrespectively), so they are unaffected.Tests
Updated
test/definition/function.lua:local f = function() endnow expects a single definition atf.Added
test/definition/field.luaregression cases for both global and local tables (A.c = function() end).Full test suite passes.
中文摘要
A.c = function() end的寫法,goto definition 原本會回傳兩個結果(欄位名稱c和 function 值)。此 PR 在definition.lua加了一段處理:當欄位名稱也在結果中時,移除冗餘的 function 值,只保留名稱節點,與X.y = 1跳到y的慣例一致。type-definition / implementation 兩個 provider 因各自已有 filter,不受影響;測試全部通過。此 bug 的根源,是
function字面量豁免在 2022 年某次 global-manager/infer 大型 refactor (0e159ee037) 中順手加入的,屬 refactor 的副作用、並非刻意設計;回傳 2 個 candidate 的測試也是同期的批量updatecommit (9451329b33) 帶入的。本 PR 由 Claude Code(deepseek-v4-flash)協助分析與撰寫。