From 9510d22463055f56548ff461ccbc54caa1ba1a2f Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Sun, 11 Sep 2022 15:14:05 +0100 Subject: patch 9.0.0444: trying to declare g:variable gives confusing error Problem: Trying to declare g:variable gives confusing error. Solution: Give a better error message. (closes #11108) --- src/evalvars.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/evalvars.c') diff --git a/src/evalvars.c b/src/evalvars.c index facafc7dd2..7de785bc4b 100644 --- a/src/evalvars.c +++ b/src/evalvars.c @@ -602,6 +602,18 @@ list_script_vars(int *first) "s:", FALSE, first); } +/* + * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:". + * But only when an identifier character follows. + */ + int +is_scoped_variable(char_u *name) +{ + return vim_strchr((char_u *)"gwbt", name[0]) != NULL + && name[1] == ':' + && eval_isnamec(name[2]); +} + /* * Evaluate one Vim expression {expr} in string "p" and append the * resulting string to "gap". "p" points to the opening "{". @@ -3679,8 +3691,7 @@ set_var_const( vim9_declare_error(name); goto failed; } - if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':' - && vim_strchr((char_u *)"gwbt", name[0]) != NULL) + if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name)) // Do not make g:var, w:var, b:var or t:var final. flags &= ~ASSIGN_FINAL; -- cgit v1.2.3