Patch 8.2.2840
Problem:    Vim9: member operation not fully tested.
Solution:   Add a few tests.
Files:      src/vim9compile.c, src/testdir/test_vim9_expr.vim


*** ../vim-8.2.2839/src/vim9compile.c   2021-05-05 22:51:35.631336525 +0200
--- src/vim9compile.c   2021-05-06 20:51:04.321360475 +0200
***************
*** 2708,2748 ****
  
  /*
   * Compile getting a member from a list/dict/string/blob.  Stack has the
!  * indexable value and the index.
   */
      static int
  compile_member(int is_slice, cctx_T *cctx)
  {
      type_T    **typep;
      garray_T  *stack = &cctx->ctx_type_stack;
!     vartype_T vtype;
!     type_T    *valtype;
  
!     // We can index a list and a dict.  If we don't know the type
!     // we can use the index value type.
!     // TODO: If we don't know use an instruction to figure it out at
!     // runtime.
      typep = ((type_T **)stack->ga_data) + stack->ga_len
                                                  - (is_slice ? 3 : 2);
!     vtype = (*typep)->tt_type;
!     valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
      // If the index is a string, the variable must be a Dict.
!     if (*typep == &t_any && valtype == &t_string)
!       vtype = VAR_DICT;
!     if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
      {
!       if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
            return FAIL;
        if (is_slice)
        {
!           valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
!           if (need_type(valtype, &t_number, -2, 0, cctx,
                                                         FALSE, FALSE) == FAIL)
                return FAIL;
        }
      }
  
!     if (vtype == VAR_DICT)
      {
        if (is_slice)
        {
--- 2708,2747 ----
  
  /*
   * Compile getting a member from a list/dict/string/blob.  Stack has the
!  * indexable value and the index or the two indexes of a slice.
   */
      static int
  compile_member(int is_slice, cctx_T *cctx)
  {
      type_T    **typep;
      garray_T  *stack = &cctx->ctx_type_stack;
!     vartype_T vartype;
!     type_T    *idxtype;
  
!     // We can index a list, dict and blob.  If we don't know the type
!     // we can use the index value type.  If we still don't know use an "ANY"
!     // instruction.
      typep = ((type_T **)stack->ga_data) + stack->ga_len
                                                  - (is_slice ? 3 : 2);
!     vartype = (*typep)->tt_type;
!     idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
      // If the index is a string, the variable must be a Dict.
!     if (*typep == &t_any && idxtype == &t_string)
!       vartype = VAR_DICT;
!     if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
      {
!       if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
            return FAIL;
        if (is_slice)
        {
!           idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
!           if (need_type(idxtype, &t_number, -2, 0, cctx,
                                                         FALSE, FALSE) == FAIL)
                return FAIL;
        }
      }
  
!     if (vartype == VAR_DICT)
      {
        if (is_slice)
        {
***************
*** 2768,2774 ****
        if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
            return FAIL;
      }
!     else if (vtype == VAR_STRING)
      {
        *typep = &t_string;
        if ((is_slice
--- 2767,2773 ----
        if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
            return FAIL;
      }
!     else if (vartype == VAR_STRING)
      {
        *typep = &t_string;
        if ((is_slice
***************
*** 2776,2782 ****
                : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
            return FAIL;
      }
!     else if (vtype == VAR_BLOB)
      {
        if (is_slice)
        {
--- 2775,2781 ----
                : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
            return FAIL;
      }
!     else if (vartype == VAR_BLOB)
      {
        if (is_slice)
        {
***************
*** 2791,2802 ****
                return FAIL;
        }
      }
!     else if (vtype == VAR_LIST || *typep == &t_any)
      {
        if (is_slice)
        {
            if (generate_instr_drop(cctx,
!                    vtype == VAR_LIST ?  ISN_LISTSLICE : ISN_ANYSLICE,
                                                            2) == FAIL)
                return FAIL;
        }
--- 2790,2801 ----
                return FAIL;
        }
      }
!     else if (vartype == VAR_LIST || *typep == &t_any)
      {
        if (is_slice)
        {
            if (generate_instr_drop(cctx,
!                    vartype == VAR_LIST ?  ISN_LISTSLICE : ISN_ANYSLICE,
                                                            2) == FAIL)
                return FAIL;
        }
***************
*** 2810,2816 ****
                    *typep = &t_any;
            }
            if (generate_instr_drop(cctx,
!                vtype == VAR_LIST ?  ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
                return FAIL;
        }
      }
--- 2809,2816 ----
                    *typep = &t_any;
            }
            if (generate_instr_drop(cctx,
!                       vartype == VAR_LIST ?  ISN_LISTINDEX : ISN_ANYINDEX, 1)
!                                                                      == FAIL)
                return FAIL;
        }
      }
*** ../vim-8.2.2839/src/testdir/test_vim9_expr.vim      2021-04-28 
20:00:35.014355562 +0200
--- src/testdir/test_vim9_expr.vim      2021-05-06 21:03:47.634712949 +0200
***************
*** 3066,3071 ****
--- 3066,3075 ----
      assert_equal('ábçd', text[: 3])
      assert_equal('bçdëf', text[1 :])
      assert_equal('ábçdëf', text[:])
+ 
+     assert_equal('a', g:astring[0])
+     assert_equal('sd', g:astring[1 : 2])
+     assert_equal('asdf', g:astring[:])
    END
    CheckDefAndScriptSuccess(lines)
  
***************
*** 3135,3140 ****
--- 3139,3147 ----
        assert_equal([0], list[0 : -5])
        assert_equal([], list[0 : -6])
        assert_equal([], list[0 : -99])
+ 
+       assert_equal(2, g:alist[0])
+       assert_equal([2, 3, 4], g:alist[:])
    END
    CheckDefAndScriptSuccess(lines)
  
***************
*** 3157,3162 ****
--- 3164,3172 ----
        var res = l[0].lnum > l[1].lnum
        assert_true(res)
  
+       assert_equal(2, g:adict['aaa'])
+       assert_equal(8, g:adict.bbb)
+ 
        var dd = {}
        def Func1()
          eval dd.key1.key2
***************
*** 3168,3173 ****
--- 3178,3195 ----
    END
    CheckDefAndScriptSuccess(lines)
  enddef
+ 
+ def Test_expr7_blob_subscript()
+   var lines =<< trim END
+       var b = 0z112233
+       assert_equal(0x11, b[0])
+       assert_equal(0z112233, b[:])
+ 
+       assert_equal(0x01, g:ablob[0])
+       assert_equal(0z01ab, g:ablob[:])
+   END
+   CheckDefAndScriptSuccess(lines)
+ enddef
  
  def Test_expr7_subscript_linebreak()
    var lines =<< trim END
*** ../vim-8.2.2839/src/version.c       2021-05-06 18:46:31.039085745 +0200
--- src/version.c       2021-05-06 20:59:09.959542978 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2840,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
240. You think Webster's Dictionary is a directory of WEB sites.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202105061905.146J5Kxe1154780%40masaka.moolenaar.net.

Raspunde prin e-mail lui