File tree Expand file tree Collapse file tree 4 files changed +79
-1
lines changed Expand file tree Collapse file tree 4 files changed +79
-1
lines changed Original file line number Diff line number Diff line change 9
9
* ` FIX ` Lua 5.1: fix incorrect warning when using setfenv with an int as first parameter
10
10
* ` FIX ` Improve type narrow by checking exact match on literal type params
11
11
* ` FIX ` Correctly list enums for function overload arguments [ #2840 ] ( https://github.com/LuaLS/lua-language-server/pull/2840 )
12
+ * ` FIX ` Incorrect function params' type infer when there is only ` @overload ` [ #2509 ] ( https://github.com/LuaLS/lua-language-server/issues/2509 ) [ #2708 ] ( https://github.com/LuaLS/lua-language-server/issues/2708 ) [ #2709 ] ( https://github.com/LuaLS/lua-language-server/issues/2709 )
12
13
13
14
## 3.10.5
14
15
` 2024-8-19 `
Original file line number Diff line number Diff line change @@ -1099,6 +1099,7 @@ local function compileFunctionParam(func, source)
1099
1099
1100
1100
-- local call ---@type fun(f: fun(x: number));call(function (x) end) --> x -> number
1101
1101
local funcNode = vm .compileNode (func )
1102
+ local found = false
1102
1103
for n in funcNode :eachObject () do
1103
1104
if n .type == ' doc.type.function' and n .args [aindex ] then
1104
1105
local argNode = vm .compileNode (n .args [aindex ])
@@ -1107,9 +1108,16 @@ local function compileFunctionParam(func, source)
1107
1108
vm .setNode (source , an )
1108
1109
end
1109
1110
end
1110
- return true
1111
+ -- NOTE: keep existing behavior for local call which only set type based on the 1st match
1112
+ if func .parent .type == ' callargs' then
1113
+ return true
1114
+ end
1115
+ found = true
1111
1116
end
1112
1117
end
1118
+ if found then
1119
+ return true
1120
+ end
1113
1121
1114
1122
local derviationParam = config .get (guide .getUri (func ), ' Lua.type.inferParamType' )
1115
1123
if derviationParam and func .parent .type == ' local' and func .parent .ref then
Original file line number Diff line number Diff line change 359
359
--- @return number
360
360
local function calcFunctionMatchScore (uri , args , func )
361
361
if vm .isVarargFunctionWithOverloads (func )
362
+ or vm .isFunctionWithOnlyOverloads (func )
362
363
or not isAllParamMatched (uri , args , func .args )
363
364
then
364
365
return - 1
@@ -490,6 +491,36 @@ function vm.isVarargFunctionWithOverloads(func)
490
491
return false
491
492
end
492
493
494
+ --- @param func table
495
+ --- @return boolean
496
+ function vm .isFunctionWithOnlyOverloads (func )
497
+ if func .type ~= ' function' then
498
+ return false
499
+ end
500
+ if func ._onlyOverloadFunction ~= nil then
501
+ return func ._onlyOverloadFunction
502
+ end
503
+
504
+ if not func .bindDocs then
505
+ func ._onlyOverloadFunction = false
506
+ return false
507
+ end
508
+ local hasOverload = false
509
+ for _ , doc in ipairs (func .bindDocs ) do
510
+ if doc .type == ' doc.overload' then
511
+ hasOverload = true
512
+ elseif doc .type == ' doc.param'
513
+ or doc .type == ' doc.return'
514
+ then
515
+ -- has specified @param or @return, thus not only @overload
516
+ func ._onlyOverloadFunction = false
517
+ return false
518
+ end
519
+ end
520
+ func ._onlyOverloadFunction = hasOverload
521
+ return true
522
+ end
523
+
493
524
--- @param func parser.object
494
525
--- @return boolean
495
526
function vm .isEmptyFunction (func )
Original file line number Diff line number Diff line change @@ -172,6 +172,44 @@ local v = 'y'
172
172
local <?r?> = f(v)
173
173
]]
174
174
175
+ TEST ' string|number' [[
176
+ ---@overload fun(a: string)
177
+ ---@overload fun(a: number)
178
+ local function f(<?a?>) end
179
+ ]]
180
+
181
+ TEST ' 1|2' [[
182
+ ---@overload fun(a: 1)
183
+ ---@overload fun(a: 2)
184
+ local function f(<?a?>) end
185
+ ]]
186
+
187
+ TEST ' string' [[
188
+ ---@overload fun(a: 1): string
189
+ ---@overload fun(a: 2): number
190
+ local function f(a) end
191
+
192
+ local <?r?> = f(1)
193
+ ]]
194
+
195
+ TEST ' number' [[
196
+ ---@overload fun(a: 1): string
197
+ ---@overload fun(a: 2): number
198
+ local function f(a) end
199
+
200
+ local <?r?> = f(2)
201
+ ]]
202
+
203
+ TEST ' string|number' [[
204
+ ---@overload fun(a: 1): string
205
+ ---@overload fun(a: 2): number
206
+ local function f(a) end
207
+
208
+ ---@type number
209
+ local v
210
+ local <?r?> = f(v)
211
+ ]]
212
+
175
213
TEST ' number' [[
176
214
---@overload fun(a: 1, c: fun(x: number))
177
215
---@overload fun(a: 2, c: fun(x: string))
You can’t perform that action at this time.
0 commit comments