patch 8.2.0753: Vim9: expressions are evaluated in the discovery phase
Problem: Vim9: expressions are evaluated in the discovery phase.
Solution: Bail out if an expression is not a constant. Require a type for
declared constants.
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index e85da5a..c528b80 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -494,7 +494,7 @@
def Concat(arg: string): string
return name .. arg
enddef
- let g:result = Concat('bie')
+ let g:result: string = Concat('bie')
let g:localname = name
export const CONST = 1234
@@ -1633,7 +1633,7 @@
CheckScriptFailure([
'vim9script',
'let g:var = 123',
- 'unlet g:var# comment',
+ 'unlet g:var# comment1',
], 'E108:')
CheckScriptFailure([
@@ -1643,7 +1643,7 @@
CheckScriptSuccess([
'vim9script',
- 'if 1 # comment',
+ 'if 1 # comment2',
' echo "yes"',
'elseif 2 #comment',
' echo "no"',
@@ -1652,14 +1652,14 @@
CheckScriptFailure([
'vim9script',
- 'if 1# comment',
+ 'if 1# comment3',
' echo "yes"',
'endif',
], 'E15:')
CheckScriptFailure([
'vim9script',
- 'if 0 # comment',
+ 'if 0 # comment4',
' echo "yes"',
'elseif 2#comment',
' echo "no"',
@@ -1668,23 +1668,18 @@
CheckScriptSuccess([
'vim9script',
- 'let # comment',
+ 'let v = 1 # comment5',
])
CheckScriptFailure([
'vim9script',
- 'let# comment',
- ], 'E121:')
-
- CheckScriptSuccess([
- 'vim9script',
- 'let v:version # comment',
- ])
+ 'let v = 1# comment6',
+ ], 'E15:')
CheckScriptFailure([
'vim9script',
- 'let v:version# comment',
- ], 'E121:')
+ 'let v:version',
+ ], 'E1091:')
CheckScriptSuccess([
'vim9script',
@@ -1722,6 +1717,41 @@
delete('Xfinished')
enddef
+def Test_let_func_call()
+ let lines =<< trim END
+ vim9script
+ func GetValue()
+ if exists('g:count')
+ let g:count += 1
+ else
+ let g:count = 1
+ endif
+ return 'this'
+ endfunc
+ let val: string = GetValue()
+ END
+ writefile(lines, 'Xfinished')
+ source Xfinished
+ assert_equal(1, g:count)
+
+ unlet g:count
+ delete('Xfinished')
+enddef
+
+def Test_let_missing_type()
+ let lines =<< trim END
+ vim9script
+ func GetValue()
+ return 'this'
+ endfunc
+ let val = GetValue()
+ END
+ writefile(lines, 'Xfinished')
+ assert_fails('source Xfinished', 'E1091:')
+
+ delete('Xfinished')
+enddef
+
" Keep this last, it messes up highlighting.
def Test_substitute_cmd()
new