Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using _ENV instead of self #177

Open
surfskidude opened this issue Sep 11, 2018 · 5 comments
Open

Using _ENV instead of self #177

surfskidude opened this issue Sep 11, 2018 · 5 comments

Comments

@surfskidude
Copy link

I use _ENV as first argument to an object oriented Lua library and I get warnings on _ENV not being used and that the variables in the _ENV table are undefined.

Example:
local function f1(_ENV)
return xyz -- xyz is in the _ENV table
end

@mpeterv
Copy link
Owner

mpeterv commented Sep 12, 2018

It's easy enough to treat globals in scope of some local _ENV as its fields, silencing the warnings.

It's a bit trickier with top level "global" _ENV (technically an upvalue of the main chunk). If it's assigned to then it's no longer obvious whether globals point to original global environment, or to the assigned one, or possibly to both.

local function something(t)
    _ENV = t
end

some_function(something)

 -- Warn about unknown field or assume _ENV could have been assigned to by now?
os.unknown_function()

This can be implemented, too (translate each global name to _ENV.name, add a virtual _ENV upvalue to the main chunk, find reaching assignments for local accesses the way luacheck already does, treat indexing of local _ENV as a global if only the original value of the _ENV upvalue reaches it), it's just a bit complicated.

Then there is also an issue with Lua versions that don't have _ENV. Ideally with --std=lua51 or --std=luajit or --std=min this special _ENV treatment should be disabled, but currenty --std only sets allowed globals and nothing else, and no option values are available during checking, they are applied at a later stage instead.

(I'm just listing reasons why this is not trivial to implement, I'm still adding this to TODOs.)

@surfskidude
Copy link
Author

Yes, I do understand that this is not an easy to implement feature.

Thank you for the detailed response and for considering adding this feature request to your TODO list.

@mpeterv
Copy link
Owner

mpeterv commented Sep 13, 2018

Keeping this open until it's implemented.

@mpeterv mpeterv reopened this Sep 13, 2018
@Mehgugs
Copy link

Mehgugs commented Jan 23, 2019

Any updates on this?

@linuxmaniac
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants