Skip to content

Commit

Permalink
fix: function: do not mistake optional argument for vararg
Browse files Browse the repository at this point in the history
Fixes #826.
  • Loading branch information
hishamhm committed Oct 23, 2024
1 parent 815de09 commit 374bd99
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions spec/lang/subtyping/function_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local util = require("spec.util")

describe("subtyping of functions:", function()
it("f(a, ?b) <: f(a) (regression test 1 for #826)", util.check([[
local type shared = string
local interface test1
a: function(a:string,b:shared)
end
local record test2 is test1
a: function(a:string, b:shared, c?:string) -- Error when shared != string
end
local record test3 is test1
a: function(a:string, b:shared, c?:number) -- Error when shared != number
end
]]))

it("f(a, ?b) <: f(a) (regression test 2 for #826)", util.check([[
local type shared = number
local interface test1
a: function(a:string,b:shared)
end
local record test2 is test1
a: function(a:string, b:shared, c?:string) -- Error when shared != string
end
local record test3 is test1
a: function(a:string, b:shared, c?:number) -- Error when shared != number
end
]]))
end)
2 changes: 1 addition & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8942,7 +8942,7 @@ a.types[i], b.types[i]), }
else
for i = ((a.is_method or b.is_method) and 2 or 1), #aa do
local ai = aa[i]
local bi = ba[i] or ba[#ba]
local bi = ba[i] or (b.args.is_va and ba[#ba])
if bi then
self:arg_check(nil, errs, ai, bi, "bivariant", "argument", i)
end
Expand Down
2 changes: 1 addition & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8942,7 +8942,7 @@ do
else
for i = ((a.is_method or b.is_method) and 2 or 1), #aa do
local ai = aa[i]
local bi = ba[i] or ba[#ba]
local bi = ba[i] or (b.args.is_va and ba[#ba])
if bi then
self:arg_check(nil, errs, ai, bi, "bivariant", "argument", i)
end
Expand Down

0 comments on commit 374bd99

Please sign in to comment.