summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-08-02 20:06:50 +0200
committerBram Moolenaar <Bram@vim.org>2021-08-02 20:06:50 +0200
commitbb7ee7abe1ea1e60b8a5dadd2bed8cdbe4f3e8fb (patch)
tree6ad2d2081dec45e2669790281a11f7d4690cbaf3
parent35578168becd1e11973bec413f2078a4bf81ba6b (diff)
downloadvim-git-bb7ee7abe1ea1e60b8a5dadd2bed8cdbe4f3e8fb.tar.gz
patch 8.2.3276: Vim9: exists() can only be evaluated at runtimev8.2.3276
Problem: Vim9: exists() can only be evaluated at runtime. Solution: Evaluate at compile time for option name literals. (closes #8437)
-rw-r--r--src/evalfunc.c3
-rw-r--r--src/proto/evalfunc.pro1
-rw-r--r--src/testdir/test_vim9_builtin.vim19
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c15
5 files changed, 34 insertions, 6 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c
index ff0d33a4c..6db930ef2 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -49,7 +49,6 @@ static void f_escape(typval_T *argvars, typval_T *rettv);
static void f_eval(typval_T *argvars, typval_T *rettv);
static void f_eventhandler(typval_T *argvars, typval_T *rettv);
static void f_execute(typval_T *argvars, typval_T *rettv);
-static void f_exists(typval_T *argvars, typval_T *rettv);
static void f_expand(typval_T *argvars, typval_T *rettv);
static void f_expandcmd(typval_T *argvars, typval_T *rettv);
static void f_feedkeys(typval_T *argvars, typval_T *rettv);
@@ -3521,7 +3520,7 @@ f_execute(typval_T *argvars, typval_T *rettv)
/*
* "exists()" function
*/
- static void
+ void
f_exists(typval_T *argvars, typval_T *rettv)
{
char_u *p;
diff --git a/src/proto/evalfunc.pro b/src/proto/evalfunc.pro
index 67bb5e4d3..5bb38b00f 100644
--- a/src/proto/evalfunc.pro
+++ b/src/proto/evalfunc.pro
@@ -18,6 +18,7 @@ buf_T *get_buf_arg(typval_T *arg);
win_T *get_optional_window(typval_T *argvars, int idx);
void execute_redir_str(char_u *value, int value_len);
void execute_common(typval_T *argvars, typval_T *rettv, int arg_off);
+void f_exists(typval_T *argvars, typval_T *rettv);
void f_has(typval_T *argvars, typval_T *rettv);
int dynamic_feature(char_u *feature);
void mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv);
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index f4d7448f5..19baada10 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -790,6 +790,25 @@ enddef
def Test_exists()
CheckDefAndScriptFailure2(['exists(10)'], 'E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1')
call assert_equal(1, exists('&tabstop'))
+
+ if exists('+newoption')
+ if &newoption == 'ok'
+ endif
+ endif
+ if exists('&newoption')
+ if &newoption == 'ok'
+ endif
+ endif
+ if exists('+tabstop')
+ assert_equal(8, &tabstop)
+ else
+ assert_report('tabstop option not existing?')
+ endif
+ if exists('&tabstop')
+ assert_equal(8, &tabstop)
+ else
+ assert_report('tabstop option not existing?')
+ endif
enddef
def Test_expand()
diff --git a/src/version.c b/src/version.c
index fae2fd63c..879800260 100644
--- a/src/version.c
+++ b/src/version.c
@@ -756,6 +756,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3276,
+/**/
3275,
/**/
3274,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 15e6e63a8..c4eed5428 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -3395,8 +3395,10 @@ compile_call(
int is_autoload;
int is_searchpair;
- // we can evaluate "has('name')" at compile time
- if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
+ // We can evaluate "has('name')" at compile time.
+ // We can evaluate some "exists()" values at compile time.
+ if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
+ || (varlen == 6 && STRNCMP(*arg, "exists", 6) == 0))
{
char_u *s = skipwhite(*arg + varlen + 1);
typval_T argvars[2];
@@ -3408,7 +3410,9 @@ compile_call(
(void)eval_lit_string(&s, &argvars[0], TRUE);
s = skipwhite(s);
if (*s == ')' && argvars[0].v_type == VAR_STRING
- && !dynamic_feature(argvars[0].vval.v_string))
+ && ((**arg == 'h' && !dynamic_feature(argvars[0].vval.v_string))
+ || (**arg == 'e' && (*argvars[0].vval.v_string == '+'
+ || *argvars[0].vval.v_string == '&'))))
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
@@ -3416,7 +3420,10 @@ compile_call(
argvars[1].v_type = VAR_UNKNOWN;
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
- f_has(argvars, tv);
+ if (**arg == 'h')
+ f_has(argvars, tv);
+ else
+ f_exists(argvars, tv);
clear_tv(&argvars[0]);
++ppconst->pp_used;
return OK;