Skip to content

Commit 73f8157

Browse files
committed
special treats for do
1 parent 98589dd commit 73f8157

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

script/vm/tracer.lua

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local guide = require 'parser.guide'
99
---@class vm.tracer
1010
---@field source parser.object
1111
---@field assigns parser.object[]
12-
---@field nodes table<parser.object, vm.node>
12+
---@field nodes table<parser.object, vm.node|false>
1313
---@field main parser.object
1414
---@field uri uri
1515
local mt = {}
@@ -34,15 +34,20 @@ end
3434
---@param mark table
3535
function mt:collectBlock(obj, mark)
3636
while true do
37+
obj = obj.parent
3738
if mark[obj] then
3839
return
3940
end
40-
mark[obj] = true
41-
self.assigns[#self.assigns+1] = obj
41+
if not guide.isBlockType(obj) then
42+
return
43+
end
4244
if obj == self.main then
4345
return
4446
end
45-
obj = obj.parent
47+
mark[obj] = true
48+
if obj.type ~= 'do' then
49+
self.assigns[#self.assigns+1] = obj
50+
end
4651
end
4752
end
4853

@@ -78,29 +83,46 @@ end
7883
---@return parser.object?
7984
function mt:getLastAssign(source)
8085
local assign = self.source
86+
local block = guide.getParentBlock(source)
87+
if not block then
88+
return nil
89+
end
8190
for _, obj in ipairs(self.assigns) do
82-
if obj.start > source.start then
91+
if obj.start >= source.start then
8392
break
8493
end
85-
assign = obj
94+
local objBlock = guide.getParentBlock(obj)
95+
if not objBlock then
96+
break
97+
end
98+
if objBlock == block
99+
or objBlock.type == 'do'
100+
or objBlock.finish > block.finish then
101+
assign = obj
102+
end
86103
end
87104
return assign
88105
end
89106

90107
---@param source parser.object
91108
---@return vm.node?
92109
function mt:getNode(source)
93-
if self.nodes[source] then
94-
return self.nodes[source]
110+
if self.nodes[source] ~= nil then
111+
return self.nodes[source] or nil
95112
end
96113
local lastAssign = self:getLastAssign(source)
97114
if not lastAssign then
115+
self.nodes[source] = false
98116
return nil
99117
end
100118
if guide.isSet(lastAssign) then
101119
local lastNode = vm.compileNode(lastAssign)
120+
self.nodes[source] = lastNode
102121
return lastNode
103122
end
123+
local lastNode = self:getNode(lastAssign)
124+
self.nodes[source] = lastNode
125+
return lastNode
104126
end
105127

106128
---@param source parser.object

test/type_inference/init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,15 @@ x = '1'
17581758
x = 1
17591759
]]
17601760

1761+
TEST 'integer' [[
1762+
local x
1763+
x = true
1764+
do
1765+
x = 1
1766+
end
1767+
print(<?x?>)
1768+
]]
1769+
17611770
TEST 'integer?' [[
17621771
---@type integer?
17631772
local <?x?>

0 commit comments

Comments
 (0)