• R/O
  • SSH

vim: コミット

Mirror of the Vim source from https://github.com/vim/vim


コミットメタ情報

リビジョン07e48ee8c3bb67239e9459fa9037918098bcc802 (tree)
日時2020-09-17 04:15:05
作者Bram Moolenaar <Bram@vim....>
コミッターBram Moolenaar

ログメッセージ

patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9

Commit: https://github.com/vim/vim/commit/a187c43cfe8863d48b2159d695fedcb71f8525c1
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Sep 16 21:08:28 2020 +0200

patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9
Problem: Cannot lock a variable in legacy Vim script like in Vim9.
Solution: Make ":lockvar 0" work.

変更サマリ

差分

diff -r 3d2dc70119a7 -r 07e48ee8c3bb runtime/doc/eval.txt
--- a/runtime/doc/eval.txt Wed Sep 16 18:00:04 2020 +0200
+++ b/runtime/doc/eval.txt Wed Sep 16 21:15:05 2020 +0200
@@ -12364,7 +12364,9 @@
1236412364 < is equivalent to: >
1236512365 :let x = 1
1236612366 :lockvar! x
12367-< This is useful if you want to make sure the variable
12367+< NOTE: in Vim9 script `:const` works differently, see
12368+ |vim9-const|
12369+ This is useful if you want to make sure the variable
1236812370 is not modified. If the value is a List or Dictionary
1236912371 literal then the items also cannot be changed: >
1237012372 const ll = [1, 2, 3]
@@ -12404,6 +12406,8 @@
1240412406
1240512407 [depth] is relevant when locking a |List| or
1240612408 |Dictionary|. It specifies how deep the locking goes:
12409+ 0 Lock the variable {name} but not its
12410+ value.
1240712411 1 Lock the |List| or |Dictionary| itself,
1240812412 cannot add or remove items, but can
1240912413 still change their values.
@@ -12417,7 +12421,14 @@
1241712421 |Dictionary|, one level deeper.
1241812422 The default [depth] is 2, thus when {name} is a |List|
1241912423 or |Dictionary| the values cannot be changed.
12420- *E743*
12424+
12425+ Example with [depth] 0: >
12426+ let mylist = [1, 2, 3]
12427+ lockvar 0 mylist
12428+ let mylist[0] = 77 " OK
12429+ call add(mylist, 4] " OK
12430+ let mylist = [7, 8, 9] " Error!
12431+< *E743*
1242112432 For unlimited depth use [!] and omit [depth].
1242212433 However, there is a maximum depth of 100 to catch
1242312434 loops.
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/dict.c
--- a/src/dict.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/dict.c Wed Sep 16 21:15:05 2020 +0200
@@ -1009,7 +1009,7 @@
10091009 }
10101010 else if (*action == 'f' && HI2DI(hi2) != di1)
10111011 {
1012- if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
1012+ if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10131013 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
10141014 break;
10151015 clear_tv(&di1->di_tv);
@@ -1227,7 +1227,7 @@
12271227 if (argvars[2].v_type != VAR_UNKNOWN)
12281228 semsg(_(e_toomanyarg), "remove()");
12291229 else if ((d = argvars[0].vval.v_dict) != NULL
1230- && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
1230+ && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
12311231 {
12321232 key = tv_get_string_chk(&argvars[1]);
12331233 if (key != NULL)
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/eval.c
--- a/src/eval.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/eval.c Wed Sep 16 21:15:05 2020 +0200
@@ -1055,7 +1055,8 @@
10551055 }
10561056 // existing variable, need to check if it can be changed
10571057 else if ((flags & GLV_READ_ONLY) == 0
1058- && var_check_ro(lp->ll_di->di_flags, name, FALSE))
1058+ && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1059+ || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
10591060 {
10601061 clear_tv(&var1);
10611062 return NULL;
@@ -1220,7 +1221,7 @@
12201221 semsg(_(e_letwrong), op);
12211222 return;
12221223 }
1223- if (var_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
1224+ if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
12241225 return;
12251226
12261227 if (lp->ll_range && rettv->v_type == VAR_BLOB)
@@ -1297,7 +1298,7 @@
12971298 }
12981299 *endp = cc;
12991300 }
1300- else if (var_check_lock(lp->ll_newkey == NULL
1301+ else if (value_check_lock(lp->ll_newkey == NULL
13011302 ? lp->ll_tv->v_lock
13021303 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
13031304 ;
@@ -1317,7 +1318,7 @@
13171318 */
13181319 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
13191320 {
1320- if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1321+ if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
13211322 return;
13221323 ri = ri->li_next;
13231324 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/evalvars.c
--- a/src/evalvars.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/evalvars.c Wed Sep 16 21:15:05 2020 +0200
@@ -1560,9 +1560,9 @@
15601560 *name_end = cc;
15611561 }
15621562 else if ((lp->ll_list != NULL
1563- && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1563+ && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
15641564 || (lp->ll_dict != NULL
1565- && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1565+ && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
15661566 return FAIL;
15671567 else if (lp->ll_range)
15681568 {
@@ -1573,7 +1573,7 @@
15731573 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
15741574 {
15751575 li = ll_li->li_next;
1576- if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1576+ if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
15771577 return FAIL;
15781578 ll_li = li;
15791579 ++ll_n1;
@@ -1646,7 +1646,7 @@
16461646 di = HI2DI(hi);
16471647 if (var_check_fixed(di->di_flags, name, FALSE)
16481648 || var_check_ro(di->di_flags, name, FALSE)
1649- || var_check_lock(d->dv_lock, name, FALSE))
1649+ || value_check_lock(d->dv_lock, name, FALSE))
16501650 return FAIL;
16511651
16521652 delete_var(ht, hi);
@@ -1677,9 +1677,6 @@
16771677 int cc;
16781678 dictitem_T *di;
16791679
1680- if (deep == 0) // nothing to do
1681- return OK;
1682-
16831680 if (lp->ll_tv == NULL)
16841681 {
16851682 cc = *name_end;
@@ -1710,11 +1707,16 @@
17101707 di->di_flags |= DI_FLAGS_LOCK;
17111708 else
17121709 di->di_flags &= ~DI_FLAGS_LOCK;
1713- item_lock(&di->di_tv, deep, lock, FALSE);
1710+ if (deep != 0)
1711+ item_lock(&di->di_tv, deep, lock, FALSE);
17141712 }
17151713 }
17161714 *name_end = cc;
17171715 }
1716+ else if (deep == 0)
1717+ {
1718+ // nothing to do
1719+ }
17181720 else if (lp->ll_range)
17191721 {
17201722 listitem_T *li = lp->ll_li;
@@ -3007,8 +3009,13 @@
30073009 goto failed;
30083010 }
30093011
3012+ // Check in this order for backwards compatibility:
3013+ // - Whether the variable is read-only
3014+ // - Whether the variable value is locked
3015+ // - Whether the variable is locked
30103016 if (var_check_ro(di->di_flags, name, FALSE)
3011- || var_check_lock(di->di_tv.v_lock, name, FALSE))
3017+ || value_check_lock(di->di_tv.v_lock, name, FALSE)
3018+ || var_check_lock(di->di_flags, name, FALSE))
30123019 goto failed;
30133020 }
30143021 else
@@ -3158,6 +3165,22 @@
31583165 }
31593166
31603167 /*
3168+ * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3169+ * Also give an error message.
3170+ */
3171+ int
3172+var_check_lock(int flags, char_u *name, int use_gettext)
3173+{
3174+ if (flags & DI_FLAGS_LOCK)
3175+ {
3176+ semsg(_(e_variable_is_locked_str),
3177+ use_gettext ? (char_u *)_(name) : name);
3178+ return TRUE;
3179+ }
3180+ return FALSE;
3181+}
3182+
3183+/*
31613184 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
31623185 * Also give an error message.
31633186 */
@@ -3204,12 +3227,12 @@
32043227 }
32053228
32063229 /*
3207- * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3208- * Also give an error message, using "name" or _("name") when use_gettext is
3209- * TRUE.
3230+ * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3231+ * value. Also give an error message, using "name" or _("name") when
3232+ * "use_gettext" is TRUE.
32103233 */
32113234 int
3212-var_check_lock(int lock, char_u *name, int use_gettext)
3235+value_check_lock(int lock, char_u *name, int use_gettext)
32133236 {
32143237 if (lock & VAR_LOCKED)
32153238 {
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/list.c
--- a/src/list.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/list.c Wed Sep 16 21:15:05 2020 +0200
@@ -817,7 +817,7 @@
817817 }
818818
819819 l = argvars[0].vval.v_list;
820- if (l != NULL && !var_check_lock(l->lv_lock,
820+ if (l != NULL && !value_check_lock(l->lv_lock,
821821 (char_u *)N_("flatten() argument"), TRUE)
822822 && list_flatten(l, maxdepth) == OK)
823823 copy_tv(&argvars[0], rettv);
@@ -1439,7 +1439,7 @@
14391439 int idx;
14401440
14411441 if ((l = argvars[0].vval.v_list) == NULL
1442- || var_check_lock(l->lv_lock, arg_errmsg, TRUE))
1442+ || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
14431443 return;
14441444
14451445 idx = (long)tv_get_number_chk(&argvars[1], &error);
@@ -1687,7 +1687,7 @@
16871687 else
16881688 {
16891689 l = argvars[0].vval.v_list;
1690- if (l == NULL || var_check_lock(l->lv_lock,
1690+ if (l == NULL || value_check_lock(l->lv_lock,
16911691 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
16921692 TRUE))
16931693 goto theend;
@@ -1955,13 +1955,13 @@
19551955 else if (argvars[0].v_type == VAR_LIST)
19561956 {
19571957 if ((l = argvars[0].vval.v_list) == NULL
1958- || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
1958+ || (!map && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
19591959 return;
19601960 }
19611961 else if (argvars[0].v_type == VAR_DICT)
19621962 {
19631963 if ((d = argvars[0].vval.v_dict) == NULL
1964- || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1964+ || (!map && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
19651965 return;
19661966 }
19671967 else
@@ -2004,7 +2004,7 @@
20042004
20052005 --todo;
20062006 di = HI2DI(hi);
2007- if (map && (var_check_lock(di->di_tv.v_lock,
2007+ if (map && (value_check_lock(di->di_tv.v_lock,
20082008 arg_errmsg, TRUE)
20092009 || var_check_ro(di->di_flags,
20102010 arg_errmsg, TRUE)))
@@ -2077,7 +2077,7 @@
20772077 l->lv_lock = VAR_LOCKED;
20782078 for (li = l->lv_first; li != NULL; li = nli)
20792079 {
2080- if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
2080+ if (map && value_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
20812081 break;
20822082 nli = li->li_next;
20832083 set_vim_var_nr(VV_KEY, idx);
@@ -2131,7 +2131,7 @@
21312131 if (argvars[0].v_type == VAR_LIST)
21322132 {
21332133 if ((l = argvars[0].vval.v_list) != NULL
2134- && !var_check_lock(l->lv_lock,
2134+ && !value_check_lock(l->lv_lock,
21352135 (char_u *)N_("add() argument"), TRUE)
21362136 && list_append_tv(l, &argvars[1]) == OK)
21372137 copy_tv(&argvars[0], rettv);
@@ -2139,7 +2139,7 @@
21392139 else if (argvars[0].v_type == VAR_BLOB)
21402140 {
21412141 if ((b = argvars[0].vval.v_blob) != NULL
2142- && !var_check_lock(b->bv_lock,
2142+ && !value_check_lock(b->bv_lock,
21432143 (char_u *)N_("add() argument"), TRUE))
21442144 {
21452145 int error = FALSE;
@@ -2282,7 +2282,7 @@
22822282
22832283 l1 = argvars[0].vval.v_list;
22842284 l2 = argvars[1].vval.v_list;
2285- if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
2285+ if (l1 != NULL && !value_check_lock(l1->lv_lock, arg_errmsg, TRUE)
22862286 && l2 != NULL)
22872287 {
22882288 if (argvars[2].v_type != VAR_UNKNOWN)
@@ -2318,7 +2318,7 @@
23182318
23192319 d1 = argvars[0].vval.v_dict;
23202320 d2 = argvars[1].vval.v_dict;
2321- if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
2321+ if (d1 != NULL && !value_check_lock(d1->dv_lock, arg_errmsg, TRUE)
23222322 && d2 != NULL)
23232323 {
23242324 // Check the third argument.
@@ -2402,7 +2402,7 @@
24022402 else if (argvars[0].v_type != VAR_LIST)
24032403 semsg(_(e_listblobarg), "insert()");
24042404 else if ((l = argvars[0].vval.v_list) != NULL
2405- && !var_check_lock(l->lv_lock,
2405+ && !value_check_lock(l->lv_lock,
24062406 (char_u *)N_("insert() argument"), TRUE))
24072407 {
24082408 if (argvars[2].v_type != VAR_UNKNOWN)
@@ -2475,7 +2475,7 @@
24752475 if (argvars[0].v_type != VAR_LIST)
24762476 semsg(_(e_listblobarg), "reverse()");
24772477 else if ((l = argvars[0].vval.v_list) != NULL
2478- && !var_check_lock(l->lv_lock,
2478+ && !value_check_lock(l->lv_lock,
24792479 (char_u *)N_("reverse() argument"), TRUE))
24802480 {
24812481 if (l->lv_first == &range_list_item)
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/proto/evalvars.pro
--- a/src/proto/evalvars.pro Wed Sep 16 18:00:04 2020 +0200
+++ b/src/proto/evalvars.pro Wed Sep 16 21:15:05 2020 +0200
@@ -68,9 +68,10 @@
6868 void set_var(char_u *name, typval_T *tv, int copy);
6969 void set_var_const(char_u *name, type_T *type, typval_T *tv_arg, int copy, int flags);
7070 int var_check_ro(int flags, char_u *name, int use_gettext);
71+int var_check_lock(int flags, char_u *name, int use_gettext);
7172 int var_check_fixed(int flags, char_u *name, int use_gettext);
7273 int var_wrong_func_name(char_u *name, int new_var);
73-int var_check_lock(int lock, char_u *name, int use_gettext);
74+int value_check_lock(int lock, char_u *name, int use_gettext);
7475 int valid_varname(char_u *varname);
7576 void reset_v_option_vars(void);
7677 void assert_error(garray_T *gap);
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/testdir/test_const.vim
--- a/src/testdir/test_const.vim Wed Sep 16 18:00:04 2020 +0200
+++ b/src/testdir/test_const.vim Wed Sep 16 21:15:05 2020 +0200
@@ -215,6 +215,14 @@
215215
216216 if 0 | lockvar x | endif
217217 let x = 'again'
218+
219+ let val = [1, 2, 3]
220+ lockvar 0 val
221+ let val[0] = 9
222+ call assert_equal([9, 2, 3], val)
223+ call add(val, 4)
224+ call assert_equal([9, 2, 3, 4], val)
225+ call assert_fails('let val = [4, 5, 6]', 'E1122:')
218226 endfunc
219227
220228
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/testdir/test_listdict.vim
--- a/src/testdir/test_listdict.vim Wed Sep 16 18:00:04 2020 +0200
+++ b/src/testdir/test_listdict.vim Wed Sep 16 21:15:05 2020 +0200
@@ -354,7 +354,7 @@
354354 " Locked variables
355355 func Test_list_locked_var()
356356 let expected = [
357- \ [['0000-000', 'ppppppp'],
357+ \ [['1000-000', 'ppppppF'],
358358 \ ['0000-000', 'ppppppp'],
359359 \ ['0000-000', 'ppppppp']],
360360 \ [['1000-000', 'ppppppF'],
@@ -381,7 +381,7 @@
381381 exe "unlockvar " . depth . " l"
382382 endif
383383 let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
384- call assert_equal(expected[depth][u][0], ps)
384+ call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
385385 let ps = ''
386386 try
387387 let l[1][1][0] = 99
@@ -425,7 +425,7 @@
425425 catch
426426 let ps .= 'F'
427427 endtry
428- call assert_equal(expected[depth][u][1], ps)
428+ call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth)
429429 endfor
430430 endfor
431431 call assert_fails("let x=islocked('a b')", 'E488:')
@@ -438,7 +438,7 @@
438438 " Unletting locked variables
439439 func Test_list_locked_var_unlet()
440440 let expected = [
441- \ [['0000-000', 'ppppppp'],
441+ \ [['1000-000', 'ppppppp'],
442442 \ ['0000-000', 'ppppppp'],
443443 \ ['0000-000', 'ppppppp']],
444444 \ [['1000-000', 'ppFppFp'],
@@ -466,7 +466,7 @@
466466 exe "unlockvar " . depth . " l"
467467 endif
468468 let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
469- call assert_equal(expected[depth][u][0], ps)
469+ call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
470470 let ps = ''
471471 try
472472 unlet l[2]['6'][7]
@@ -666,6 +666,9 @@
666666 call s:arg_list_test(1, 2, [3, 4], {5: 6})
667667 endfunc
668668
669+func Test_dict_item_locked()
670+endfunc
671+
669672 " Tests for reverse(), sort(), uniq()
670673 func Test_reverse_sort_uniq()
671674 let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/typval.c
--- a/src/typval.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/typval.c Wed Sep 16 21:15:05 2020 +0200
@@ -512,8 +512,8 @@
512512 default:
513513 break;
514514 }
515- return var_check_lock(tv->v_lock, name, use_gettext)
516- || (lock != 0 && var_check_lock(lock, name, use_gettext));
515+ return value_check_lock(tv->v_lock, name, use_gettext)
516+ || (lock != 0 && value_check_lock(lock, name, use_gettext));
517517 }
518518
519519 /*
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/userfunc.c
--- a/src/userfunc.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/userfunc.c Wed Sep 16 21:15:05 2020 +0200
@@ -3346,11 +3346,11 @@
33463346 if (fudi.fd_di == NULL)
33473347 {
33483348 // Can't add a function to a locked dictionary
3349- if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
3349+ if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
33503350 goto erret;
33513351 }
33523352 // Can't change an existing function if it is locked
3353- else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
3353+ else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
33543354 goto erret;
33553355
33563356 // Give the function a sequential number. Can only be used with a
diff -r 3d2dc70119a7 -r 07e48ee8c3bb src/version.c
--- a/src/version.c Wed Sep 16 18:00:04 2020 +0200
+++ b/src/version.c Wed Sep 16 21:15:05 2020 +0200
@@ -751,6 +751,8 @@
751751 static int included_patches[] =
752752 { /* Add new patch number below this line */
753753 /**/
754+ 1698,
755+/**/
754756 1697,
755757 /**/
756758 1696,
旧リポジトリブラウザで表示