Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## 3.10.1
`2024-8-2`
Expand Down
9 changes: 9 additions & 0 deletions script/vm/operator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ vm.binarySwitch = util.switch()
})
else
local node = vm.runOperator(binaryMap[op], source[1], source[2])
if not node then
node = vm.runOperator(binaryMap[op], source[2], source[1])
end
if node then
vm.setNode(source, node)
end
Expand Down Expand Up @@ -300,6 +303,9 @@ vm.binarySwitch = util.switch()
})
else
local node = vm.runOperator(binaryMap[op], source[1], source[2])
if not node then
node = vm.runOperator(binaryMap[op], source[2], source[1])
end
if node then
vm.setNode(source, node)
return
Expand Down Expand Up @@ -396,6 +402,9 @@ vm.binarySwitch = util.switch()
return
end
local node = vm.runOperator(binaryMap[source.op.type], source[1], source[2])
if not node then
node = vm.runOperator(binaryMap[source.op.type], source[2], source[1])
end
if node then
vm.setNode(source, node)
end
Expand Down
236 changes: 236 additions & 0 deletions test/type_inference/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4192,3 +4192,239 @@ TEST 'boolean|number' [[
---@type A
local <?x?>
]]

--reverse binary operator tests

TEST 'A' [[
---@class A
---@operator add(number): A

---@type A
local x
local <?y?> = x + 1
]]

TEST 'A' [[
---@class A
---@operator add(number): A

---@type A
local x
local <?y?> = 1 + x
]]

TEST 'A' [[
---@class A
---@operator sub(number): A

---@type A
local x
local <?y?> = x - 1
]]

TEST 'A' [[
---@class A
---@operator sub(number): A

---@type A
local x
local <?y?> = 1 - x
]]

TEST 'A' [[
---@class A
---@operator mul(number): A

---@type A
local x
local <?y?> = x * 1
]]

TEST 'A' [[
---@class A
---@operator mul(number): A

---@type A
local x
local <?y?> = 1 * x
]]

TEST 'A' [[
---@class A
---@operator div(number): A

---@type A
local x
local <?y?> = x / 1
]]

TEST 'A' [[
---@class A
---@operator div(number): A

---@type A
local x
local <?y?> = 1 / x
]]

TEST 'A' [[
---@class A
---@operator idiv(number): A

---@type A
local x
local <?y?> = x // 1
]]

TEST 'A' [[
---@class A
---@operator idiv(number): A

---@type A
local x
local <?y?> = 1 // x
]]

TEST 'A' [[
---@class A
---@operator mod(number): A

---@type A
local x
local <?y?> = x % 1
]]

TEST 'A' [[
---@class A
---@operator mod(number): A

---@type A
local x
local <?y?> = 1 % x
]]

TEST 'A' [[
---@class A
---@operator pow(number): A

---@type A
local x
local <?y?> = x ^ 1
]]

TEST 'A' [[
---@class A
---@operator pow(number): A

---@type A
local x
local <?y?> = 1 ^ x
]]

TEST 'A' [[
---@class A
---@operator concat(number): A

---@type A
local x
local <?y?> = x .. 1
]]

TEST 'A' [[
---@class A
---@operator concat(number): A

---@type A
local x
local <?y?> = 1 .. x
]]

TEST 'A' [[
---@class A
---@operator band(number): A

---@type A
local x
local <?y?> = x & 1
]]

TEST 'A' [[
---@class A
---@operator band(number): A

---@type A
local x
local <?y?> = 1 & x
]]

TEST 'A' [[
---@class A
---@operator bor(number): A

---@type A
local x
local <?y?> = x | 1
]]

TEST 'A' [[
---@class A
---@operator bor(number): A

---@type A
local x
local <?y?> = 1 | x
]]

TEST 'A' [[
---@class A
---@operator bxor(number): A

---@type A
local x
local <?y?> = x ~ 1
]]

TEST 'A' [[
---@class A
---@operator bxor(number): A

---@type A
local x
local <?y?> = 1 ~ x
]]

TEST 'A' [[
---@class A
---@operator shl(number): A

---@type A
local x
local <?y?> = x << 1
]]

TEST 'A' [[
---@class A
---@operator shl(number): A

---@type A
local x
local <?y?> = 1 << x
]]

TEST 'A' [[
---@class A
---@operator shr(number): A

---@type A
local x
local <?y?> = x >> 1
]]

TEST 'A' [[
---@class A
---@operator shr(number): A

---@type A
local x
local <?y?> = 1 >> x
]]