[vim/vim] vim9script: Ex command :finish in if-block (#6123)

41 views
Skip to first unread message

alexaschee

unread,
May 23, 2020, 12:17:07 AM5/23/20
to vim/vim, Subscribed

Describe the bug
In if-endif block:
If TRUE, both :fini and fini work;
if FALSE, :fini works, but not fini - E117: Missing :endif.

Seems this only happens with fini; other Ex commands work well.

To Reproduce

vim9script
if 0
    echo ":: if-block"
    fini
endif
echo ":: Finally"

Environment (please complete the following information):

  • Vim version [8.2.813]
  • OS: Windows 10


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,
May 23, 2020, 7:58:05 AM5/23/20
to vim/vim, Subscribed


> **Describe the bug**

> In if-endif block:
> If TRUE, both `:fini` and `fini` work;
> if FALSE, `:fini` works, but not `fini` - `E117: Missing :endif`.

>
> Seems this only happens with `fini`; other Ex commands work well.
>
> **To Reproduce**

>
> vim9script
> if 0
> echo ":: if-block"
> fini
> endif
> echo ":: Finally"

This behavior is documented: help vim9-discovery

This is not ideal, but it's hard to make this work predictably.

Keep in mind that "finish" is a bit of a weird thing. You never see
that in a C or Java file. Why would you want the file to continue but
not use it?

A more realistic example is when defining a function in two variants:

if condition
def TheFunc()
... solution A
enddef
else
def TheFunc()
... solution B
enddef
endif

It has the same problem: in the discovery phase evaluating "condition"
may not work. It might be possible to do when it is a constant or uses
"has('feature')". Defenitely not if it calls a function, since that
might have side effects.

Now, the question is if we should evaluate "condition" in the discovery
phase, and what to do if it's not a constant? This may lead to
unpredictable behavior. I haven't found a good choice yet.

For now, let's approach it as a normal code file, and ignore those
conditions, parse all the lines. Since it only looks at the start of
the line, you can make a workaround like:
if condition | def TheFunction()
... solution A
else | def TheFunction()
... solution B
endif

This doesn't work, since the functions are not found and won't be
compiled in the next phase.

Not sure if there is any solution in this direction. The alternative
would be to only compile a function once it is called. You would get
any errors only then, not when defining the function. We would need to
fall back to this if we can't make the discovery phase work.

--
"Hit any key to continue" it said, but nothing happened after F sharp.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Bram Moolenaar

unread,
May 24, 2020, 1:26:50 PM5/24/20
to vim...@googlegroups.com, Bram Moolenaar
The more I think about this, the more I tend to go for the postponed
compilation.

The discovery phase has some obvious flaws, as mentioned above. It's
quite a big deviation from the previous Vim script behavior, where you
could use "if condition" to skip parts of the script, or chose to define
a function in a separate way. And the problem that started this thread:
Finish the script.

I don't think the discovery phase can be made to work well enough to
behave predictably.

The alternative, not compile functions right away, is not ideal, but we
can deal with the disadvantage. At least it's similar to what happened
before: An error in a function is only found when calling that function.

We can make it a bit better by adding a :compile command. This will
compile all the functions in the current script. Then errors will be
reported early. It won't behave very different from the using the
discovery + compile phases.

Another advantage is that when a script is sourced but functions are
never actually called we don't pay the price of compilation. That makes
sourcing a script a bit faster.

--
hundred-and-one symptoms of being an internet addict:
172. You join listservers just for the extra e-mail.

lacygoill

unread,
Jun 2, 2021, 8:45:38 AM6/2/21
to vim/vim, Subscribed

I guess the issue can be closed, because the code no longer gives E171 since 8.2.0818, and there is no relevant reference at :h todo.

K.Takata

unread,
Jun 2, 2021, 8:49:41 AM6/2/21
to vim/vim, Subscribed

Closed #6123.

Reply all
Reply to author
Forward
0 new messages