Skip to content

Commit 6f88913

Browse files
committed
cast global
1 parent 1ca3b7d commit 6f88913

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

script/core/diagnostics/cast-type-mismatch.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ return function (uri, callback)
1616
end
1717

1818
for _, doc in ipairs(state.ast.docs) do
19-
if doc.type == 'doc.cast' and doc.loc then
19+
if doc.type == 'doc.cast' and doc.name then
2020
await.delay()
21-
local defs = vm.getDefs(doc.loc)
21+
local defs = vm.getDefs(doc.name)
2222
local loc = defs[1]
2323
if loc then
2424
local defNode = vm.compileNode(loc)

script/core/diagnostics/unknown-cast-variable.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ return function (uri, callback)
1616
end
1717

1818
for _, doc in ipairs(state.ast.docs) do
19-
if doc.type == 'doc.cast' and doc.loc then
19+
if doc.type == 'doc.cast' and doc.name then
2020
await.delay()
21-
local defs = vm.getDefs(doc.loc)
21+
local defs = vm.getDefs(doc.name)
2222
local loc = defs[1]
2323
if not loc then
2424
callback {
25-
start = doc.loc.start,
26-
finish = doc.loc.finish,
27-
message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', doc.loc[1])
25+
start = doc.name.start,
26+
finish = doc.name.finish,
27+
message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', doc.name[1])
2828
}
2929
end
3030
end

script/parser/guide.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ local childMap = {
165165
['doc.version'] = {'#versions'},
166166
['doc.diagnostic'] = {'#names'},
167167
['doc.as'] = {'as'},
168-
['doc.cast'] = {'loc', '#casts'},
168+
['doc.cast'] = {'name', '#casts'},
169169
['doc.cast.block'] = {'extends'},
170170
['doc.operator'] = {'op', 'exp', 'extends'},
171171
}

script/parser/luadoc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ local docSwitch = util.switch()
13161316
return result
13171317
end
13181318

1319-
result.loc = loc
1319+
result.name = loc
13201320
result.finish = loc.finish
13211321

13221322
while true do

script/vm/tracer.lua

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local util = require 'utility'
88
---@field package _casts? parser.object[]
99

1010
---@class vm.tracer
11+
---@field name string
1112
---@field source parser.object
1213
---@field assigns parser.object[]
1314
---@field assignMap table<parser.object, true>
@@ -30,7 +31,7 @@ function mt:getCasts()
3031
root._casts = {}
3132
local docs = root.docs
3233
for _, doc in ipairs(docs) do
33-
if doc.type == 'doc.cast' and doc.loc then
34+
if doc.type == 'doc.cast' and doc.name then
3435
root._casts[#root._casts+1] = doc
3536
end
3637
end
@@ -113,10 +114,10 @@ function mt:collectLocal()
113114

114115
local casts = self:getCasts()
115116
for _, cast in ipairs(casts) do
116-
if cast.loc[1] == self.source[1]
117+
if cast.name[1] == self.name
117118
and cast.start > startPos
118119
and cast.finish < finishPos
119-
and guide.getLocal(self.source, self.source[1], cast.start) == self.source then
120+
and guide.getLocal(self.source, self.name, cast.start) == self.source then
120121
self.casts[#self.casts+1] = cast
121122
end
122123
end
@@ -153,6 +154,17 @@ function mt:collectGlobal()
153154
finishPos = get.finish
154155
end
155156
end
157+
158+
local casts = self:getCasts()
159+
for _, cast in ipairs(casts) do
160+
if cast.name[1] == self.name then
161+
self.casts[#self.casts+1] = cast
162+
end
163+
end
164+
165+
if #self.casts > 0 then
166+
self.fastCalc = false
167+
end
156168
end
157169

158170
---@param start integer
@@ -794,8 +806,10 @@ local function createTracer(source)
794806

795807
if source.type == 'local'
796808
or source.type == 'self' then
809+
tracer.name = source[1]
797810
tracer:collectLocal()
798811
else
812+
tracer.name = source.global:getName()
799813
tracer:collectGlobal()
800814
end
801815

test/type_inference/init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,3 +4157,12 @@ if type(X) ~= 'number' then
41574157
print(<?X?>)
41584158
end
41594159
]]
4160+
4161+
TEST 'boolean' [[
4162+
---@type number
4163+
X = Y
4164+
4165+
---@cast X boolean
4166+
4167+
print(<?X?>)
4168+
]]

0 commit comments

Comments
 (0)