Skip to content

Commit 2a9570c

Browse files
committed
fix #1679
1 parent 63168b7 commit 2a9570c

File tree

5 files changed

+45
-26
lines changed

5 files changed

+45
-26
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* `FIX` incorrect type check for generic with nil
55
* `FIX` [#1676]
66
* `FIX` [#1677]
7+
* `FIX` [#1679]
78
* `FIX` [#1680]
89

910
[#1676]: https://github.com/sumneko/lua-language-server/issues/1676
1011
[#1677]: https://github.com/sumneko/lua-language-server/issues/1677
12+
[#1679]: https://github.com/sumneko/lua-language-server/issues/1679
1113
[#1680]: https://github.com/sumneko/lua-language-server/issues/1680
1214

1315
## 3.6.1

script/vm/node.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,19 @@ function mt:remove(name)
246246
or (c.type == 'doc.type.table' and name == 'table')
247247
or (c.type == 'doc.type.array' and name == 'table')
248248
or (c.type == 'doc.type.sign' and name == c.node[1])
249-
or (c.type == 'doc.type.function' and name == 'function') then
249+
or (c.type == 'doc.type.function' and name == 'function')
250+
or (c.type == 'doc.type.string' and name == 'string') then
250251
table.remove(self, index)
251252
self[c] = nil
252253
end
253254
end
254255
return self
255256
end
256257

258+
---@param uri uri
257259
---@param name string
258-
function mt:narrow(name)
259-
if name ~= 'nil' and self.optional == true then
260+
function mt:narrow(uri, name)
261+
if self.optional == true then
260262
self.optional = nil
261263
end
262264
for index = #self, 1, -1 do
@@ -267,12 +269,13 @@ function mt:narrow(name)
267269
or (c.type == 'doc.type.table' and name == 'table')
268270
or (c.type == 'doc.type.array' and name == 'table')
269271
or (c.type == 'doc.type.sign' and name == c.node[1])
270-
or (c.type == 'doc.type.function' and name == 'function') then
272+
or (c.type == 'doc.type.function' and name == 'function')
273+
or (c.type == 'doc.type.string' and name == 'string') then
271274
goto CONTINUE
272275
end
273276
if c.type == 'global' and c.cate == 'type' then
274277
if (c.name == name)
275-
or (c.name == 'integer' and name == 'number') then
278+
or (vm.isSubType(uri, c.name, name)) then
276279
goto CONTINUE
277280
end
278281
end

script/vm/runner.lua

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local guide = require 'parser.guide'
1111
---@field _mark table
1212
---@field _has table<parser.object, true>
1313
---@field _main parser.object
14+
---@field _uri uri
1415
local mt = {}
1516
mt.__index = mt
1617
mt._index = 1
@@ -168,15 +169,18 @@ function mt:_lookIntoChild(action, topNode, outNode)
168169
-- if x == y then
169170
topNode = self:_lookIntoChild(handler, topNode, outNode)
170171
local checkerNode = vm.compileNode(checker)
171-
if action.op.type == '==' then
172-
topNode = checkerNode
173-
if outNode then
174-
outNode:removeNode(topNode)
175-
end
176-
else
177-
topNode:removeNode(checkerNode)
178-
if outNode then
179-
outNode = checkerNode
172+
local checkerName = vm.getNodeName(checker)
173+
if checkerName then
174+
if action.op.type == '==' then
175+
topNode:narrow(self._uri, checkerName)
176+
if outNode then
177+
outNode:removeNode(checkerNode)
178+
end
179+
else
180+
topNode:removeNode(checkerNode)
181+
if outNode then
182+
outNode:narrow(self._uri, checkerName)
183+
end
180184
end
181185
end
182186
elseif handler.type == 'call'
@@ -189,14 +193,14 @@ function mt:_lookIntoChild(action, topNode, outNode)
189193
-- if type(x) == 'string' then
190194
self:_lookIntoChild(handler, topNode:copy())
191195
if action.op.type == '==' then
192-
topNode:narrow(checker[1])
196+
topNode:narrow(self._uri, checker[1])
193197
if outNode then
194198
outNode:remove(checker[1])
195199
end
196200
else
197201
topNode:remove(checker[1])
198202
if outNode then
199-
outNode:narrow(checker[1])
203+
outNode:narrow(self._uri, checker[1])
200204
end
201205
end
202206
elseif handler.type == 'getlocal'
@@ -215,14 +219,14 @@ function mt:_lookIntoChild(action, topNode, outNode)
215219
and call.args[1].node == self._loc then
216220
-- `local tp = type(x);if tp == 'string' then`
217221
if action.op.type == '==' then
218-
topNode:narrow(checker[1])
222+
topNode:narrow(self._uri, checker[1])
219223
if outNode then
220224
outNode:remove(checker[1])
221225
end
222226
else
223227
topNode:remove(checker[1])
224228
if outNode then
225-
outNode:narrow(checker[1])
229+
outNode:narrow(self._uri, checker[1])
226230
end
227231
end
228232
end
@@ -352,6 +356,7 @@ function vm.launchRunner(loc, callback)
352356
_mark = {},
353357
_has = {},
354358
_main = main,
359+
_uri = guide.getUri(loc),
355360
_callback = callback,
356361
}, mt)
357362

script/vm/type.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local lang = require 'language'
99

1010
---@param object vm.node.object
1111
---@return string?
12-
local function getNodeName(object)
12+
function vm.getNodeName(object)
1313
if object.type == 'global' and object.cate == 'type' then
1414
---@cast object vm.global
1515
return object.name
@@ -90,14 +90,14 @@ local function checkParentEnum(parentName, child, uri, mark, errs)
9090
return false
9191
else
9292
---@cast child parser.object
93-
local childName = getNodeName(child)
93+
local childName = vm.getNodeName(child)
9494
if childName == 'number'
9595
or childName == 'integer'
9696
or childName == 'boolean'
9797
or childName == 'string' then
9898
for _, enum in ipairs(enums) do
9999
for nd in vm.compileNode(enum):eachObject() do
100-
if childName == getNodeName(nd) and nd[1] == child[1] then
100+
if childName == vm.getNodeName(nd) and nd[1] == child[1] then
101101
return true
102102
end
103103
end
@@ -284,7 +284,7 @@ function vm.isSubType(uri, child, parent, mark, errs)
284284
if config.get(uri, 'Lua.type.weakUnionCheck') then
285285
local hasKnownType = 0
286286
for n in child:eachObject() do
287-
if getNodeName(n) then
287+
if vm.getNodeName(n) then
288288
hasKnownType = hasKnownType + 1
289289
if vm.isSubType(uri, n, parent, mark, errs) == true then
290290
return true
@@ -303,7 +303,7 @@ function vm.isSubType(uri, child, parent, mark, errs)
303303
else
304304
local weakNil = config.get(uri, 'Lua.type.weakNilCheck')
305305
for n in child:eachObject() do
306-
local nodeName = getNodeName(n)
306+
local nodeName = vm.getNodeName(n)
307307
if nodeName
308308
and not (nodeName == 'nil' and weakNil)
309309
and vm.isSubType(uri, n, parent, mark, errs) == false then
@@ -329,7 +329,7 @@ function vm.isSubType(uri, child, parent, mark, errs)
329329
end
330330

331331
---@cast child vm.node.object
332-
local childName = getNodeName(child)
332+
local childName = vm.getNodeName(child)
333333
if childName == 'any'
334334
or childName == 'unknown' then
335335
return true
@@ -349,7 +349,7 @@ function vm.isSubType(uri, child, parent, mark, errs)
349349
elseif parent.type == 'vm.node' then
350350
local hasKnownType = 0
351351
for n in parent:eachObject() do
352-
if getNodeName(n) then
352+
if vm.getNodeName(n) then
353353
hasKnownType = hasKnownType + 1
354354
if vm.isSubType(uri, child, n, mark, errs) == true then
355355
return true
@@ -377,7 +377,7 @@ function vm.isSubType(uri, child, parent, mark, errs)
377377

378378
---@cast parent vm.node.object
379379

380-
local parentName = getNodeName(parent)
380+
local parentName = vm.getNodeName(parent)
381381
if parentName == 'any'
382382
or parentName == 'unknown' then
383383
return true

test/type_inference/init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,3 +3965,12 @@ local t
39653965
39663966
local <?n?> = t[2]
39673967
]]
3968+
3969+
TEST 'N' [[
3970+
---@class N: number
3971+
local x
3972+
3973+
if x == 0.1 then
3974+
print(<?x?>)
3975+
end
3976+
]]

0 commit comments

Comments
 (0)