Skip to content

Commit

Permalink
fix: handle metamethod for __eq
Browse files Browse the repository at this point in the history
Fixes #814.
  • Loading branch information
hishamhm committed Oct 13, 2024
1 parent ba54466 commit 62ccc2f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
50 changes: 50 additions & 0 deletions spec/lang/code_gen/macroexp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,55 @@ describe("macroexp code generation", function()
print("yes")
end
]]))

it("can resolve __eq metamethod (regression test for #814)", util.gen([[
local type R = record
x: number
y: number
metamethod __lt: function(a: R, b: R) = macroexp(a: R, b: R)
return a.x < b.x
end
metamethod __eq: function(a: R, b: R) = macroexp(a: R, b: R)
return a.x == b.x
end
end
local r: R = { x = 10, y = 20 }
local s: R = { x = 10, y = 0 }
if r > s then
print("yes")
end
if r == s then
print("the 'x' fields are equal")
end
]], [[
local r = { x = 10, y = 20 }
local s = { x = 10, y = 0 }
if s.x < r.x then
print("yes")
end
if r.x == s.x then
print("the 'x' fields are equal")
end
]]))
end)

Empty file.
6 changes: 3 additions & 3 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12399,9 +12399,9 @@ self:expand_type(node, values, elements) })
end

if node.op.op == "==" or node.op.op == "~=" then



if is_lua_table_type(ra) and is_lua_table_type(rb) then
self:check_metamethod(node, binop_to_metamethod[node.op.op], ra, rb, ua, ub)
end

if ra.typename == "enum" and rb.typename == "string" then
if not (rb.literal and ra.enumset[rb.literal]) then
Expand Down
6 changes: 3 additions & 3 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -12399,9 +12399,9 @@ do
end

if node.op.op == "==" or node.op.op == "~=" then
-- if is_lua_table_type(ra) and is_lua_table_type(rb) then
-- self:check_metamethod(node, binop_to_metamethod[node.op.op], ra, rb)
-- end
if is_lua_table_type(ra) and is_lua_table_type(rb) then
self:check_metamethod(node, binop_to_metamethod[node.op.op], ra, rb, ua, ub)
end

if ra is EnumType and rb is StringType then
if not (rb.literal and ra.enumset[rb.literal]) then
Expand Down

0 comments on commit 62ccc2f

Please sign in to comment.