lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

`@deprecated` is ignored on globals when assigned by another global

Open rhys-vdw opened this issue 11 months ago • 2 comments

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

Image

Interestingly the deprecation is shown correctly in the autocomplete:

Image

rhys-vdw avatar Feb 13 '25 15:02 rhys-vdw

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 & y have 2 definitions
  • z & a have only 1 definition

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

tomlau10 avatar Feb 14 '25 03:02 tomlau10

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 @deprecated only officially supports functions but not variables / fields.

I noticed this too.

rhys-vdw avatar Feb 14 '25 03:02 rhys-vdw