diff src/evalvars.c @ 22202:7899b4e2880c v8.2.1650

patch 8.2.1650: Vim9: result of && and || expression is not bool in script Commit: https://github.com/vim/vim/commit/c1ec0422e43720d2e96627605532ee9806c0789f Author: Bram Moolenaar <Bram@vim.org> Date: Wed Sep 9 22:27:58 2020 +0200 patch 8.2.1650: Vim9: result of && and || expression is not bool in script Problem: Vim9: result of && and || expression cannot be assigned to a bool at the script level. Solution: Add the VAR_BOOL_OK flag. Convert to bool when needed.
author Bram Moolenaar <Bram@vim.org>
date Wed, 09 Sep 2020 22:30:04 +0200
parents ee967cdcf749
children 88927d5f275d
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -778,7 +778,7 @@ ex_let(exarg_T *eap)
 	evalarg_T   evalarg;
 	int	    len = 1;
 
-	rettv.v_type = VAR_UNKNOWN;
+	CLEAR_FIELD(rettv);
 	i = FAIL;
 	if (has_assign || concat)
 	{
@@ -2935,10 +2935,12 @@ set_var(
 set_var_const(
     char_u	*name,
     type_T	*type,
-    typval_T	*tv,
+    typval_T	*tv_arg,
     int		copy,	    // make copy of value in "tv"
     int		flags)	    // LET_IS_CONST and/or LET_NO_COMMAND
 {
+    typval_T	*tv = tv_arg;
+    typval_T	bool_tv;
     dictitem_T	*di;
     char_u	*varname;
     hashtab_T	*ht;
@@ -2971,6 +2973,15 @@ set_var_const(
 				      && var_wrong_func_name(name, di == NULL))
 	return;
 
+    if (need_convert_to_bool(type, tv))
+    {
+	// Destination is a bool and the value is not, but it can be converted.
+	CLEAR_FIELD(bool_tv);
+	bool_tv.v_type = VAR_BOOL;
+	bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
+	tv = &bool_tv;
+    }
+
     if (di != NULL)
     {
 	if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
@@ -2989,7 +3000,7 @@ set_var_const(
 		    return;
 		}
 
-		// check the type
+		// check the type and adjust to bool if needed
 		if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
 		    return;
 	    }