comparison src/testdir/test_vim9_script.vim @ 24438:5c6ccab68d1e v8.2.2759

patch 8.2.2759: Vim9: for loop infers type of loop variable Commit: https://github.com/vim/vim/commit/f2253963c28e4791092620df6a6bb238c33168df Author: Bram Moolenaar <Bram@vim.org> Date: Tue Apr 13 20:53:13 2021 +0200 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 https://github.com/vim/vim/issues/8102)
author Bram Moolenaar <Bram@vim.org>
date Tue, 13 Apr 2021 21:00:05 +0200
parents baf4913fe21c
children d2f9bdd938fa
comparison
equal deleted inserted replaced
24437:c63e5ce43b69 24438:5c6ccab68d1e
2293 source Xvim9for.vim 2293 source Xvim9for.vim
2294 delete('Xvim9for.vim') 2294 delete('Xvim9for.vim')
2295 enddef 2295 enddef
2296 2296
2297 def Test_for_loop() 2297 def Test_for_loop()
2298 var result = '' 2298 var lines =<< trim END
2299 for cnt in range(7) 2299 var result = ''
2300 if cnt == 4 2300 for cnt in range(7)
2301 break 2301 if cnt == 4
2302 endif 2302 break
2303 if cnt == 2 2303 endif
2304 continue 2304 if cnt == 2
2305 endif 2305 continue
2306 result ..= cnt .. '_' 2306 endif
2307 endfor 2307 result ..= cnt .. '_'
2308 assert_equal('0_1_3_', result) 2308 endfor
2309 2309 assert_equal('0_1_3_', result)
2310 var concat = '' 2310
2311 for str in eval('["one", "two"]') 2311 var concat = ''
2312 concat ..= str 2312 for str in eval('["one", "two"]')
2313 endfor 2313 concat ..= str
2314 assert_equal('onetwo', concat) 2314 endfor
2315 2315 assert_equal('onetwo', concat)
2316 var total = 0 2316
2317 for nr in 2317 var total = 0
2318 [1, 2, 3] 2318 for nr in
2319 total += nr 2319 [1, 2, 3]
2320 endfor 2320 total += nr
2321 assert_equal(6, total) 2321 endfor
2322 2322 assert_equal(6, total)
2323 total = 0 2323
2324 for nr 2324 total = 0
2325 in [1, 2, 3] 2325 for nr
2326 total += nr 2326 in [1, 2, 3]
2327 endfor 2327 total += nr
2328 assert_equal(6, total) 2328 endfor
2329 2329 assert_equal(6, total)
2330 total = 0 2330
2331 for nr 2331 total = 0
2332 in 2332 for nr
2333 [1, 2, 3] 2333 in
2334 total += nr 2334 [1, 2, 3]
2335 endfor 2335 total += nr
2336 assert_equal(6, total) 2336 endfor
2337 2337 assert_equal(6, total)
2338
2339 # loop over string
2340 var res = ''
2341 for c in 'aéc̀d'
2342 res ..= c .. '-'
2343 endfor
2344 assert_equal('a-é-c̀-d-', res)
2345
2346 res = ''
2347 for c in ''
2348 res ..= c .. '-'
2349 endfor
2350 assert_equal('', res)
2351
2352 res = ''
2353 for c in test_null_string()
2354 res ..= c .. '-'
2355 endfor
2356 assert_equal('', res)
2357
2358 var foo: list<dict<any>> = [
2359 {a: 'Cat'}
2360 ]
2361 for dd in foo
2362 dd.counter = 12
2363 endfor
2364 assert_equal([{a: 'Cat', counter: 12}], foo)
2365 END
2366 CheckDefAndScriptSuccess(lines)
2367
2368 # TODO: should also work at script level
2338 var res = "" 2369 var res = ""
2339 for [n: number, s: string] in [[1, 'a'], [2, 'b']] 2370 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2340 res ..= n .. s 2371 res ..= n .. s
2341 endfor 2372 endfor
2342 assert_equal('1a2b', res) 2373 assert_equal('1a2b', res)
2343
2344 # loop over string
2345 res = ''
2346 for c in 'aéc̀d'
2347 res ..= c .. '-'
2348 endfor
2349 assert_equal('a-é-c̀-d-', res)
2350
2351 res = ''
2352 for c in ''
2353 res ..= c .. '-'
2354 endfor
2355 assert_equal('', res)
2356
2357 res = ''
2358 for c in test_null_string()
2359 res ..= c .. '-'
2360 endfor
2361 assert_equal('', res)
2362 enddef 2374 enddef
2363 2375
2364 def Test_for_loop_fails() 2376 def Test_for_loop_fails()
2365 CheckDefFailure(['for '], 'E1097:') 2377 CheckDefFailure(['for '], 'E1097:')
2366 CheckDefFailure(['for x'], 'E1097:') 2378 CheckDefFailure(['for x'], 'E1097:')
2469 END 2481 END
2470 CheckDefExecFailure(lines, 'E1017:', 1) 2482 CheckDefExecFailure(lines, 'E1017:', 1)
2471 enddef 2483 enddef
2472 2484
2473 def Test_for_loop_with_try_continue() 2485 def Test_for_loop_with_try_continue()
2474 var looped = 0 2486 var lines =<< trim END
2475 var cleanup = 0 2487 var looped = 0
2476 for i in range(3) 2488 var cleanup = 0
2477 looped += 1 2489 for i in range(3)
2478 try 2490 looped += 1
2479 eval [][0] 2491 try
2480 catch 2492 eval [][0]
2481 continue 2493 catch
2482 finally 2494 continue
2483 cleanup += 1 2495 finally
2484 endtry 2496 cleanup += 1
2485 endfor 2497 endtry
2486 assert_equal(3, looped) 2498 endfor
2487 assert_equal(3, cleanup) 2499 assert_equal(3, looped)
2500 assert_equal(3, cleanup)
2501 END
2502 CheckDefAndScriptSuccess(lines)
2488 enddef 2503 enddef
2489 2504
2490 def Test_while_loop() 2505 def Test_while_loop()
2491 var result = '' 2506 var result = ''
2492 var cnt = 0 2507 var cnt = 0