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

将函数返回值作为字典Key时,无法解析字典返回的值变量类型 #178

Open
SaekiYuu1997 opened this issue Nov 7, 2024 · 4 comments

Comments

@SaekiYuu1997
Copy link

Version: vscode-emmyLua-0.8.20

---@class InnerStruct
---@field name string
---@field age number


---@class Foo
local M = {}

function M:Init()
    ---@type table<number, InnerStruct>
    self._testDict = {}
end

function M:Test1()
    local id1 = self:GetIdx() -- 此处id1能够正确解析为number类型
    local val1 = self._testDict[id1] -- val1为unknown
end

function M:Test2()
    local id1 = 1
    local val1 = self._testDict[id1] -- val1能够正确解析为InnerStruct
end

---@return number
function M:GetIdx()
    return 1
end

以我的理解,上面的代码中Test1Test2中的val1局部变量都应该是InnerStruct类型。
这里是有bug还是我的用法有问题呢?

@SaekiYuu1997
Copy link
Author

补充:
下面的代码也可以正确解析

function M:Test3()
    ---@type number
    local id1 = self:GetIdx()
    local val1 = self._testDict[id1]
end

@CppCXY
Copy link
Member

CppCXY commented Nov 8, 2024

emmylua的类型推断因为一些设计上的失误全面崩坏, 实际上当local id1 = self:GetIdx()时, id1的真实类型是匿名类型, 他继承自number, 所以无法继续参与推断.

另外emmylua语言服务器当前正在重写的过程中, 所以我短时间内也不会修改这个BUG

@SaekiYuu1997
Copy link
Author

emmylua的类型推断因为一些设计上的失误全面崩坏, 实际上当local id1 = self:GetIdx()时, id1的真实类型是匿名类型, 他继承自number, 所以无法继续参与推断.

另外emmylua语言服务器当前正在重写的过程中, 所以我短时间内也不会修改这个BUG

这是仅存在vscode插件中的bug吗,我在jetbrain上使用这段代码能够正确解析Test1val1的类型

@CppCXY
Copy link
Member

CppCXY commented Nov 8, 2024

vscode emmylua是完全不同的实现, 他是C#写的, 性能和各个方便都远强于intellij-emmylua

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

No branches or pull requests

2 participants