Skip to content

Commit 671d952

Browse files
committed
infer unknown operation as unknown
#1996
1 parent a2cc107 commit 671d952

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

Diff for: script/vm/operator.lua

+15-14
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ vm.binarySwitch = util.switch()
249249
})
250250
else
251251
local node = vm.runOperator(binaryMap[op], source[1], source[2])
252-
vm.setNode(source, node or vm.declareGlobal('type', 'integer'))
252+
if node then
253+
vm.setNode(source, node)
254+
end
253255
end
254256
end)
255257
: case '+'
@@ -296,20 +298,17 @@ vm.binarySwitch = util.switch()
296298
local uri = guide.getUri(source)
297299
local infer1 = vm.getInfer(source[1])
298300
local infer2 = vm.getInfer(source[2])
299-
if infer1:hasType(uri, 'integer')
300-
or infer2:hasType(uri, 'integer') then
301-
if not infer1:hasType(uri, 'number')
302-
and not infer2:hasType(uri, 'number') then
303-
vm.setNode(source, vm.declareGlobal('type', 'integer'))
304-
return
305-
end
301+
if infer1:hasType(uri, 'integer')
302+
and infer2:hasType(uri, 'integer') then
303+
vm.setNode(source, vm.declareGlobal('type', 'integer'))
304+
return
305+
end
306+
if (infer1:hasType(uri, 'number') or infer1:hasType(uri, 'integer'))
307+
and (infer2:hasType(uri, 'number') or infer2:hasType(uri, 'integer')) then
308+
vm.setNode(source, vm.declareGlobal('type', 'number'))
309+
return
306310
end
307311
end
308-
if op == '//' then
309-
vm.setNode(source, node or vm.declareGlobal('type', 'integer'))
310-
return
311-
end
312-
vm.setNode(source, node or vm.declareGlobal('type', 'number'))
313312
end
314313
end)
315314
: case '..'
@@ -346,7 +345,9 @@ vm.binarySwitch = util.switch()
346345
})
347346
else
348347
local node = vm.runOperator(binaryMap[source.op.type], source[1], source[2])
349-
vm.setNode(source, node or vm.declareGlobal('type', 'string'))
348+
if node then
349+
vm.setNode(source, node)
350+
end
350351
end
351352
end)
352353
: case '>'

Diff for: test/type_inference/init.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ TEST 'boolean' [[
178178
<?x?> = a == b
179179
]]
180180

181-
TEST 'integer' [[
181+
TEST 'unknown' [[
182182
<?x?> = a << b
183183
]]
184184

185185
TEST 'integer' [[
186186
<?x?> = 1 << 2
187187
]]
188188

189-
TEST 'string' [[
189+
TEST 'unknown' [[
190190
<?x?> = a .. b
191191
]]
192192

@@ -202,7 +202,7 @@ TEST 'string' [[
202202
<?x?> = 'a' .. 1.0
203203
]]
204204

205-
TEST 'number' [[
205+
TEST 'unknown' [[
206206
<?x?> = a + b
207207
]]
208208

@@ -227,11 +227,11 @@ local a
227227
<?x?> = - a
228228
]]
229229

230-
TEST 'integer' [[
230+
TEST 'unknown' [[
231231
<?x?> = 1 + X
232232
]]
233233

234-
TEST 'number' [[
234+
TEST 'unknown' [[
235235
<?x?> = 1.0 + X
236236
]]
237237

@@ -2416,7 +2416,7 @@ local <?z?> = f()
24162416

24172417
TEST 'integer|table' [[
24182418
local function returnI()
2419-
return a + 1
2419+
return 1
24202420
end
24212421
24222422
local function f()

0 commit comments

Comments
 (0)