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
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.![]()