diff --git a/changelog.md b/changelog.md index 2f0ea8f72..2a35ff3d2 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `FIX` Improve type narrow by checking exact match on literal type params ## 3.10.5 `2024-8-19` diff --git a/script/vm/function.lua b/script/vm/function.lua index 1e3083172..f30c1126d 100644 --- a/script/vm/function.lua +++ b/script/vm/function.lua @@ -353,6 +353,33 @@ local function isAllParamMatched(uri, args, params) return true end +---@param uri uri +---@param args parser.object[] +---@param func parser.object +---@return integer +local function calcFunctionMatchScore(uri, args, func) + if vm.isVarargFunctionWithOverloads(func) + or not isAllParamMatched(uri, args, func.args) + then + return -1 + end + local matchScore = 0 + for i = 1, math.min(#args, #func.args) do + local arg, param = args[i], func.args[i] + local defLiterals = vm.getLiterals(param) + if defLiterals then + for n in vm.compileNode(arg):eachObject() do + -- if param's literals map contains arg's literal, this is the most narrowed exact match + if defLiterals[guide.getLiteral(n)] then + matchScore = matchScore + 1 + break + end + end + end + end + return matchScore +end + ---@param func parser.object ---@param args? parser.object[] ---@return parser.object[]? @@ -365,21 +392,29 @@ function vm.getExactMatchedFunctions(func, args) return funcs end local uri = guide.getUri(func) - local needRemove + local matchScores = {} for i, n in ipairs(funcs) do - if vm.isVarargFunctionWithOverloads(n) - or not isAllParamMatched(uri, args, n.args) then - if not needRemove then - needRemove = {} - end - needRemove[#needRemove+1] = i - end + matchScores[i] = calcFunctionMatchScore(uri, args, n) + end + + local maxMatchScore = math.max(table.unpack(matchScores)) + if maxMatchScore == -1 then + -- all should be removed + return nil end - if not needRemove then + + local minMatchScore = math.min(table.unpack(matchScores)) + if minMatchScore == maxMatchScore then + -- all should be kept return funcs end - if #needRemove == #funcs then - return nil + + -- remove functions that have matchScore < maxMatchScore + local needRemove = {} + for i, matchScore in ipairs(matchScores) do + if matchScore < maxMatchScore then + needRemove[#needRemove + 1] = i + end end util.tableMultiRemove(funcs, needRemove) return funcs