Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle metamethod for __eq #817

Merged
merged 1 commit into from
Oct 16, 2024
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
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
Loading