summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-11-09 00:09:13 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-11-09 00:09:13 +0800
commit679b32f9ad2999fb71fef335ce48b9a909f9937d (patch)
tree17f0de84e3f8026ba25b193f7d54eabe0f3e0b24
parent4d73939b6ec2320f72b35e1e193b8bf1fb18f64c (diff)
downloadlua-language-server-679b32f9ad2999fb71fef335ce48b9a909f9937d.zip
fix #1677
-rw-r--r--changelog.md2
-rw-r--r--script/parser/guide.lua11
-rw-r--r--script/vm/compiler.lua16
-rw-r--r--test/type_inference/init.lua18
4 files changed, 30 insertions, 17 deletions
diff --git a/changelog.md b/changelog.md
index 4c9ba5cb..d3908446 100644
--- a/changelog.md
+++ b/changelog.md
@@ -3,8 +3,10 @@
## 3.6.2
* `FIX` incorrect type check for generic with nil
* `FIX` [#1676]
+* `FIX` [#1677]
[#1676]: https://github.com/sumneko/lua-language-server/issues/1676
+[#1677]: https://github.com/sumneko/lua-language-server/issues/1677
## 3.6.1
`2022-11-8`
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 4a398fcb..b40207ed 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -281,17 +281,10 @@ function m.isLiteral(obj)
end
--- 获取字面量
----@param obj parser.object
+---@param obj table
---@return any
function m.getLiteral(obj)
- local tp = obj.type
- if tp == 'boolean' then
- return obj[1]
- elseif tp == 'string' then
- return obj[1]
- elseif tp == 'number' then
- return obj[1]
- elseif tp == 'integer' then
+ if m.isLiteral(obj) then
return obj[1]
end
return nil
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 86527fb7..0cc27d81 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -284,7 +284,7 @@ local searchFieldSwitch = util.switch()
---@param suri uri
---@param object vm.global
----@param key? string|vm.global
+---@param key? string|number|integer|boolean|vm.global
---@param ref boolean
---@param pushResult fun(field: vm.object, isMark?: boolean)
function vm.getClassFields(suri, object, key, ref, pushResult)
@@ -341,22 +341,22 @@ function vm.getClassFields(suri, object, key, ref, pushResult)
end
end
else
- local typeName
+ local keyObject
if keyType == 'number' then
if math.tointeger(key) then
- typeName = 'integer'
+ keyObject = { type = 'integer', [1] = key }
else
- typeName = 'number'
+ keyObject = { type = 'number', [1] = key }
end
elseif keyType == 'boolean'
or keyType == 'string' then
- typeName = keyType
+ keyObject = { type = keyType, [1] = key }
end
- if typeName and field.field.type ~= 'doc.field.name' then
+ if keyObject and field.field.type ~= 'doc.field.name' then
-- ---@field [integer] boolean -> class[1]
local fieldNode = vm.compileNode(field.field)
- if vm.isSubType(suri, typeName, fieldNode) then
- local nkey = '|' .. typeName
+ if vm.isSubType(suri, keyObject, fieldNode) then
+ local nkey = '|' .. keyType
if not searchedFields[nkey] then
pushResult(field, true)
hasFounded[nkey] = true
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 6032cd0f..40f9891d 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -3947,3 +3947,21 @@ end
local <?x?> = f(true)
]]
+
+TEST 'number' [[
+---@class A
+---@field [1] number
+---@field [2] boolean
+local t
+
+local <?n?> = t[1]
+]]
+
+TEST 'boolean' [[
+---@class A
+---@field [1] number
+---@field [2] boolean
+local t
+
+local <?n?> = t[2]
+]]