changeset 7730:80ce794827c4 v7.4.1163

commit https://github.com/vim/vim/commit/17a13437c9414a8693369a97f3be2fc8ad48c12e Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jan 24 14:22:10 2016 +0100 patch 7.4.1163 Problem: Expressions "0 + v:true" and "'' . v:true" cause an error. Solution: Return something sensible when using a special variable as a number or as a string. (suggested by Damien)
author Christian Brabandt <cb@256bit.org>
date Sun, 24 Jan 2016 14:30:04 +0100
parents 0f5ca5591cda
children c0b0e9255b13
files src/eval.c src/testdir/test_viml.vim src/version.c
diffstat 3 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -7820,6 +7820,20 @@ failret:
     return OK;
 }
 
+    static char *
+get_var_special_name(int nr)
+{
+    switch (nr)
+    {
+	case VVAL_FALSE: return "false";
+	case VVAL_TRUE: return "true";
+	case VVAL_NONE: return "none";
+	case VVAL_NULL: return "null";
+    }
+    EMSG2(_(e_intern2), "get_var_special_name()");
+    return "42";
+}
+
 /*
  * Return a string with the string representation of a variable.
  * If the memory is allocated "tofree" is set to it, otherwise NULL.
@@ -7914,14 +7928,7 @@ echo_string(tv, tofree, numbuf, copyID)
 
 	case VAR_SPECIAL:
 	    *tofree = NULL;
-	    switch (tv->vval.v_number)
-	    {
-		case VVAL_FALSE: r = (char_u *)"false"; break;
-		case VVAL_TRUE: r = (char_u *)"true"; break;
-		case VVAL_NONE: r = (char_u *)"none"; break;
-		case VVAL_NULL: r = (char_u *)"null"; break;
-		default: EMSG2(_(e_intern2), "echo_string(special)");
-	    }
+	    r = (char_u *)get_var_special_name(tv->vval.v_number);
 	    break;
 
 	default:
@@ -21704,6 +21711,9 @@ get_tv_number_chk(varp, denote)
 	case VAR_DICT:
 	    EMSG(_("E728: Using a Dictionary as a Number"));
 	    break;
+	case VAR_SPECIAL:
+	    return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
+	    break;
 	default:
 	    EMSG2(_(e_intern2), "get_tv_number()");
 	    break;
@@ -21859,6 +21869,10 @@ get_tv_string_buf_chk(varp, buf)
 	    if (varp->vval.v_string != NULL)
 		return varp->vval.v_string;
 	    return (char_u *)"";
+	case VAR_SPECIAL:
+	    STRCPY(buf, get_var_special_name(varp->vval.v_number));
+	    return buf;
+
 	default:
 	    EMSG2(_(e_intern2), "get_tv_string_buf()");
 	    break;
--- a/src/testdir/test_viml.vim
+++ b/src/testdir/test_viml.vim
@@ -936,6 +936,16 @@ func Test_type()
     call assert_equal(6, type(v:true))
     call assert_equal(7, type(v:none))
     call assert_equal(7, type(v:null))
+
+    call assert_equal(0, 0 + v:false)
+    call assert_equal(1, 0 + v:true)
+    call assert_equal(0, 0 + v:none)
+    call assert_equal(0, 0 + v:null)
+
+    call assert_equal('false', '' . v:false)
+    call assert_equal('true', '' . v:true)
+    call assert_equal('none', '' . v:none)
+    call assert_equal('null', '' . v:null)
 endfunc
 
 "-------------------------------------------------------------------------------
--- a/src/version.c
+++ b/src/version.c
@@ -742,6 +742,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1163,
+/**/
     1162,
 /**/
     1161,