Skip to content

Commit 679b32f

Browse files
committed
fix #1677
1 parent 4d73939 commit 679b32f

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
## 3.6.2
44
* `FIX` incorrect type check for generic with nil
55
* `FIX` [#1676]
6+
* `FIX` [#1677]
67

78
[#1676]: https://github.com/sumneko/lua-language-server/issues/1676
9+
[#1677]: https://github.com/sumneko/lua-language-server/issues/1677
810

911
## 3.6.1
1012
`2022-11-8`

script/parser/guide.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,10 @@ function m.isLiteral(obj)
281281
end
282282

283283
--- 获取字面量
284-
---@param obj parser.object
284+
---@param obj table
285285
---@return any
286286
function m.getLiteral(obj)
287-
local tp = obj.type
288-
if tp == 'boolean' then
289-
return obj[1]
290-
elseif tp == 'string' then
291-
return obj[1]
292-
elseif tp == 'number' then
293-
return obj[1]
294-
elseif tp == 'integer' then
287+
if m.isLiteral(obj) then
295288
return obj[1]
296289
end
297290
return nil

script/vm/compiler.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ local searchFieldSwitch = util.switch()
284284

285285
---@param suri uri
286286
---@param object vm.global
287-
---@param key? string|vm.global
287+
---@param key? string|number|integer|boolean|vm.global
288288
---@param ref boolean
289289
---@param pushResult fun(field: vm.object, isMark?: boolean)
290290
function vm.getClassFields(suri, object, key, ref, pushResult)
@@ -341,22 +341,22 @@ function vm.getClassFields(suri, object, key, ref, pushResult)
341341
end
342342
end
343343
else
344-
local typeName
344+
local keyObject
345345
if keyType == 'number' then
346346
if math.tointeger(key) then
347-
typeName = 'integer'
347+
keyObject = { type = 'integer', [1] = key }
348348
else
349-
typeName = 'number'
349+
keyObject = { type = 'number', [1] = key }
350350
end
351351
elseif keyType == 'boolean'
352352
or keyType == 'string' then
353-
typeName = keyType
353+
keyObject = { type = keyType, [1] = key }
354354
end
355-
if typeName and field.field.type ~= 'doc.field.name' then
355+
if keyObject and field.field.type ~= 'doc.field.name' then
356356
-- ---@field [integer] boolean -> class[1]
357357
local fieldNode = vm.compileNode(field.field)
358-
if vm.isSubType(suri, typeName, fieldNode) then
359-
local nkey = '|' .. typeName
358+
if vm.isSubType(suri, keyObject, fieldNode) then
359+
local nkey = '|' .. keyType
360360
if not searchedFields[nkey] then
361361
pushResult(field, true)
362362
hasFounded[nkey] = true

test/type_inference/init.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3947,3 +3947,21 @@ end
39473947
39483948
local <?x?> = f(true)
39493949
]]
3950+
3951+
TEST 'number' [[
3952+
---@class A
3953+
---@field [1] number
3954+
---@field [2] boolean
3955+
local t
3956+
3957+
local <?n?> = t[1]
3958+
]]
3959+
3960+
TEST 'boolean' [[
3961+
---@class A
3962+
---@field [1] number
3963+
---@field [2] boolean
3964+
local t
3965+
3966+
local <?n?> = t[2]
3967+
]]

0 commit comments

Comments
 (0)