[vim/vim] Vim9: confusing error message when using "=" instead of "==" in comparison (#7795)

24 views
Skip to first unread message

lacygoill

unread,
Feb 7, 2021, 3:21:29 AM2/7/21
to vim/vim, Subscribed

Describe the bug

In Vim9 script, using the assignment operator = in a comparison gives a confusing error message.

To Reproduce

Run this shell command:

vim -Nu NONE -S <(cat <<'EOF'

    vim9

    def Func()

        var x = ''

        if x = ''

        endif

    enddef

    defcompile

EOF

)

E1012 is raised:

E1012: Type mismatch; expected bool but got string

Expected behavior

A different error is raised. Maybe E15:

E15: Invalid expression: x = ''

Environment

  • Vim version: 8.2 Included patches: 1-2475
  • OS: Ubuntu 16.04.7 LTS
  • Terminal: xterm(365)

Additional context

When reading E1012, my first reaction is to try and fix the type of x. But that's not the issue. The issue comes from the = assignment operator on the third line, which should be replaced with the == comparison operator.

if x = ''

     ^

     ✘



if x == ''

     ^^

     ✔


At the script level, E15 is raised:

vim9

var x = ''

if x = ''

endif
E15: Invalid expression: x = ''

If we replace the empty string with 0 in the assignment, E15 is still raised:

vim9

var x = 0

if x = ''

endif
E15: Invalid expression: x = ''

But if we wrap the code in a function, a different error is raised:

vim9

def Func()

    var x = 0

    if x = ''

    endif

enddef

defcompile
E488: Trailing characters: = ''

Finally, in Vim script legacy, E15 is raised (at the script level, and in a function):

let x = ''

if x = ''

endif
E15: Invalid expression: x = ''

All of this is inconsistent. I think E15 should always be raised; not E488, nor E1012.


I wonder whether it would make sense to create a dedicated error for such a common mistake:

E1234: Cannot compare operands with "="


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Bram Moolenaar

unread,
Feb 7, 2021, 10:34:20 AM2/7/21
to vim/vim, Subscribed

Closed #7795 via 6628b7e.

Reply all
Reply to author
Forward
0 new messages