0
0
mirror of https://github.com/vim/vim.git synced 2025-10-01 04:54:07 -04:00

patch 8.2.2759: Vim9: for loop infers type of loop variable

Problem:    Vim9: for loop infers type of loop variable.
Solution:   Do not get the member type. (closes #8102)
This commit is contained in:
Bram Moolenaar
2021-04-13 20:53:13 +02:00
parent f48b2fa33c
commit f2253963c2
8 changed files with 112 additions and 85 deletions

View File

@@ -2295,70 +2295,82 @@ def Test_for_outside_of_function()
enddef
def Test_for_loop()
var result = ''
for cnt in range(7)
if cnt == 4
break
endif
if cnt == 2
continue
endif
result ..= cnt .. '_'
endfor
assert_equal('0_1_3_', result)
var lines =<< trim END
var result = ''
for cnt in range(7)
if cnt == 4
break
endif
if cnt == 2
continue
endif
result ..= cnt .. '_'
endfor
assert_equal('0_1_3_', result)
var concat = ''
for str in eval('["one", "two"]')
concat ..= str
endfor
assert_equal('onetwo', concat)
var concat = ''
for str in eval('["one", "two"]')
concat ..= str
endfor
assert_equal('onetwo', concat)
var total = 0
for nr in
[1, 2, 3]
total += nr
endfor
assert_equal(6, total)
var total = 0
for nr in
[1, 2, 3]
total += nr
endfor
assert_equal(6, total)
total = 0
for nr
in [1, 2, 3]
total += nr
endfor
assert_equal(6, total)
total = 0
for nr
in [1, 2, 3]
total += nr
endfor
assert_equal(6, total)
total = 0
for nr
in
[1, 2, 3]
total += nr
endfor
assert_equal(6, total)
total = 0
for nr
in
[1, 2, 3]
total += nr
endfor
assert_equal(6, total)
# loop over string
var res = ''
for c in 'aéc̀d'
res ..= c .. '-'
endfor
assert_equal('a-é-c̀-d-', res)
res = ''
for c in ''
res ..= c .. '-'
endfor
assert_equal('', res)
res = ''
for c in test_null_string()
res ..= c .. '-'
endfor
assert_equal('', res)
var foo: list<dict<any>> = [
{a: 'Cat'}
]
for dd in foo
dd.counter = 12
endfor
assert_equal([{a: 'Cat', counter: 12}], foo)
END
CheckDefAndScriptSuccess(lines)
# TODO: should also work at script level
var res = ""
for [n: number, s: string] in [[1, 'a'], [2, 'b']]
res ..= n .. s
endfor
assert_equal('1a2b', res)
# loop over string
res = ''
for c in 'aéc̀d'
res ..= c .. '-'
endfor
assert_equal('a-é-c̀-d-', res)
res = ''
for c in ''
res ..= c .. '-'
endfor
assert_equal('', res)
res = ''
for c in test_null_string()
res ..= c .. '-'
endfor
assert_equal('', res)
enddef
def Test_for_loop_fails()
@@ -2471,20 +2483,23 @@ def Test_for_loop_unpack()
enddef
def Test_for_loop_with_try_continue()
var looped = 0
var cleanup = 0
for i in range(3)
looped += 1
try
eval [][0]
catch
continue
finally
cleanup += 1
endtry
endfor
assert_equal(3, looped)
assert_equal(3, cleanup)
var lines =<< trim END
var looped = 0
var cleanup = 0
for i in range(3)
looped += 1
try
eval [][0]
catch
continue
finally
cleanup += 1
endtry
endfor
assert_equal(3, looped)
assert_equal(3, cleanup)
END
CheckDefAndScriptSuccess(lines)
enddef
def Test_while_loop()