summaryrefslogtreecommitdiff
path: root/test/type_inference
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-12-16 18:02:13 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-12-16 18:02:13 +0800
commit37a0d728ffdccfb0ab40017e43984fb82276de1d (patch)
tree66a4cd5e00c7e22075a9bac2293f41f3e08ae92d /test/type_inference
parent85c2145b172562142d82d87b844a7bc537cabf2a (diff)
parent4073e9e8bcdb5e48fd01f9f1fd14e2942a356ad0 (diff)
downloadlua-language-server-37a0d728ffdccfb0ab40017e43984fb82276de1d.zip
Merge branch 'trace'
Diffstat (limited to 'test/type_inference')
-rw-r--r--test/type_inference/init.lua158
1 files changed, 153 insertions, 5 deletions
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 33521a0d..0b69a34c 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -1590,7 +1590,7 @@ AAA = {}
local <?x?> = AAA()
]]
-TEST 'string|integer' [[
+TEST 'string' [[
local <?x?>
x = '1'
x = 1
@@ -1637,7 +1637,7 @@ function A()
end
]]
-TEST 'unknown' [[
+TEST 'string' [[
local x
function A()
@@ -1758,6 +1758,26 @@ x = '1'
x = 1
]]
+TEST 'integer' [[
+local x
+x = true
+do
+ x = 1
+end
+print(<?x?>)
+]]
+
+TEST 'boolean' [[
+local x
+x = true
+function XX()
+ do
+ x = 1
+ end
+end
+print(<?x?>)
+]]
+
TEST 'integer?' [[
---@type integer?
local <?x?>
@@ -1809,6 +1829,17 @@ end
print(<?x?>)
]]
+TEST 'nil' [[
+---@type integer?
+local x
+
+if not x then
+ print(<?x?>)
+end
+
+print(x)
+]]
+
TEST 'integer' [[
---@type integer?
local x
@@ -1840,6 +1871,15 @@ if xxx and x then
end
]]
+TEST 'unknown' [[
+---@type integer?
+local x
+
+if not x and x then
+ print(<?x?>)
+end
+]]
+
TEST 'integer' [[
---@type integer?
local x
@@ -2277,7 +2317,7 @@ local x
print(<?x?>)
]]
-TEST 'unknown?' [[
+TEST 'nil' [[
---@type string?
local x
@@ -2351,7 +2391,7 @@ end
print(<?t?>)
]]
-TEST 'unknown?' [[
+TEST 'nil' [[
---@type integer?
local t
@@ -3160,7 +3200,7 @@ local function f() end
local x, y, <?z?> = 1, 2, f()
]]
-TEST 'function' [[
+TEST 'unknown' [[
local f
print(<?f?>)
@@ -3168,6 +3208,26 @@ print(<?f?>)
function f() end
]]
+TEST 'unknown' [[
+local f
+
+do
+ print(<?f?>)
+end
+
+function f() end
+]]
+
+TEST 'function' [[
+local f
+
+function A()
+ print(<?f?>)
+end
+
+function f() end
+]]
+
TEST 'number' [[
---@type number|nil
local n
@@ -4000,3 +4060,91 @@ local m, v
local <?r?> = m * v
]]
+
+TEST 'A|B' [[
+---@class A
+---@class B
+
+---@type A|B
+local t
+
+if x then
+ ---@cast t A
+else
+ print(<?t?>)
+end
+]]
+
+TEST 'A|B' [[
+---@class A
+---@class B
+
+---@type A|B
+local t
+
+if x then
+ ---@cast t A
+elseif <?t?> then
+end
+]]
+
+TEST 'A|B' [[
+---@class A
+---@class B
+
+---@type A|B
+local t
+
+if x then
+ ---@cast t A
+ print(t)
+elseif <?t?> then
+end
+]]
+
+TEST 'A|B' [[
+---@class A
+---@class B
+
+---@type A|B
+local t
+
+if x then
+ ---@cast t A
+ print(t)
+elseif <?t?> then
+ ---@cast t A
+ print(t)
+end
+]]
+
+TEST 'function' [[
+local function x()
+ print(<?x?>)
+end
+]]
+
+TEST 'number' [[
+---@type number?
+local x
+
+do
+ if not x then
+ return
+ end
+end
+
+print(<?x?>)
+]]
+
+TEST 'number' [[
+---@type number[]
+local xs
+
+---@type fun(x): number?
+local f
+
+for _, <?x?> in ipairs(xs) do
+ x = f(x)
+end
+]]