[vim/vim] [vim9script] unexpected behavior accessing `export const` variable from same file (Issue #15132)

24 views
Skip to first unread message

errael

unread,
Jun 30, 2024, 10:51:42 PM (2 days ago) Jun 30
to vim/vim, Subscribed

Steps to reproduce

vim9script

def F1()
    export const val1 = 'foo1'
enddef

def F2(): dict<any>
    return {k: val1}
enddef

def F3()
    F1()
    F2()
enddef

F3()

produces

Error detected while compiling :source buffer=1[16]..function <SNR>38_F3[2]..<SNR>38_F2:
line    1:
E1001: Variable not found: val1

The following with a twist works

vim9script

def F1()
    export const val1 = 'foo1'
enddef

def F2(): dict<any>
    return {k: val1}
enddef

F1()    # <<<<<<<<<<<<<<<<<<<< execute at script level

def F3()
    F2()
enddef
F3()

Something else that works is putting F2() in a different file, importing that file into /tmp/x.vim, and then running F2
/tmp/x.vim

vim9script

import autoload '/tmp/y.vim'

def F1()
    export const val1 = 'foo1'
enddef

def F3()
    F1()
    echo y.F2()
enddef

F3()

/tmp/y.vim

vim9script

import autoload '/tmp/x.vim'

def F2(): dict<any>
    return {k: x.val1}
enddef

Expected behaviour

I'm refactoring some code written over two years ago when vim9script was first becoming available. I ran into this in the first file that gets run when a command defined in a small plugin is executed. The code in question actually looks like

def InitHighlights()
    export const hl_label       = settings.Setting('hl_label')
    ...

And some initialization has to be done before the const are defined by the InitHighlights function. It does seem pretty weird, and I was probably surprised when it worked.

If doing export const in a function is made an error, I can probably easily handle it by putting the const stuff in a different file and be careful that that file doesn't get imported/accessed until some initialization is done. Or maybe do the exports in the file that does the initialization.

Seems it should either work or fail.

Version of Vim

9.1.516

Environment

ubuntu/gtk3

Logs and stack traces

No response


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15132@github.com>

errael

unread,
Jul 1, 2024, 4:49:05 PM (2 days ago) Jul 1
to vim/vim, Subscribed

I'm thinking that export const ... should not be allowed in a def and should generate an error.

On a side issue

Here's one that is not directly related, but shows an example of the difficulty during startup.
This works

    var errors: list<string> = settings.InitSettings()
    splice.SpliceInit9(errors)

This fails

    splice.SpliceInit9(settings.InitSettings())

Probably a compiler bug. I'm guessing the compiler loads splice.vim before executing settings.InitSettings(). But the bootstrap code requires settings be initialized before splice is loaded.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15132/2200992726@github.com>

errael

unread,
Jul 2, 2024, 2:16:08 PM (10 hours ago) Jul 2
to vim/vim, Subscribed

Probably a compiler bug. I'm guessing the compiler loads splice.vim before executing settings.InitSettings(). But the bootstrap code requires settings be initialized before splice is loaded.

Opened #15137 for this.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15132/2204002922@github.com>

Reply all
Reply to author
Forward
0 new messages