Skip to content

Commit 3b642df

Browse files
committed
fix #1037
1 parent 2e4f530 commit 3b642df

File tree

8 files changed

+58
-24
lines changed

8 files changed

+58
-24
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `FIX` [#1034](https://github.com/sumneko/lua-language-server/issues/1034)
66
* `FIX` [#1035](https://github.com/sumneko/lua-language-server/issues/1035)
77
* `FIX` [#1036](https://github.com/sumneko/lua-language-server/issues/1036)
8+
* `FIX` runtime errors reported by telemetry, see [#1037](https://github.com/sumneko/lua-language-server/issues/1037)
89

910
## 3.0.0
1011
`2022-4-10`

script/core/completion/completion.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ local function checkModule(state, word, position, results)
420420
end
421421

422422
local function checkFieldFromFieldToIndex(state, name, src, parent, word, startPos, position)
423-
if type(name) == 'string' and name:match '^[%a_][%w_]*$' then
423+
if name:match '^[%a_][%w_]*$' then
424424
return nil
425425
end
426426
local textEdit, additionalTextEdits
@@ -493,7 +493,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
493493
kind = define.CompletionItemKind.Function
494494
end
495495
buildFunction(results, src, value, oop, {
496-
label = tostring(name),
496+
label = name,
497497
kind = kind,
498498
match = name:match '^[^(]+',
499499
insertText = name:match '^[^(]+',
@@ -526,7 +526,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
526526
textEdit, additionalTextEdits = checkFieldFromFieldToIndex(state, name, src, parent, word, startPos, position)
527527
end
528528
results[#results+1] = {
529-
label = tostring(name),
529+
label = name,
530530
kind = kind,
531531
deprecated = vm.isDeprecated(src) or nil,
532532
textEdit = textEdit,
@@ -554,6 +554,7 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
554554
if isSameSource(state, src, startPos) then
555555
goto CONTINUE
556556
end
557+
name = tostring(name)
557558
if isGlobal and locals and locals[name] then
558559
goto CONTINUE
559560
end
@@ -1376,7 +1377,9 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
13761377
if left then
13771378
for _, field in ipairs(fields) do
13781379
local name = guide.getKeyName(field)
1379-
if not mark[name] and matchKey(left, guide.getKeyName(field)) then
1380+
if name
1381+
and not mark[name]
1382+
and matchKey(left, tostring(name)) then
13801383
results[#results+1] = {
13811384
label = guide.getKeyName(field),
13821385
kind = define.CompletionItemKind.Property,

script/core/diagnostics/circle-doc-class.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ return function (uri, callback)
3737
}
3838
goto CONTINUE
3939
end
40-
if not mark[newName] then
40+
if newName and not mark[newName] then
4141
mark[newName] = true
4242
local docs = vm.getDocSets(uri, newName)
4343
for _, otherDoc in ipairs(docs) do

script/core/hover/description.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ local function tryDocModule(source)
146146
end
147147

148148
local function buildEnumChunk(docType, name)
149+
if not docType then
150+
return nil
151+
end
149152
local enums = {}
150153
local types = {}
151154
local lines = {}
@@ -164,7 +167,7 @@ local function buildEnumChunk(docType, name)
164167
end
165168
end
166169
if #enums == 0 then
167-
return
170+
return nil
168171
end
169172
lines[#lines+1] = ('%s:'):format(name)
170173
for _, enum in ipairs(enums) do

script/library.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ local function compileSingleMetaDoc(uri, script, metaLang, status)
109109
version = 5.1
110110
jit = true
111111
else
112-
version = tonumber(config.get(uri, 'Lua.runtime.version'):sub(-3))
112+
version = tonumber(config.get(uri, 'Lua.runtime.version'):sub(-3)) or 5.4
113113
jit = false
114114
end
115115

script/vm/compiler.lua

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ local searchFieldSwitch = util.switch()
8787
: call(function (suri, node, key, pushResult)
8888
-- change to `string: stringlib` ?
8989
local stringlib = globalMgr.getGlobal('type', 'stringlib')
90-
m.getClassFields(suri, stringlib, key, pushResult)
90+
if stringlib then
91+
m.getClassFields(suri, stringlib, key, pushResult)
92+
end
9193
end)
9294
: case 'local'
9395
: call(function (suri, node, key, pushResult)
@@ -302,8 +304,8 @@ function m.getReturnOfFunction(func, index)
302304
end
303305

304306
local function getReturnOfSetMetaTable(args)
305-
local tbl = args and args[1]
306-
local mt = args and args[2]
307+
local tbl = args[1]
308+
local mt = args[2]
307309
local node = union()
308310
if tbl then
309311
node:merge(m.compileNode(tbl))
@@ -325,23 +327,35 @@ end
325327

326328
local function getReturn(func, index, args)
327329
if func.special == 'setmetatable' then
330+
if not args then
331+
return nil
332+
end
328333
return getReturnOfSetMetaTable(args)
329334
end
330335
if func.special == 'pcall' and index > 1 then
336+
if not args then
337+
return nil
338+
end
331339
local newArgs = {}
332340
for i = 2, #args do
333341
newArgs[#newArgs+1] = args[i]
334342
end
335343
return getReturn(args[1], index - 1, newArgs)
336344
end
337345
if func.special == 'xpcall' and index > 1 then
346+
if not args then
347+
return nil
348+
end
338349
local newArgs = {}
339350
for i = 3, #args do
340351
newArgs[#newArgs+1] = args[i]
341352
end
342353
return getReturn(args[1], index - 1, newArgs)
343354
end
344355
if func.special == 'require' then
356+
if not args then
357+
return nil
358+
end
345359
local nameArg = args[1]
346360
if not nameArg or nameArg.type ~= 'string' then
347361
return nil
@@ -941,9 +955,11 @@ local compilerSwitch = util.switch()
941955
-- initValue
942956
selectNode(source._iterArgs[2], source.exps, 3)
943957
end
944-
for i, loc in ipairs(source.keys) do
945-
local node = getReturn(source._iterator, i, source._iterArgs)
946-
nodeMgr.setNode(loc, node)
958+
if source.keys then
959+
for i, loc in ipairs(source.keys) do
960+
local node = getReturn(source._iterator, i, source._iterArgs)
961+
nodeMgr.setNode(loc, node)
962+
end
947963
end
948964
end)
949965
: case 'doc.type'
@@ -1360,7 +1376,7 @@ local compilerSwitch = util.switch()
13601376
if source.op.type == '//' then
13611377
local a = valueMgr.getNumber(source[1])
13621378
local b = valueMgr.getNumber(source[2])
1363-
if a and b then
1379+
if a and b and b ~= 0 then
13641380
local result = a // b
13651381
nodeMgr.setNode(source, {
13661382
type = math.type(result) == 'integer' and 'integer' or 'number',

script/vm/global-manager.lua

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ local compilerGlobalSwitch = util.switch()
7474
if parentName == '_G' then
7575
name = keyName
7676
else
77-
name = parentName .. m.ID_SPLITE .. keyName
77+
name = ('%s%s%s'):format(parentName, m.ID_SPLITE, keyName)
7878
end
7979
elseif source.node.special == '_G' then
8080
name = keyName
8181
end
8282
if not name then
8383
return
8484
end
85-
local uri = guide.getUri(source)
85+
local uri = guide.getUri(source)
8686
local global = m.declareGlobal('variable', name, uri)
8787
global:addSet(uri, source)
8888
source._globalNode = global
@@ -92,17 +92,22 @@ local compilerGlobalSwitch = util.switch()
9292
: case 'getindex'
9393
---@param source parser.object
9494
: call(function (source)
95-
local name = guide.getKeyName(source)
96-
if not name then
95+
local name
96+
local keyName = guide.getKeyName(source)
97+
if not keyName then
9798
return
9899
end
99100
if source.node._globalNode then
100101
local parentName = source.node._globalNode:getName()
101-
if parentName ~= '_G' then
102-
name = parentName .. m.ID_SPLITE .. name
102+
if parentName == '_G' then
103+
name = keyName
104+
else
105+
name = ('%s%s%s'):format(parentName, m.ID_SPLITE, keyName)
103106
end
107+
elseif source.node.special == '_G' then
108+
name = keyName
104109
end
105-
local uri = guide.getUri(source)
110+
local uri = guide.getUri(source)
106111
local global = m.declareGlobal('variable', name, uri)
107112
global:addGet(uri, source)
108113
source._globalNode = global
@@ -337,9 +342,11 @@ function m.dropUri(uri)
337342
m.globalSubs[uri] = nil
338343
for key in pairs(globalSub) do
339344
local global = m.globals[key]
340-
global:dropUri(uri)
341-
if not global:isAlive() then
342-
m.globals[key] = nil
345+
if global then
346+
global:dropUri(uri)
347+
if not global:isAlive() then
348+
m.globals[key] = nil
349+
end
343350
end
344351
end
345352
end

script/vm/local-id.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ function m.compileLocalID(source)
121121
return
122122
end
123123
local root = guide.getRoot(source)
124+
if not root then
125+
log.error('No root?', require 'inspect' (source, { depth = 1 }))
126+
return
127+
end
124128
if not root._localIDs then
125129
root._localIDs = util.multiTable(2)
126130
end

0 commit comments

Comments
 (0)