LuaLS / LuaLS/lua-language-server

Class Inheritance Triggers Typing Issues

未关闭
#1,893 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

question
主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Type Checking, Diagnostics/Syntax Checking

Expected Behaviour

To put it in plain terms, this is for a system of UserControls for a settings-panel system. There is a single Superclass, called "MCT.Option", which houses all of the unified behavior of all the UserControls. Then there are subclasses, for each type of UserControl - checkbox, dropdown, etc.

There's an abstracted method on the UserControl "holders", holder:get_option_by_key("user_control_key') that I have annotated as returning an MCT.Option. In reality, that superclass is never actually returned in practice - it's purely abstracted, and any option is one of the several subclasses.

So when I call:

---@type MCT.Option.Checkbox
local option = holder:get_option_by_key("i_know_this_is_a_checkbox")

I'm expecting to very easily call a conversion from superclass -> subclass.

I have all that (I think) I need to tell the debugger that it's a subclass: initial definition of the subclass is ---@class MCT.Option.Checkbox : MCT.Option, Class to tell it its parents, and it reads from the higher methods and properties completely fine.

But when I call ---@type like that, the debugger doesn't let me simply convert from one to another.

I can of course use ---@cast option MCT.Option.Checkbox after the variable is defined, but that's a bit clunkier in my opinion than having the type defined above the variable, and it ends up requiring me to reference the variable in the annotation, so it takes a bit longer to write up (and I have a lot of these!)

Actual Behaviour

image

Reproduction steps
  1. Create a superclass:
---@class MySuperclass 
local o = {}
  1. Create a subclass:
---@class MySubclass : MySuperclass 
local o = {}
  1. Create a function that returns MySuperclass
  2. Try to direct the type that returns from that function into MySublass
Additional Notes

Thank you for all the hard work you've done over the years - I've been using this extension for a very very long time and it's truly a lifesaver.

Log File

file_s%3A_modding_warhammer_3_groovy_mct.log

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,通过 Windows 上的 Visual Studio Code 扩展重现 issue 中的超类和子类示例,重点关注将 MySuperclass 结果赋给 MySubclass 时的类型检查诊断。跟踪类继承涉及的类型检查行为,并确认修复完成后,无需 @cast 即可接受该赋值。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。