diff options
-rw-r--r-- | .luarc.json | 1 | ||||
-rw-r--r-- | script/parser/newparser.lua | 24 | ||||
-rw-r--r-- | script/vm/runner.lua | 35 | ||||
-rw-r--r-- | test/type_inference/init.lua | 11 |
4 files changed, 52 insertions, 19 deletions
diff --git a/.luarc.json b/.luarc.json index c0b893a3..32fa50a7 100644 --- a/.luarc.json +++ b/.luarc.json @@ -10,6 +10,7 @@ "discard-returns": "Any", "redundant-parameter": "Any", "missing-parameter": "Any", + "need-check-nil": "Any", "redundant-value": "Any", "deprecated": "Any" }, diff --git a/script/parser/newparser.lua b/script/parser/newparser.lua index fe7b2343..864ffcff 100644 --- a/script/parser/newparser.lua +++ b/script/parser/newparser.lua @@ -2947,13 +2947,25 @@ local function parseReturn() end pushActionIntoCurrentChunk(rtn) for i = #Chunk, 1, -1 do - local func = Chunk[i] - if func.type == 'function' - or func.type == 'main' then - if not func.returns then - func.returns = {} + local block = Chunk[i] + if block.type == 'function' + or block.type == 'main' then + if not block.returns then + block.returns = {} end - func.returns[#func.returns+1] = rtn + block.returns[#block.returns+1] = rtn + break + end + end + for i = #Chunk, 1, -1 do + local block = Chunk[i] + if block.type == 'ifblock' + or block.type == 'elseifblock' + or block.type == 'else' then + if not block.returns then + block.returns = {} + end + block.returns[#block.returns+1] = rtn break end end diff --git a/script/vm/runner.lua b/script/vm/runner.lua index 9384ec01..5c92dcbe 100644 --- a/script/vm/runner.lua +++ b/script/vm/runner.lua @@ -180,19 +180,28 @@ function mt:_compileBlock(block) order = 3, } self:_compileNarrowByFilter(childBlock.filter, childBlock[1].start - 1) - local finalState = { - type = 'save', - pos = childBlock.finish, - order = 1, - } - finals[#finals+1] = finalState - self.steps[#self.steps+1] = finalState - self.steps[#self.steps+1] = { - type = 'load', - ref1 = outState, - pos = childBlock.finish, - order = 2, - } + if childBlock.returns then + self.steps[#self.steps+1] = { + type = 'load', + ref1 = outState, + pos = childBlock.finish, + order = 1, + } + else + local finalState = { + type = 'save', + pos = childBlock.finish, + order = 1, + } + finals[#finals+1] = finalState + self.steps[#self.steps+1] = finalState + self.steps[#self.steps+1] = { + type = 'load', + ref1 = outState, + pos = childBlock.finish, + order = 2, + } + end end end for i, final in ipairs(finals) do diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 389fb5be..64cc43a8 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -1840,6 +1840,17 @@ local x <?x?> = x or 1 ]] +TEST 'integer' [[ +---@type integer? +local x + +if not x then + return +end + +print(<?x?>) +]] + TEST 'integer' [=[ local x |