Skip to content

Commit e0a4869

Browse files
authored
Merge pull request #2779 from SeanTheBuilder1/binary-ops-symmetry
feat: flip binary operator check if failed (#2777)
2 parents 1dbe571 + 8635fef commit e0a4869

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5+
* `NEW` Add support for binary metamethod on right operand [#2777](https://github.com/LuaLS/lua-language-server/pull/2777)
56

67
## 3.10.1
78
`2024-8-2`

script/vm/operator.lua

+9
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ vm.binarySwitch = util.switch()
261261
})
262262
else
263263
local node = vm.runOperator(binaryMap[op], source[1], source[2])
264+
if not node then
265+
node = vm.runOperator(binaryMap[op], source[2], source[1])
266+
end
264267
if node then
265268
vm.setNode(source, node)
266269
end
@@ -300,6 +303,9 @@ vm.binarySwitch = util.switch()
300303
})
301304
else
302305
local node = vm.runOperator(binaryMap[op], source[1], source[2])
306+
if not node then
307+
node = vm.runOperator(binaryMap[op], source[2], source[1])
308+
end
303309
if node then
304310
vm.setNode(source, node)
305311
return
@@ -396,6 +402,9 @@ vm.binarySwitch = util.switch()
396402
return
397403
end
398404
local node = vm.runOperator(binaryMap[source.op.type], source[1], source[2])
405+
if not node then
406+
node = vm.runOperator(binaryMap[source.op.type], source[2], source[1])
407+
end
399408
if node then
400409
vm.setNode(source, node)
401410
end

test/type_inference/common.lua

+236
Original file line numberDiff line numberDiff line change
@@ -4192,3 +4192,239 @@ TEST 'boolean|number' [[
41924192
---@type A
41934193
local <?x?>
41944194
]]
4195+
4196+
--reverse binary operator tests
4197+
4198+
TEST 'A' [[
4199+
---@class A
4200+
---@operator add(number): A
4201+
4202+
---@type A
4203+
local x
4204+
local <?y?> = x + 1
4205+
]]
4206+
4207+
TEST 'A' [[
4208+
---@class A
4209+
---@operator add(number): A
4210+
4211+
---@type A
4212+
local x
4213+
local <?y?> = 1 + x
4214+
]]
4215+
4216+
TEST 'A' [[
4217+
---@class A
4218+
---@operator sub(number): A
4219+
4220+
---@type A
4221+
local x
4222+
local <?y?> = x - 1
4223+
]]
4224+
4225+
TEST 'A' [[
4226+
---@class A
4227+
---@operator sub(number): A
4228+
4229+
---@type A
4230+
local x
4231+
local <?y?> = 1 - x
4232+
]]
4233+
4234+
TEST 'A' [[
4235+
---@class A
4236+
---@operator mul(number): A
4237+
4238+
---@type A
4239+
local x
4240+
local <?y?> = x * 1
4241+
]]
4242+
4243+
TEST 'A' [[
4244+
---@class A
4245+
---@operator mul(number): A
4246+
4247+
---@type A
4248+
local x
4249+
local <?y?> = 1 * x
4250+
]]
4251+
4252+
TEST 'A' [[
4253+
---@class A
4254+
---@operator div(number): A
4255+
4256+
---@type A
4257+
local x
4258+
local <?y?> = x / 1
4259+
]]
4260+
4261+
TEST 'A' [[
4262+
---@class A
4263+
---@operator div(number): A
4264+
4265+
---@type A
4266+
local x
4267+
local <?y?> = 1 / x
4268+
]]
4269+
4270+
TEST 'A' [[
4271+
---@class A
4272+
---@operator idiv(number): A
4273+
4274+
---@type A
4275+
local x
4276+
local <?y?> = x // 1
4277+
]]
4278+
4279+
TEST 'A' [[
4280+
---@class A
4281+
---@operator idiv(number): A
4282+
4283+
---@type A
4284+
local x
4285+
local <?y?> = 1 // x
4286+
]]
4287+
4288+
TEST 'A' [[
4289+
---@class A
4290+
---@operator mod(number): A
4291+
4292+
---@type A
4293+
local x
4294+
local <?y?> = x % 1
4295+
]]
4296+
4297+
TEST 'A' [[
4298+
---@class A
4299+
---@operator mod(number): A
4300+
4301+
---@type A
4302+
local x
4303+
local <?y?> = 1 % x
4304+
]]
4305+
4306+
TEST 'A' [[
4307+
---@class A
4308+
---@operator pow(number): A
4309+
4310+
---@type A
4311+
local x
4312+
local <?y?> = x ^ 1
4313+
]]
4314+
4315+
TEST 'A' [[
4316+
---@class A
4317+
---@operator pow(number): A
4318+
4319+
---@type A
4320+
local x
4321+
local <?y?> = 1 ^ x
4322+
]]
4323+
4324+
TEST 'A' [[
4325+
---@class A
4326+
---@operator concat(number): A
4327+
4328+
---@type A
4329+
local x
4330+
local <?y?> = x .. 1
4331+
]]
4332+
4333+
TEST 'A' [[
4334+
---@class A
4335+
---@operator concat(number): A
4336+
4337+
---@type A
4338+
local x
4339+
local <?y?> = 1 .. x
4340+
]]
4341+
4342+
TEST 'A' [[
4343+
---@class A
4344+
---@operator band(number): A
4345+
4346+
---@type A
4347+
local x
4348+
local <?y?> = x & 1
4349+
]]
4350+
4351+
TEST 'A' [[
4352+
---@class A
4353+
---@operator band(number): A
4354+
4355+
---@type A
4356+
local x
4357+
local <?y?> = 1 & x
4358+
]]
4359+
4360+
TEST 'A' [[
4361+
---@class A
4362+
---@operator bor(number): A
4363+
4364+
---@type A
4365+
local x
4366+
local <?y?> = x | 1
4367+
]]
4368+
4369+
TEST 'A' [[
4370+
---@class A
4371+
---@operator bor(number): A
4372+
4373+
---@type A
4374+
local x
4375+
local <?y?> = 1 | x
4376+
]]
4377+
4378+
TEST 'A' [[
4379+
---@class A
4380+
---@operator bxor(number): A
4381+
4382+
---@type A
4383+
local x
4384+
local <?y?> = x ~ 1
4385+
]]
4386+
4387+
TEST 'A' [[
4388+
---@class A
4389+
---@operator bxor(number): A
4390+
4391+
---@type A
4392+
local x
4393+
local <?y?> = 1 ~ x
4394+
]]
4395+
4396+
TEST 'A' [[
4397+
---@class A
4398+
---@operator shl(number): A
4399+
4400+
---@type A
4401+
local x
4402+
local <?y?> = x << 1
4403+
]]
4404+
4405+
TEST 'A' [[
4406+
---@class A
4407+
---@operator shl(number): A
4408+
4409+
---@type A
4410+
local x
4411+
local <?y?> = 1 << x
4412+
]]
4413+
4414+
TEST 'A' [[
4415+
---@class A
4416+
---@operator shr(number): A
4417+
4418+
---@type A
4419+
local x
4420+
local <?y?> = x >> 1
4421+
]]
4422+
4423+
TEST 'A' [[
4424+
---@class A
4425+
---@operator shr(number): A
4426+
4427+
---@type A
4428+
local x
4429+
local <?y?> = 1 >> x
4430+
]]

0 commit comments

Comments
 (0)