Skip to content

Commit f07a1ed

Browse files
h-eastchrisbra
authored andcommitted
patch 9.2.0301: Vim9: void function return value inconsistent
Problem: Vim9: void function return value inconsistent between script and :def Solution: Make void built-in functions like bufload() return void consistently (Hirohito Higashi) In Vim9 script, calling a void built-in function (e.g. bufload()) at the script level did not set rettv to VAR_VOID, making it appear to return 0. Inside :def it correctly returned VAR_VOID and raised E1031. Set rettv to VAR_VOID after calling a ret_void built-in function in Vim9 script so the behavior is consistent. Also fix the documentation for bufload() and ch_logfile() to correctly state that the return type is void. closes: #19919 Signed-off-by: Hirohito Higashi <[email protected]> Signed-off-by: Yegappan Lakshmanan <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 9e04145 commit f07a1ed

6 files changed

Lines changed: 49 additions & 8 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.2. Last change: 2026 Mar 25
1+
*builtin.txt* For Vim version 9.2. Last change: 2026 Apr 05
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -82,7 +82,7 @@ browsedir({title}, {initdir}) String put up a directory requester
8282
bufadd({name}) Number add a buffer to the buffer list
8383
bufexists({buf}) Number |TRUE| if buffer {buf} exists
8484
buflisted({buf}) Number |TRUE| if buffer {buf} is listed
85-
bufload({buf}) Number load buffer {buf} if not loaded yet
85+
bufload({buf}) none load buffer {buf} if not loaded yet
8686
bufloaded({buf}) Number |TRUE| if buffer {buf} is loaded
8787
bufname([{buf}]) String name of the buffer {buf}
8888
bufnr([{buf} [, {create}]]) Number number of the buffer {buf}
@@ -1500,7 +1500,7 @@ bufload({buf}) *bufload()*
15001500
Can also be used as a |method|: >
15011501
eval 'somename'->bufload()
15021502
<
1503-
Return type: |Number|
1503+
Return type: void
15041504

15051505

15061506
bufloaded({buf}) *bufloaded()*

runtime/doc/channel.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*channel.txt* For Vim version 9.2. Last change: 2026 Mar 13
1+
*channel.txt* For Vim version 9.2. Last change: 2026 Apr 05
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -736,7 +736,7 @@ ch_logfile({fname} [, {mode}]) *ch_logfile()*
736736
Can also be used as a |method|: >
737737
'logfile'->ch_logfile('w')
738738
<
739-
Return type: |Number|
739+
Return type: void
740740

741741
ch_open({address} [, {options}]) *ch_open()*
742742
Open a channel to {address}. See |channel|.

src/evalfunc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,6 +3499,8 @@ call_internal_func(
34993499
return FCERR_OTHER;
35003500
argvars[argcount].v_type = VAR_UNKNOWN;
35013501
global_functions[i].f_func(argvars, rettv);
3502+
if (in_vim9script() && global_functions[i].f_retfunc == ret_void)
3503+
rettv->v_type = VAR_VOID;
35023504
return FCERR_NONE;
35033505
}
35043506

@@ -3509,6 +3511,8 @@ call_internal_func_by_idx(
35093511
typval_T *rettv)
35103512
{
35113513
global_functions[idx].f_func(argvars, rettv);
3514+
if (in_vim9script() && global_functions[idx].f_retfunc == ret_void)
3515+
rettv->v_type = VAR_VOID;
35123516
}
35133517

35143518
/*

src/testdir/test_vim9_builtin.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4089,8 +4089,8 @@ enddef
40894089

40904090
def Test_setenv()
40914091
v9.CheckSourceDefAndScriptFailure(['setenv(1, 2)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1'])
4092-
assert_equal(0, setenv('', ''))
4093-
assert_equal(0, setenv('', v:null))
4092+
setenv('', '')
4093+
setenv('', v:null)
40944094
enddef
40954095

40964096
def Test_setfperm()
@@ -4939,7 +4939,7 @@ enddef
49394939

49404940
def Test_timer_stop()
49414941
v9.CheckSourceDefAndScriptFailure(['timer_stop("x")'], ['E1013: Argument 1: type mismatch, expected number but got string', 'E1210: Number required for argument 1'])
4942-
assert_equal(0, timer_stop(100))
4942+
timer_stop(100)
49434943
enddef
49444944

49454945
def Test_tolower()

src/testdir/test_vim9_func.vim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5002,6 +5002,41 @@ def Test_void_method_chain()
50025002
defcompile TestFunc
50035003
END
50045004
v9.CheckScriptFailure(lines, 'E1031: Cannot use void value')
5005+
5006+
#### Case 4: Script-level and :def should behave the same ####
5007+
# script-level: void built-in assigned to variable
5008+
lines =<< trim END
5009+
vim9script
5010+
var x = bufload('')
5011+
END
5012+
v9.CheckScriptFailure(lines, 'E1031: Cannot use void value')
5013+
5014+
# inside def: same error
5015+
lines =<< trim END
5016+
vim9script
5017+
def TestFunc()
5018+
var x = bufload('')
5019+
enddef
5020+
TestFunc()
5021+
END
5022+
v9.CheckScriptFailure(lines, 'E1031: Cannot use void value')
5023+
5024+
# script-level: echo void built-in
5025+
lines =<< trim END
5026+
vim9script
5027+
echo bufload('')
5028+
END
5029+
v9.CheckScriptFailure(lines, 'E1186: Expression does not result in a value: bufload(')
5030+
5031+
# inside def: compile-time error
5032+
lines =<< trim END
5033+
vim9script
5034+
def TestFunc()
5035+
echo bufload('')
5036+
enddef
5037+
TestFunc()
5038+
END
5039+
v9.CheckScriptFailure(lines, 'E1186: Expression does not result in a value: bufload(')
50055040
enddef
50065041

50075042
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
301,
737739
/**/
738740
300,
739741
/**/

0 commit comments

Comments
 (0)