diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-09-16 16:09:02 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-09-16 16:09:02 +0800 |
commit | 3b79c23262e11d24b410bb3125a17b73c1483686 (patch) | |
tree | f7055a279af7b362d07e3cb558a607b180c5e408 | |
parent | 5737d55897545813e1f84de8adde2a046d3904f7 (diff) | |
download | lua-language-server-3b79c23262e11d24b410bb3125a17b73c1483686.zip |
any|nil -> any
-rw-r--r-- | script-beta/parser/guide.lua | 23 | ||||
-rw-r--r-- | test-beta/type_inference/init.lua | 16 |
2 files changed, 35 insertions, 4 deletions
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua index 95ca0259..4d3f88a8 100644 --- a/script-beta/parser/guide.lua +++ b/script-beta/parser/guide.lua @@ -1950,8 +1950,12 @@ end function m.mergeTypes(types) local results = {} local mark = {} + local hasAny for i = 1, #types do local tp = types[i] + if tp == 'any' then + hasAny = true + end if not mark[tp] and tp ~= 'any' then mark[tp] = true results[#results+1] = tp @@ -1961,7 +1965,11 @@ function m.mergeTypes(types) return 'any' end if #results == 1 then - return results[1] + if results[1] == 'nil' and hasAny then + return 'any' + else + return results[1] + end end tableSort(results, function (a, b) local sa = TypeSort[a] @@ -1991,7 +1999,7 @@ function m.viewInferType(infers) local types = {} for i = 1, #infers do local tp = infers[i].type or 'any' - if not mark[tp] and tp ~= 'any' then + if not mark[tp] then types[#types+1] = tp end mark[tp] = true @@ -2699,8 +2707,15 @@ local function mergeFunctionReturns(status, source, index) if rtn[index] then local newStatus = m.status(status) m.searchInfer(newStatus, rtn[index]) - for _, infer in ipairs(newStatus.results) do - status.results[#status.results+1] = infer + if #newStatus.results == 0 then + status.results[#status.results+1] = { + type = 'any', + source = rtn[index], + } + else + for _, infer in ipairs(newStatus.results) do + status.results[#status.results+1] = infer + end end end end diff --git a/test-beta/type_inference/init.lua b/test-beta/type_inference/init.lua index 937b4b86..e37054b2 100644 --- a/test-beta/type_inference/init.lua +++ b/test-beta/type_inference/init.lua @@ -172,6 +172,22 @@ end <?y?> = x() ]] +TEST 'integer|nil' [[ +local function x() + return 1 + return nil +end +<?y?> = x() +]] + +TEST 'any' [[ +local function x() + return a + return nil +end +<?y?> = x() +]] + TEST 'integer' [[ local function x() return 1 |