Patch 9.0.0778
Problem:    Indexing of unknown const type fails during compilation.
Solution:   Check for "any" properly. (closes #11389)
Files:      src/vim9expr.c, src/testdir/test_vim9_expr.vim


*** ../vim-9.0.0777/src/vim9expr.c      2022-09-23 12:44:19.795794418 +0100
--- src/vim9expr.c      2022-10-17 13:02:44.105398769 +0100
***************
*** 93,99 ****
      vartype = typep->type_curr->tt_type;
      idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
      // If the index is a string, the variable must be a Dict.
!     if ((typep->type_curr == &t_any || typep->type_curr == &t_unknown)
                                                       && idxtype == &t_string)
        vartype = VAR_DICT;
      if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
--- 93,100 ----
      vartype = typep->type_curr->tt_type;
      idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
      // If the index is a string, the variable must be a Dict.
!     if ((typep->type_curr->tt_type == VAR_ANY
!               || typep->type_curr->tt_type == VAR_UNKNOWN)
                                                       && idxtype == &t_string)
        vartype = VAR_DICT;
      if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
***************
*** 172,179 ****
                return FAIL;
        }
      }
!     else if (vartype == VAR_LIST || typep->type_curr == &t_any
!                                            || typep->type_curr == &t_unknown)
      {
        if (is_slice)
        {
--- 173,180 ----
                return FAIL;
        }
      }
!     else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
!                                || typep->type_curr->tt_type == VAR_UNKNOWN)
      {
        if (is_slice)
        {
***************
*** 669,675 ****
            // {sub} argument of substitute() can be compiled if it starts
            // with \=
            if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
!                   && isn->isn_arg.string[1] == '=')
                compile_string(isn, cctx, 2);
        }
  
--- 670,676 ----
            // {sub} argument of substitute() can be compiled if it starts
            // with \=
            if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
!                                             && isn->isn_arg.string[1] == '=')
                compile_string(isn, cctx, 2);
        }
  
***************
*** 1646,1655 ****
      if (type == &t_bool)
        return OK;
  
!     if (type == &t_any
!           || type == &t_unknown
!           || type == &t_number
!           || type == &t_number_bool)
        // Number 0 and 1 are OK to use as a bool.  "any" could also be a bool.
        // This requires a runtime type check.
        return generate_COND2BOOL(cctx);
--- 1647,1657 ----
      if (type == &t_bool)
        return OK;
  
!     if (type->tt_type == VAR_ANY
!           || type->tt_type == VAR_UNKNOWN
!           || type->tt_type == VAR_NUMBER
!           || type == &t_number_bool
!           || type == &t_const_number_bool)
        // Number 0 and 1 are OK to use as a bool.  "any" could also be a bool.
        // This requires a runtime type check.
        return generate_COND2BOOL(cctx);
*** ../vim-9.0.0777/src/testdir/test_vim9_expr.vim      2022-10-15 
20:06:30.284328833 +0100
--- src/testdir/test_vim9_expr.vim      2022-10-17 12:59:30.289737682 +0100
***************
*** 3132,3137 ****
--- 3132,3161 ----
    unlet g:testlist
  enddef
  
+ def Test_expr9_const_any_index_slice()
+   var lines =<< trim END
+       vim9script
+ 
+       export def V(): dict<any>
+         return {a: [1, 43], b: 0}
+       enddef
+   END
+   writefile(lines, 'XexportDict.vim', 'D')
+ 
+   lines =<< trim END
+       vim9script
+ 
+       import './XexportDict.vim' as x
+ 
+       def Test()
+         const v = x.V()
+         assert_equal(43, v.a[1])
+       enddef
+       Test()
+   END
+   v9.CheckScriptSuccess(lines)
+ enddef
+ 
  def Test_expr_member_vim9script()
    var lines =<< trim END
        var d = {one:
*** ../vim-9.0.0777/src/version.c       2022-10-16 21:43:03.386665520 +0100
--- src/version.c       2022-10-17 12:56:14.306112275 +0100
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     778,
  /**/

-- 
There are only two hard things in programming: Cache invalidation,
naming things and off-by-one errors.

 /// 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/20221017121403.739DC1C0363%40moolenaar.net.

Raspunde prin e-mail lui