From cd3855fb2be78e2dc2d2ca4b8e950d9d9d9081bb Mon Sep 17 00:00:00 2001 From: phanium <91544758+phanen@users.noreply.github.com> Date: Mon, 10 Feb 2025 00:40:43 +0800 Subject: fix(lua): vim.tbl_get({}, nil, 1) should return nil #32218 Problem: `vim.tbl_get(tbl, nil, 1)` returns `tbl` itself. In this case, `keys` is not empty, but `ipairs` skips the iteration: local keys = { nil, 1 } assert(#keys == 2) for i, k in ipairs(keys) do assert(false, 'unreachable') end Solution: Use `select("#", ...)` and `select(i, ...)` to ensure consistency for count and iteration. --- runtime/lua/vim/shared.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim/shared.lua') diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index f19533f474..04a40830f7 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -526,15 +526,15 @@ end ---@param ... any Optional keys (0 or more, variadic) via which to index the table ---@return any # Nested value indexed by key (if it exists), else nil function vim.tbl_get(o, ...) - local keys = { ... } - if #keys == 0 then + local nargs = select('#', ...) + if nargs == 0 then return nil end - for i, k in ipairs(keys) do - o = o[k] --- @type any + for i = 1, nargs do + o = o[select(i, ...)] --- @type any if o == nil then return nil - elseif type(o) ~= 'table' and next(keys, i) then + elseif type(o) ~= 'table' and i ~= nargs then return nil end end -- cgit