Skip to content

Commit

Permalink
fix: error messages for > do not have the operands flipped
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Nov 16, 2024
1 parent afc0b4b commit 8ebd5df
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
41 changes: 41 additions & 0 deletions spec/lang/operator/ge_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local util = require("spec.util")

describe(">=", function()
it("ok", util.check([[
local x = 1
local y = 2
local z = true
if x >= y then
z = false
end
]]))

it("fail", util.check_type_error([[
local x = 1
local y = "hello"
local z = true
if x >= y then
z = false
end
]], {
{ msg = "cannot use operator '>=' for types integer and string" }
}))

it("fails with not gotcha", util.check_type_error([[
local x = 10
local y = 20
if not x >= y then
print("wat")
end
]], {
{ msg = "cannot use operator '>=' for types boolean and integer" }
}))

it(">= on aliases to comparable types return boolean", util.check([[
local type Test = integer
local function f(a: Test, b: Test) : boolean
return a >= b
end
]]))
end)
41 changes: 41 additions & 0 deletions spec/lang/operator/gt_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local util = require("spec.util")

describe(">", function()
it("ok", util.check([[
local x = 1
local y = 2
local z = true
if x > y then
z = false
end
]]))

it("fail", util.check_type_error([[
local x = 1
local y = "hello"
local z = true
if x > y then
z = false
end
]], {
{ msg = "cannot use operator '>' for types integer and string" }
}))

it("fails with not gotcha", util.check_type_error([[
local x = 10
local y = 20
if not x > y then
print("wat")
end
]], {
{ msg = "cannot use operator '>' for types boolean and integer" }
}))

it("> on aliases to comparable types return boolean", util.check([[
local type Test = integer
local function f(a: Test, b: Test) : boolean
return a > b
end
]]))
end)
41 changes: 41 additions & 0 deletions spec/lang/operator/le_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local util = require("spec.util")

describe("<=", function()
it("ok", util.check([[
local x = 1
local y = 2
local z = true
if x <= y then
z = false
end
]]))

it("fail", util.check_type_error([[
local x = 1
local y = "hello"
local z = true
if x <= y then
z = false
end
]], {
{ msg = "cannot use operator '<=' for types integer and string" }
}))

it("fails with not gotcha", util.check_type_error([[
local x = 10
local y = 20
if not x <= y then
print("wat")
end
]], {
{ msg = "cannot use operator '<=' for types boolean and integer" }
}))

it("<= on aliases to comparable types return boolean", util.check([[
local type Test = integer
local function f(a: Test, b: Test) : boolean
return a <= b
end
]]))
end)
4 changes: 4 additions & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12611,6 +12611,10 @@ self:expand_type(node, values, elements) })
end
if mt_name then
t, meta_on_operator = self:check_metamethod(node, mt_name, ra, rb, ua, ub, flipped)
if flipped and not meta_on_operator then
ra, rb = rb, ra
ua, ub = ub, ua
end
end
if not t then
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)
Expand Down
4 changes: 4 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -12611,6 +12611,10 @@ do
end
if mt_name then
t, meta_on_operator = self:check_metamethod(node, mt_name, ra, rb, ua, ub, flipped)
if flipped and not meta_on_operator then
ra, rb = rb, ra
ua, ub = ub, ua
end
end
if not t then
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)
Expand Down

0 comments on commit 8ebd5df

Please sign in to comment.