`@deprecated` is ignored on globals when assigned by another global
meta.lua
---@meta
--
--Assigning a global fails.
--
Global = {}
---@deprecated
AssignedGlobal = Global
--
--Assigning a field of a global also fails.
--
Global.field = {}
---@deprecated
AssignedGlobalField = Global.field
--
--Standard object works.
--
---@deprecated
Object = {}
--
--Assignment of local object works.
--
local alsoWorking = {}
---@deprecated
AssignedLocalObject = alsoWorking
test.lua
local x = AssignedGlobal
local y = AssignedGlobalField
local z = Object
local a = AssignedLocalObject
Interestingly the deprecation is shown correctly in the autocomplete:
From maintainer's comment of another issue: https://github.com/LuaLS/lua-language-server/issues/2784#issuecomment-2290825602
This is a dilemma. The current strategy is that when an object has multiple definitions, as long as one of the definitions is not marked as deprecated, the entire object is not considered deprecated. This is to allow users to re-implement built-in functions that have been deprecated, such as
table.getlen.
(translated by chatgpt)
When assigning a global to another global, there exists two definition for that variable:
- one is with
@deprecated - another one is not
- => thus it does not fulfill the all definition are marked as deprecated condition
While when assigning a local to global, seems that it is not considered as a definition of that global.
You can verify this by doing a goto definition on your x/y/z/a variables:
-
x&yhave2definitions -
z&ahave only1definition
IIUC, this is by design: as long as one of the definitions is not marked as deprecated, the entire object is not considered deprecated.
In addition, from wiki the @deprecated only officially supports functions but not variables / fields.
https://luals.github.io/wiki/annotations/#deprecated
Using @deprecated to mark fields may result in undefined behavior, as discussed here: https://github.com/LuaLS/lua-language-server/issues/1313#issuecomment-2633202893
Thanks for the explanation. I'd need to look into it more to decide whether this behavior is sensible. In my case I was able to work around it this way:
---@class Foo
local foo = {}
---@deprecated
GlobalFoo = nil ---@type Foo
In addition, from wiki the
@deprecatedonly officially supports functions but not variables / fields.
I noticed this too.