Skip to content

Commit

Permalink
standard library: special-case tuple support for table.unpack
Browse files Browse the repository at this point in the history
Add special cases for tuples of sizes up to 5.
  • Loading branch information
hishamhm committed Aug 1, 2024
1 parent 334b453 commit b19f88f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spec/call/generic_function_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ describe("generic function", function()
]]))

it("generic function definitions do not leak type variables (#322)", util.check([[
local function my_unpack<T>(_list: {T}, _x: number, _y: number): T...
local function my_move<T>(_list: {T}, _a: integer, _b: integer, _c: integer, _t?: {T}): {T}
end
local _tbl_unpack = my_unpack or table.unpack
local _tbl_move = my_move or table.move
local _map: {string:number} = setmetatable(assert({}), { __mode = "k" })
]]))

Expand Down
12 changes: 12 additions & 0 deletions spec/stdlib/table_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ describe("table", function()
local b = b as string
local c = c as number
]]))

-- standard library definition has special cases
-- for tuples of sizes up to 5
it("can unpack some tuples", util.check([[
local s = { 1234, "5678", 4566, "foo", 123 }
local a, b, c, d, e = table.unpack(s)
a = a + 1 -- number
b = b .. "!" -- string
c = c + 2 -- number
d = d .. "!" -- string
e = e + 3 -- number
]]))
end)

describe("concat", function()
Expand Down
4 changes: 4 additions & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ do
remove: function<A>({A}, ? integer): A
sort: function<A>({A}, ? SortFunction<A>)
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A>({A}, ? number, ? number): A... --[[needs_compat]]
end
Expand Down
4 changes: 4 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ do
remove: function<A>({A}, ? integer): A
sort: function<A>({A}, ? SortFunction<A>)
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A>({A}, ? number, ? number): A... --[[needs_compat]]
end
Expand Down

0 comments on commit b19f88f

Please sign in to comment.