comparison src/testdir/test_vim9_expr.vim @ 25318:24bd79082d86 v8.2.3196

patch 8.2.3196: Vim9: bool expression with numbers only fails at runtime Commit: https://github.com/vim/vim/commit/05bd9785fd0fd0102ab64554307bff0ec0ae34c1 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jul 21 21:37:28 2021 +0200 patch 8.2.3196: Vim9: bool expression with numbers only fails at runtime Problem: Vim9: bool expression with numbers only fails at runtime. Solution: Check constant to be bool at compile time. (closes https://github.com/vim/vim/issues/8603)
author Bram Moolenaar <Bram@vim.org>
date Wed, 21 Jul 2021 21:45:04 +0200
parents ec0421c25be9
children 9203b28ab453
comparison
equal deleted inserted replaced
25317:b2006e12af25 25318:24bd79082d86
370 CheckDefAndScriptFailure(lines, 'E1004:', 1) 370 CheckDefAndScriptFailure(lines, 'E1004:', 1)
371 enddef 371 enddef
372 372
373 def Test_expr2_fails() 373 def Test_expr2_fails()
374 var msg = "White space required before and after '||'" 374 var msg = "White space required before and after '||'"
375 call CheckDefAndScriptFailure(["var x = 1||2"], msg, 1) 375 call CheckDefAndScriptFailure(["var x = 1||0"], msg, 1)
376 call CheckDefAndScriptFailure(["var x = 1 ||2"], msg, 1) 376 call CheckDefAndScriptFailure(["var x = 1 ||0"], msg, 1)
377 call CheckDefAndScriptFailure(["var x = 1|| 2"], msg, 1) 377 call CheckDefAndScriptFailure(["var x = 1|| 0"], msg, 1)
378 378
379 call CheckDefFailure(["var x = false || "], 'E1097:', 3) 379 call CheckDefFailure(["var x = false || "], 'E1097:', 3)
380 call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2) 380 call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2)
381 381
382 # script does not fail, the second expression is skipped 382 # script does not fail, the second expression is skipped
384 384
385 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012:', 'E745:', 1) 385 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012:', 'E745:', 1)
386 386
387 call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) 387 call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1)
388 388
389 # TODO: should fail at compile time 389 call CheckDefAndScriptFailure2(["var x = 3 || false"], 'E1012:', 'E1023:', 1)
390 call CheckDefExecAndScriptFailure(["var x = 3 || 7"], 'E1023:', 1) 390 call CheckDefAndScriptFailure2(["var x = false || 3"], 'E1012:', 'E1023:', 1)
391 391
392 call CheckDefAndScriptFailure(["if 3"], 'E1023:', 1) 392 call CheckDefAndScriptFailure(["if 3"], 'E1023:', 1)
393 call CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2) 393 call CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2)
394 394
395 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012: Type mismatch; expected bool but got list<unknown>', 'E745:', 1) 395 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012: Type mismatch; expected bool but got list<unknown>', 'E745:', 1)
503 CheckDefAndScriptFailure(lines, 'E1004:', 1) 503 CheckDefAndScriptFailure(lines, 'E1004:', 1)
504 enddef 504 enddef
505 505
506 def Test_expr3_fails() 506 def Test_expr3_fails()
507 var msg = "White space required before and after '&&'" 507 var msg = "White space required before and after '&&'"
508 CheckDefAndScriptFailure(["var x = 1&&2"], msg, 1) 508 CheckDefAndScriptFailure(["var x = 1&&0"], msg, 1)
509 CheckDefAndScriptFailure(["var x = 1 &&2"], msg, 1) 509 CheckDefAndScriptFailure(["var x = 1 &&0"], msg, 1)
510 CheckDefAndScriptFailure(["var x = 1&& 2"], msg, 1) 510 CheckDefAndScriptFailure(["var x = 1&& 0"], msg, 1)
511 var lines =<< trim END 511 var lines =<< trim END
512 var x = 1 512 var x = 1
513 &&2 513 &&0
514 # comment 514 # comment
515 END 515 END
516 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&2"', 2) 516 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&0"', 2)
517 517
518 g:vals = [] 518 g:vals = []
519 CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) 519 CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1)
520 520
521 CheckDefExecAndScriptFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1) 521 CheckDefExecAndScriptFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1)
523 lines =<< trim END 523 lines =<< trim END
524 if 3 524 if 3
525 && true 525 && true
526 endif 526 endif
527 END 527 END
528 CheckDefExecAndScriptFailure(lines, 'E1023:', 1) 528 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1023:', 1)
529
530 lines =<< trim END
531 if true
532 && 3
533 endif
534 END
535 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1023:', 2)
529 536
530 lines =<< trim END 537 lines =<< trim END
531 if 'yes' 538 if 'yes'
532 && true 539 && true
533 endif 540 endif