Files
neovim/test/functional/vimscript/let_spec.lua
Justin M. Keyes 597555ebc0 fix(options): crash on ":let &t_Co = v:true" #41152
Problem:
Assigning a Boolean or special value (v:true/v:false/v:null/v:none) to a TTY
option aborts:

    Assertion failed: (curval.type == newval.type), function ex_let_option, file vars.c, line 1408.
    3   libsystem_c.dylib   __assert_rtn + 284
    4   nvim                ex_let_one + 3308
    5   nvim                ex_let_vars + 112
    6   nvim                ex_let + 2356
    7   nvim                execute_cmd0 + 252
    8   nvim                do_cmdline + 9076
    9   nvim                call_user_func + 3320
    10  nvim                call_func + 2076
    11  nvim                get_func_tv + 696
    12  nvim                eval_func + 380
    20  nvim                eval_to_string_eap + 276
    21  nvim                eval_map_expr + 444
    22  nvim                vgetorpeek + 3172
    23  nvim                vgetc + 764

Solution:
Apply the string-type check to TTY options too, so a Boolean/special value
gives "E928: String required" instead of aborting. Valid string/number
assignments to `t_*` pseudo-options still silently no-op.
2026-08-04 11:06:47 +00:00

143 lines
4.7 KiB
Lua

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local describe, it, before_each = t.describe, t.it, t.before_each
local eq = t.eq
local clear = n.clear
local command = n.command
local eval = n.eval
local api = n.api
local exec = n.exec
local exec_capture = n.exec_capture
local expect_exit = n.expect_exit
local source = n.source
local testprg = n.testprg
before_each(clear)
describe(':let', function()
it('correctly lists variables with curly-braces', function()
api.nvim_set_var('v', { 0 })
eq('v [0]', exec_capture('let {"v"}'))
end)
it('correctly lists variables with subscript', function()
api.nvim_set_var('v', { 0 })
eq('v[0] #0', exec_capture('let v[0]'))
eq('g:["v"][0] #0', exec_capture('let g:["v"][0]'))
eq('{"g:"}["v"][0] #0', exec_capture('let {"g:"}["v"][0]'))
end)
it(':unlet self-referencing node in a List graph #6070', function()
-- :unlet-ing a self-referencing List must not allow GC on indirectly
-- referenced in-scope Lists. Before #6070 this caused use-after-free.
expect_exit(
1000,
source,
[=[
let [l1, l2] = [[], []]
echo 'l1:' . id(l1)
echo 'l2:' . id(l2)
echo ''
let [l3, l4] = [[], []]
call add(l4, l4)
call add(l4, l3)
call add(l3, 1)
call add(l2, l2)
call add(l2, l1)
call add(l1, 1)
unlet l2
unlet l4
call garbagecollect(1)
call feedkeys(":\e:echo l1 l3\n:echo 42\n:cq\n", "t")
]=]
)
end)
it('multibyte env var #8398 #9267', function()
command("let $NVIM_TEST_LET = 'AìaB'")
eq('AìaB', eval('$NVIM_TEST_LET'))
command("let $NVIM_TEST_LET = 'AaあB'")
eq('AaあB', eval('$NVIM_TEST_LET'))
local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ
.ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ
.ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]]
command("let $NVIM_TEST_LET = '" .. mbyte .. "'")
eq(mbyte, eval('$NVIM_TEST_LET'))
end)
it('multibyte env var to child process #8398 #9267', function()
local cmd_get_child_env = ("let g:env_from_child = system(['%s', 'NVIM_TEST_LET'])"):format(
testprg('printenv-test')
)
command("let $NVIM_TEST_LET = 'AìaB'")
command(cmd_get_child_env)
eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
command("let $NVIM_TEST_LET = 'AaあB'")
command(cmd_get_child_env)
eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ
.ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ
.ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]]
command("let $NVIM_TEST_LET = '" .. mbyte .. "'")
command(cmd_get_child_env)
eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
end)
it('release of list assigned to l: variable does not trigger assertion #12387, #12430', function()
source([[
func! s:f()
let l:x = [1]
let g:x = l:
endfunc
for _ in range(2)
call s:f()
endfor
call garbagecollect()
call feedkeys('i', 't')
]])
eq(1, eval('1'))
end)
it('can apply operator to boolean option', function()
eq(true, api.nvim_get_option_value('equalalways', {}))
command('let &equalalways -= 1')
eq(false, api.nvim_get_option_value('equalalways', {}))
command('let &equalalways += 1')
eq(true, api.nvim_get_option_value('equalalways', {}))
command('let &equalalways *= 1')
eq(true, api.nvim_get_option_value('equalalways', {}))
command('let &equalalways /= 1')
eq(true, api.nvim_get_option_value('equalalways', {}))
command('let &equalalways %= 1')
eq(false, api.nvim_get_option_value('equalalways', {}))
end)
it('assigning bool/special to a string option gives E928, not a crash', function()
for _, v in ipairs({ 'v:true', 'v:false', 'v:null' }) do
-- Regular string option.
eq('Vim(let):E928: String required', t.pcall_err(command, 'let &makeprg = ' .. v))
-- TTY option ("t_" pseudo-option).
eq('Vim(let):E928: String required', t.pcall_err(command, 'let &t_Co = ' .. v))
end
end)
end)
describe(':let and :const', function()
it('have the same output when called without arguments', function()
eq(exec_capture('let'), exec_capture('const'))
end)
it('can be used in sandbox', function()
exec([[
func Func()
let l:foo = 'foo'
const l:bar = 'bar'
endfunc
sandbox call Func()
]])
end)
end)