From b19f88f40eb40be9c9053e025495b9dabff69699 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 1 Aug 2024 13:05:51 -0300 Subject: [PATCH] standard library: special-case tuple support for table.unpack Add special cases for tuples of sizes up to 5. --- spec/call/generic_function_spec.lua | 4 ++-- spec/stdlib/table_spec.lua | 12 ++++++++++++ tl.lua | 4 ++++ tl.tl | 4 ++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spec/call/generic_function_spec.lua b/spec/call/generic_function_spec.lua index ec68bb3ff..d512c307c 100644 --- a/spec/call/generic_function_spec.lua +++ b/spec/call/generic_function_spec.lua @@ -494,9 +494,9 @@ describe("generic function", function() ]])) it("generic function definitions do not leak type variables (#322)", util.check([[ - local function my_unpack(_list: {T}, _x: number, _y: number): T... + local function my_move(_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" }) ]])) diff --git a/spec/stdlib/table_spec.lua b/spec/stdlib/table_spec.lua index f5f1d0ade..a360d2efb 100644 --- a/spec/stdlib/table_spec.lua +++ b/spec/stdlib/table_spec.lua @@ -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() diff --git a/tl.lua b/tl.lua index b85344f75..e550844f5 100644 --- a/tl.lua +++ b/tl.lua @@ -353,6 +353,10 @@ do remove: function({A}, ? integer): A sort: function({A}, ? SortFunction) + unpack: function({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]] + unpack: function({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]] + unpack: function({A1, A2, A3}): A1, A2, A3 --[[needs_compat]] + unpack: function({A1, A2}): A1, A2 --[[needs_compat]] unpack: function({A}, ? number, ? number): A... --[[needs_compat]] end diff --git a/tl.tl b/tl.tl index 8b8c7af3b..c4da65e8c 100644 --- a/tl.tl +++ b/tl.tl @@ -353,6 +353,10 @@ do remove: function({A}, ? integer): A sort: function({A}, ? SortFunction) + unpack: function({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]] + unpack: function({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]] + unpack: function({A1, A2, A3}): A1, A2, A3 --[[needs_compat]] + unpack: function({A1, A2}): A1, A2 --[[needs_compat]] unpack: function({A}, ? number, ? number): A... --[[needs_compat]] end