summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-22 16:54:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-22 16:54:29 +0800
commit72effeb8f9d9f3c04d4f5aa4f228dcd1d84f2815 (patch)
tree7dd84148241e2d68930250fc8c294ffa80506134
parent09512074acae34fccc60f31a8c84d022e03ddf23 (diff)
downloadlua-language-server-72effeb8f9d9f3c04d4f5aa4f228dcd1d84f2815.zip
update
-rw-r--r--meta/template/basic.lua4
-rw-r--r--meta/template/debug.lua2
-rw-r--r--script/config/config.lua2
-rw-r--r--script/core/code-action.lua4
-rw-r--r--script/provider/markdown.lua1
-rw-r--r--script/vm/compiler.lua24
-rw-r--r--script/vm/node.lua9
-rw-r--r--script/vm/type.lua10
-rw-r--r--test/diagnostics/type-check.lua8
-rw-r--r--test/type_inference/init.lua23
10 files changed, 72 insertions, 15 deletions
diff --git a/meta/template/basic.lua b/meta/template/basic.lua
index 4509258e..8cae0ef5 100644
--- a/meta/template/basic.lua
+++ b/meta/template/basic.lua
@@ -209,8 +209,8 @@ function select(index, ...) end
function setfenv(f, table) end
---#DES 'setmetatable'
----@param table table
----@param metatable table
+---@param table table
+---@param metatable? table
---@return table
function setmetatable(table, metatable) end
diff --git a/meta/template/debug.lua b/meta/template/debug.lua
index f62aca6e..7cb417b2 100644
--- a/meta/template/debug.lua
+++ b/meta/template/debug.lua
@@ -167,7 +167,7 @@ function debug.setlocal(thread, level, index, value) end
---#DES 'debug.setmetatable'
---@generic T
---@param value T
----@param meta table
+---@param meta? table
---@return T value
function debug.setmetatable(value, meta) end
diff --git a/script/config/config.lua b/script/config/config.lua
index 73d21b0b..df998990 100644
--- a/script/config/config.lua
+++ b/script/config/config.lua
@@ -164,7 +164,7 @@ function m.prop(uri, key, prop, value)
return false
end
----@param uri uri
+---@param uri? uri
---@param key string
---@return any
function m.get(uri, key)
diff --git a/script/core/code-action.lua b/script/core/code-action.lua
index 116fff24..d2cf440a 100644
--- a/script/core/code-action.lua
+++ b/script/core/code-action.lua
@@ -5,6 +5,10 @@ local sp = require 'bee.subprocess'
local guide = require "parser.guide"
local converter = require 'proto.converter'
+---@param uri uri
+---@param row integer
+---@param mode string
+---@param code string
local function checkDisableByLuaDocExits(uri, row, mode, code)
if row < 0 then
return nil
diff --git a/script/provider/markdown.lua b/script/provider/markdown.lua
index 653ae135..6b007713 100644
--- a/script/provider/markdown.lua
+++ b/script/provider/markdown.lua
@@ -48,6 +48,7 @@ function mt:emptyLine()
return self
end
+---@return string
function mt:string(nl)
if self._cacheResult then
return self._cacheResult
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 795113e0..2f3cc36f 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -1105,7 +1105,7 @@ local binarySwich = util.switch()
start = source.start,
finish = source.finish,
parent = source,
- [1] =result,
+ [1] = result,
})
else
vm.setNode(source, vm.declareGlobal('type', 'integer'))
@@ -1140,7 +1140,7 @@ local binarySwich = util.switch()
start = source.start,
finish = source.finish,
parent = source,
- [1] =result,
+ [1] = result,
})
else
if op == '+'
@@ -1149,10 +1149,15 @@ local binarySwich = util.switch()
or op == '//'
or op == '%' then
local uri = guide.getUri(source)
- if vm.getInfer(source[1]):view(uri) == 'integer'
- and vm.getInfer(source[2]):view(uri) == 'integer' then
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- return
+ local infer1 = vm.getInfer(source[1])
+ local infer2 = vm.getInfer(source[2])
+ if infer1:hasType(uri, 'integer')
+ or infer2:hasType(uri, 'integer') then
+ if not infer1:hasType(uri, 'number')
+ and not infer2:hasType(uri, 'number') then
+ vm.setNode(source, vm.declareGlobal('type', 'integer'))
+ return
+ end
end
end
vm.setNode(source, vm.declareGlobal('type', 'number'))
@@ -1736,7 +1741,12 @@ local compilerSwitch = util.switch()
if source.op.type == '-' then
local v = vm.getNumber(source[1])
if v == nil then
- vm.setNode(source, vm.declareGlobal('type', 'number'))
+ local infer = vm.getInfer(source[1])
+ if infer:hasType(guide.getUri(source), 'integer') then
+ vm.setNode(source, vm.declareGlobal('type', 'integer'))
+ else
+ vm.setNode(source, vm.declareGlobal('type', 'number'))
+ end
return
else
vm.setNode(source, {
diff --git a/script/vm/node.lua b/script/vm/node.lua
index 61781e5f..8ed5c027 100644
--- a/script/vm/node.lua
+++ b/script/vm/node.lua
@@ -310,6 +310,7 @@ end
---@param source vm.object
---@param node vm.node | vm.object
---@param cover? boolean
+---@return vm.node
function vm.setNode(source, node, cover)
if not node then
if TEST then
@@ -324,18 +325,20 @@ function vm.setNode(source, node, cover)
if cover then
---@cast node vm.node
vm.nodeCache[source] = node
- return
+ return node
end
local me = vm.nodeCache[source]
if me then
me:merge(node)
else
if node.type == 'vm.node' then
- vm.nodeCache[source] = node:copy()
+ me = node:copy()
else
- vm.nodeCache[source] = vm.createNode(node)
+ me = vm.createNode(node)
end
+ vm.nodeCache[source] = me
end
+ return me
end
---@param source vm.object
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 8e8ca96b..5735b482 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -250,6 +250,16 @@ function vm.canCastType(uri, defNode, refNode)
return true
end
end
+
+ if vm.isSubType(uri, refNode, 'number') then
+ -- allow `local x = 0;x = 1.0`,
+ -- but not allow `local x ---@type integer;x = 1.0`
+ if defInfer:hasType(uri, 'integer')
+ and not defNode:hasType 'integer' then
+ return true
+ end
+ end
+
if vm.isSubType(uri, refNode, defNode) then
return true
end
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index b9681d78..bb0ccdc0 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -324,5 +324,13 @@ local x
<!x!> = 1.5
]]
+TEST [[
+---@diagnostic disable:undefined-global
+---@type integer
+local x
+
+x = 1 + G
+]]
+
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 74ac7120..2c17363b 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -210,6 +210,27 @@ TEST 'integer' [[
<?x?> = 1 + 2
]]
+TEST 'integer' [[
+---@type integer
+local a
+
+<?x?> = - a
+]]
+
+TEST 'number' [[
+local a
+
+<?x?> = - a
+]]
+
+TEST 'integer' [[
+<?x?> = 1 + X
+]]
+
+TEST 'number' [[
+<?x?> = 1.0 + X
+]]
+
TEST 'tablelib' [[
---@class tablelib
table = {}
@@ -2358,7 +2379,7 @@ end
local <?z?> = f()
]]
-TEST 'number|table' [[
+TEST 'integer|table' [[
local function returnI()
return a + 1
end