-
-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: improve function type narrow by checking params' literal identical #2822
Conversation
09ba965
to
742117c
Compare
Seems my latest strategy of checking param's So the logic will add bonus score for both the base function and the overload one 由於 ---@alias ccTweaked.os.event
---| "alarm"
---| "char"
---| string
---@async
---@param event? ccTweaked.os.event
---@return any ...
---@overload fun(event: "alarm"): "alarm", integer
---@overload fun(event: "char"): "char", string
function os.pullEvent(event) end
local event, alarmID = os.pullEvent("alarm")
--> any, any (expected: "alarm", integer)
local event, character = os.pullEvent("char")
--> any, any (expected: "char", string) 要再想想先。。。 |
想到了 🙂 計分制中不直接做
這樣就傾向選擇 param literal type 定義為較 narrow 的 function 了 ---@alias ccTweaked.os.event
---| "alarm"
---| "char"
---| string
---@async
---@param event? ccTweaked.os.event
---@return any ...
---@overload fun(event: "alarm"): "alarm", integer
---@overload fun(event: "char"): "char", string
function os.pullEvent(event) end
local event, alarmID = os.pullEvent("alarm")
--> "alarm", integer (good)
local event, character = os.pullEvent("char")
--> "char", string (good) |
742117c
to
e09d181
Compare
先 hold 一下,我突然又有些想法估計可以 fix #2509 🤔
要些時間驗證一下 edit: 還是分開另外處理吧 |
我没有看代码,但是看描述我觉得无论什么时候都不应该用计分。如果无法确定如何narrow,那就不做narrow。 |
我這裡的計分,最初其實是 計算 exact literal match 的 param count
讓我具體舉一些例子 例子1---@overload fun(a: string): string
---@overload fun(a: 'test'): integer
local function f(...) end
例子2---@alias ccTweaked.os.event
---| "alarm"
---| "char"
---| string
---@async
---@param event? ccTweaked.os.event
---@return any ...
---@overload fun(event: "alarm"): "alarm", integer
---@overload fun(event: "char"): "char", string
function os.pullEvent(event) end
我大概理解你的擔心,是以為這計分制是 (胡亂) 計算什麼 similarity 之類? |
明白了,这里的计分指的是narrow程度,那应该没有问题 |
確實你這個說法更準確 👍 |
The following related issues are not fixed by this PR, but I tested and they already work as of
v3.10.5
:#2509, #1343, #1146edit: I originally thought #2509 is working, but NO. Just that the reproduction code is not complete, and I didn't reproduce it and think it is solved. I confirmed that it is still unsolved with a more complete example code.
The Problem
Originated from an example provided by @carsakiller: #1456 (comment), where the base signature contains a param type that is a superset of the other overload, type narrow feature will always includes the baseline one and cannot perform a more accurate type narrow.
A minimal example:
Proposed Solution
I introduced a match score when trying to do a further type narrow. After the 1st pass checking
isAllParamMatched
in the existing logic, I tried to check if their arguments' literal have exact match:123
,"test"
This is just an extra logic and should be no worse than current logic. Because when there are no identical view param, all functions just have the same score
0
=> all will be kept as in the existing logic.After the fix:
中文版
長話短說,我嘗試在
vm.getExactMatchedFunctions
引入1個 計分制在滿足原有的
isAllParamMatched
前題下這樣能夠在 literal type 的 param exact match 時有較高分數
達致更準確的 type narrow
=> 所有 function 都是
0分
=> 全部保留我不確定有沒有更好改法
這個需要 @sumneko 來 review
(希望我沒有像上次一樣,fix 了1個問題卻又引起新 bug ... 🤦♂️ )