Skip to content

Commit 8ebd5df

Browse files
committed
fix: error messages for > do not have the operands flipped
1 parent afc0b4b commit 8ebd5df

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

spec/lang/operator/ge_spec.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local util = require("spec.util")
2+
3+
describe(">=", function()
4+
it("ok", util.check([[
5+
local x = 1
6+
local y = 2
7+
local z = true
8+
if x >= y then
9+
z = false
10+
end
11+
]]))
12+
13+
it("fail", util.check_type_error([[
14+
local x = 1
15+
local y = "hello"
16+
local z = true
17+
if x >= y then
18+
z = false
19+
end
20+
]], {
21+
{ msg = "cannot use operator '>=' for types integer and string" }
22+
}))
23+
24+
it("fails with not gotcha", util.check_type_error([[
25+
local x = 10
26+
local y = 20
27+
if not x >= y then
28+
print("wat")
29+
end
30+
]], {
31+
{ msg = "cannot use operator '>=' for types boolean and integer" }
32+
}))
33+
34+
it(">= on aliases to comparable types return boolean", util.check([[
35+
local type Test = integer
36+
37+
local function f(a: Test, b: Test) : boolean
38+
return a >= b
39+
end
40+
]]))
41+
end)

spec/lang/operator/gt_spec.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local util = require("spec.util")
2+
3+
describe(">", function()
4+
it("ok", util.check([[
5+
local x = 1
6+
local y = 2
7+
local z = true
8+
if x > y then
9+
z = false
10+
end
11+
]]))
12+
13+
it("fail", util.check_type_error([[
14+
local x = 1
15+
local y = "hello"
16+
local z = true
17+
if x > y then
18+
z = false
19+
end
20+
]], {
21+
{ msg = "cannot use operator '>' for types integer and string" }
22+
}))
23+
24+
it("fails with not gotcha", util.check_type_error([[
25+
local x = 10
26+
local y = 20
27+
if not x > y then
28+
print("wat")
29+
end
30+
]], {
31+
{ msg = "cannot use operator '>' for types boolean and integer" }
32+
}))
33+
34+
it("> on aliases to comparable types return boolean", util.check([[
35+
local type Test = integer
36+
37+
local function f(a: Test, b: Test) : boolean
38+
return a > b
39+
end
40+
]]))
41+
end)

spec/lang/operator/le_spec.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local util = require("spec.util")
2+
3+
describe("<=", function()
4+
it("ok", util.check([[
5+
local x = 1
6+
local y = 2
7+
local z = true
8+
if x <= y then
9+
z = false
10+
end
11+
]]))
12+
13+
it("fail", util.check_type_error([[
14+
local x = 1
15+
local y = "hello"
16+
local z = true
17+
if x <= y then
18+
z = false
19+
end
20+
]], {
21+
{ msg = "cannot use operator '<=' for types integer and string" }
22+
}))
23+
24+
it("fails with not gotcha", util.check_type_error([[
25+
local x = 10
26+
local y = 20
27+
if not x <= y then
28+
print("wat")
29+
end
30+
]], {
31+
{ msg = "cannot use operator '<=' for types boolean and integer" }
32+
}))
33+
34+
it("<= on aliases to comparable types return boolean", util.check([[
35+
local type Test = integer
36+
37+
local function f(a: Test, b: Test) : boolean
38+
return a <= b
39+
end
40+
]]))
41+
end)

tl.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12611,6 +12611,10 @@ self:expand_type(node, values, elements) })
1261112611
end
1261212612
if mt_name then
1261312613
t, meta_on_operator = self:check_metamethod(node, mt_name, ra, rb, ua, ub, flipped)
12614+
if flipped and not meta_on_operator then
12615+
ra, rb = rb, ra
12616+
ua, ub = ub, ua
12617+
end
1261412618
end
1261512619
if not t then
1261612620
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)

tl.tl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12611,6 +12611,10 @@ do
1261112611
end
1261212612
if mt_name then
1261312613
t, meta_on_operator = self:check_metamethod(node, mt_name, ra, rb, ua, ub, flipped)
12614+
if flipped and not meta_on_operator then
12615+
ra, rb = rb, ra
12616+
ua, ub = ub, ua
12617+
end
1261412618
end
1261512619
if not t then
1261612620
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)

0 commit comments

Comments
 (0)