LuaLS / LuaLS/lua-language-server

`generic` inheritance does not work

Open
#1,272 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feat/generic feat/LuaCats Annotations
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

The second case

```lua
Core = {};

---@class Rotation
---@field Enabled boolean
local Rotation = {}

function Rotation:OnLoad() end
function Rotation:OnUnload() end

---@class RotationEngine
Core.RotationEngine = {}

---@generic T
---@param name `T`
---@return T|Rotation rotation
function Core.RotationEngine.AddRotation(name) end

---@class MyRotation
---@field testfield table
local MyRotation = {};

function MyRotation:Test() end

local rotation = Core.RotationEngine.AddRotation("MyRotation");

---@param self MyRotation
---@return boolean
local function myFunc(self) end

local a = myFunc(rotation);

```

In the first case, in the `MyRotation` class, we seek methods of the `Rotation` class,

![image](https://user-images.githubusercontent.com/57856647/176996494-7304718b-488e-4c58-b88b-6c5078f6ef2c.png)

but also an error `Cannot assign MyRotation|Rotation to parameter MyRotation.Lua Diagnostics.(param-type-mismatch)`

![image](https://user-images.githubusercontent.com/57856647/176996540-b4162795-1b9e-4d10-af84-27a8c2cbe5b4.png)

The second case

```lua
Core = {};

---@class Rotation
---@field Enabled boolean
local Rotation = {}

function Rotation:OnLoad() end
function Rotation:OnUnload() end

---@class RotationEngine
Core.RotationEngine = {}

---@generic T: Rotation
---@param name `T`
---@return T rotation
function Core.RotationEngine.AddRotation(name) end

---@class MyRotation
---@field testfield table
local MyRotation = {};

function MyRotation:Test() end

local rotation = Core.RotationEngine.AddRotation("MyRotation");

---@param self MyRotation
---@return boolean
local function myFunc(self) end

local a = myFunc(rotation);
```

In the second case there is no inheritance

![image](https://user-images.githubusercontent.com/57856647/176996609-80dc809d-414d-4da2-8482-07384f474d32.png)

and there is no error either

![image](https://user-images.githubusercontent.com/57856647/176996651-265a114d-6ecd-408a-adf7-28a68edf225e.png)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing both Lua snippets from the issue and compare the inferred type of rotation, method completion, and diagnostics. Trace the generic return-type inference and class-inheritance handling involved in AddRotation. Done means the intended inherited members are available and the incorrect MyRotation|Rotation type mismatch is resolved without losing the second case's behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.