]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1948: Vim9: object variable "this." should only be used in constructor v9.0.1948
authorh-east <h.east.727@gmail.com>
Thu, 28 Sep 2023 20:18:19 +0000 (22:18 +0200)
committerChristian Brabandt <cb@256bit.org>
Thu, 28 Sep 2023 20:18:19 +0000 (22:18 +0200)
Problem:  Vim9: object variable "this." should only be used in
          constructor
Solution: Disallow to this in normal object methods (other than
          constructors)

closes: #13152
closes: #13212

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: h-east <h.east.727@gmail.com>
runtime/doc/tags
runtime/doc/vim9class.txt
src/errors.h
src/testdir/test_vim9_class.vim
src/userfunc.c
src/version.c

index 918e767721812729622193bf9d9f0f9145ce7507..13baf8fd17f55c12ddb142e66640c13071c3503c 100644 (file)
@@ -4483,6 +4483,7 @@ E1387     vim9class.txt   /*E1387*
 E1388  vim9class.txt   /*E1388*
 E1389  vim9class.txt   /*E1389*
 E139   message.txt     /*E139*
+E1390  vim9class.txt   /*E1390*
 E140   message.txt     /*E140*
 E1400  builtin.txt     /*E1400*
 E1401  builtin.txt     /*E1401*
index b230cb800764cb9047a0bbcbf06caf0de0022317..7b9f8b4a6c7f1541934678dbead3ccd6f74bae42 100644 (file)
@@ -216,7 +216,8 @@ see this pattern: >
              this.col = col
           enddef
         endclass
-
+<
+                                                       *E1390*
 Not only is this text you need to write, it also has the type of each
 variables twice.  Since this is so common a shorter way to write new() is
 provided: >
index 7260c30cbf738f9c7df0d9781f6fdbc916e23a60..baf36d5e4ac84960603712014c00eef427bb2d18 100644 (file)
@@ -3529,6 +3529,8 @@ EXTERN char e_public_keyword_not_supported_for_method[]
        INIT(= N_("E1388: Public keyword not supported for a method"));
 EXTERN char e_missing_name_after_implements[]
        INIT(= N_("E1389: Missing name after implements"));
+EXTERN char e_cannot_use_an_object_variable_except_with_the_new_method_str[]
+       INIT(= N_("E1390: Cannot use an object variable \"this.%s\" except with the \"new\" method"));
 #endif
 EXTERN char e_cannot_mix_positional_and_non_positional_str[]
        INIT(= N_("E1400: Cannot mix positional and non-positional arguments: %s"));
@@ -3544,4 +3546,4 @@ EXTERN char e_invalid_format_specifier_str[]
        INIT(= N_("E1405: Invalid format specifier: %s"));
 EXTERN char e_aptypes_is_null_nr_str[]
        INIT(= "E1408: Internal error: ap_types or ap_types[idx] is NULL: %d: %s");
-// E1390 - E1399 unused
+// E1391 - E1399 unused
index 2fd66c937f8ef279d321945423475e3b9c6ee669..c272aab396e2d33bfa33570788a9ca216595af0d 100644 (file)
@@ -955,6 +955,17 @@ def Test_class_new_with_object_member()
   END
   v9.CheckSourceFailure(lines, 'E1013:')
 
+  lines =<< trim END
+    vim9script
+
+    class C
+      this.str: string
+      def MethodA(this.str)
+      enddef
+    endclass
+  END
+  v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.str" except with the "new" method')
+
   lines =<< trim END
       vim9script
 
index e0c1d5f9196f6f0a23fc22c751013ca9c839200e..0f487fc1207021fcfa6009a214d83d1bc11a8f46 100644 (file)
@@ -320,6 +320,16 @@ get_function_args(
                ++p;
            char_u *argend = p;
 
+           // object variable this. can be used only in a constructor
+           if (STRNCMP(eap->arg, "new", 3) != 0)
+           {
+               c = *argend;
+               *argend = NUL;
+               semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
+               *argend = c;
+               break;
+           }
+
            if (*skipwhite(p) == '=')
            {
                char_u *defval = skipwhite(skipwhite(p) + 1);
index fb1b307138dd34b65562f012caf7fb16eda78c16..49b406781c3c1f20bfc0c22b13b297cd7aec7873 100644 (file)
@@ -699,6 +699,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1948,
 /**/
     1947,
 /**/