Mercurial > vim
comparison src/testdir/test_vim9_script.vim @ 30333:fc0830246f49 v9.0.0502
patch 9.0.0502: a closure in a nested loop in a :def function does not work
Commit: https://github.com/vim/vim/commit/cc34181f9994d64f8c8fa2f5845eaf0cc963067f
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Sep 19 15:54:34 2022 +0100
patch 9.0.0502: a closure in a nested loop in a :def function does not work
Problem: A closure in a nested loop in a :def function does not work.
Solution: Use an array of loopvars, one per loop level.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 19 Sep 2022 17:00:07 +0200 |
parents | 1358585dde2b |
children | 7fc27d7ce3b0 |
comparison
equal
deleted
inserted
replaced
30332:462d122636b3 | 30333:fc0830246f49 |
---|---|
2320 assert_equal(i .. a, flist[n]()) | 2320 assert_equal(i .. a, flist[n]()) |
2321 ++n | 2321 ++n |
2322 endfor | 2322 endfor |
2323 endfor | 2323 endfor |
2324 END | 2324 END |
2325 v9.CheckScriptSuccess(['vim9script'] + lines) | 2325 v9.CheckDefAndScriptSuccess(lines) |
2326 # FIXME: not yet right for :def | 2326 enddef |
2327 lines[14] = 'assert_equal(2 .. a, flist[n]())' | 2327 |
2328 v9.CheckDefSuccess(lines) | 2328 def Test_define_global_closure_in_loops() |
2329 var lines =<< trim END | |
2330 vim9script | |
2331 | |
2332 def Func() | |
2333 for i in range(3) | |
2334 var ii = i | |
2335 for a in ['a', 'b', 'c'] | |
2336 var aa = a | |
2337 if ii == 0 && aa == 'a' | |
2338 def g:Global_0a(): string | |
2339 return ii .. aa | |
2340 enddef | |
2341 endif | |
2342 if ii == 1 && aa == 'b' | |
2343 def g:Global_1b(): string | |
2344 return ii .. aa | |
2345 enddef | |
2346 endif | |
2347 if ii == 2 && aa == 'c' | |
2348 def g:Global_2c(): string | |
2349 return ii .. aa | |
2350 enddef | |
2351 endif | |
2352 endfor | |
2353 endfor | |
2354 enddef | |
2355 Func() | |
2356 END | |
2357 v9.CheckScriptSuccess(lines) | |
2358 assert_equal("0a", g:Global_0a()) | |
2359 assert_equal("1b", g:Global_1b()) | |
2360 assert_equal("2c", g:Global_2c()) | |
2361 | |
2362 delfunc g:Global_0a | |
2363 delfunc g:Global_1b | |
2364 delfunc g:Global_2c | |
2329 enddef | 2365 enddef |
2330 | 2366 |
2331 def Test_for_loop_fails() | 2367 def Test_for_loop_fails() |
2332 v9.CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:']) | 2368 v9.CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:']) |
2333 v9.CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:']) | 2369 v9.CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:']) |
2416 for item: dict<number> in l | 2452 for item: dict<number> in l |
2417 item->extend({s: ''}) | 2453 item->extend({s: ''}) |
2418 endfor | 2454 endfor |
2419 END | 2455 END |
2420 v9.CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>') | 2456 v9.CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>') |
2457 | |
2458 lines =<< trim END | |
2459 for a in range(3) | |
2460 while a > 3 | |
2461 for b in range(2) | |
2462 while b < 0 | |
2463 for c in range(5) | |
2464 while c > 6 | |
2465 while c < 0 | |
2466 for d in range(1) | |
2467 for e in range(3) | |
2468 while e > 3 | |
2469 endwhile | |
2470 endfor | |
2471 endfor | |
2472 endwhile | |
2473 endwhile | |
2474 endfor | |
2475 endwhile | |
2476 endfor | |
2477 endwhile | |
2478 endfor | |
2479 END | |
2480 v9.CheckDefSuccess(lines) | |
2481 | |
2482 v9.CheckDefFailure(['for x in range(3)'] + lines + ['endfor'], 'E1306:') | |
2421 enddef | 2483 enddef |
2422 | 2484 |
2423 def Test_for_loop_script_var() | 2485 def Test_for_loop_script_var() |
2424 # cannot use s:var in a :def function | 2486 # cannot use s:var in a :def function |
2425 v9.CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1254:') | 2487 v9.CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1254:') |