Description
A table assigned after a forward local declaration does not gain fields assigned later. This produces undefined-field at the assignment and causes exported module fields to be considered nullable.
The equivalent code with declaration and table initialization in one statement works without diagnostics.
This affects LuaSnip: luasnip/config.lua forward-declares c, assigns its table later, and then aliases c.setup = c.set_config. As a result, normal user configuration gets function require('luasnip').setup may be nil [need-check-nil].
Minimal reproduction
.emmyrc.json:
split/config.lua:
local config
config = {
set_config = function(user_config)
return user_config or {}
end,
}
config.setup = config.set_config
return config
split/api.lua:
local API = {}
API.setup = require('split.config').setup
return API
client.lua:
require('split.api').setup {}
Run emmylua_check ..
Actual behavior
split/config.lua:9:8: Undefined field `setup`. [undefined-field]
split/api.lua:3:37: Undefined field `setup`. [undefined-field]
client.lua:1:1: function require('split.api').setup may be nil [need-check-nil]
Expected behavior
No diagnostics. config is definitely assigned the table before config.setup is added, and setup is definitely assigned before the module is returned.
Changing only the first two declarations to initialize the local directly makes the diagnostics disappear:
local config = {
set_config = function(user_config)
return user_config or {}
end,
}
Environment
Description
A table assigned after a forward local declaration does not gain fields assigned later. This produces
undefined-fieldat the assignment and causes exported module fields to be considered nullable.The equivalent code with declaration and table initialization in one statement works without diagnostics.
This affects LuaSnip:
luasnip/config.luaforward-declaresc, assigns its table later, and then aliasesc.setup = c.set_config. As a result, normal user configuration getsfunction require('luasnip').setup may be nil [need-check-nil].Minimal reproduction
.emmyrc.json:{}split/config.lua:split/api.lua:client.lua:Run
emmylua_check ..Actual behavior
Expected behavior
No diagnostics.
configis definitely assigned the table beforeconfig.setupis added, andsetupis definitely assigned before the module is returned.Changing only the first two declarations to initialize the local directly makes the diagnostics disappear:
Environment
emmylua_check 0.25.1emmylua_ls 0.25.1