Skip to content

Make operator call work with iterators - #3507

Open
NathanSnail wants to merge 2 commits into
LuaLS:masterfrom
NathanSnail:operator_iterators
Open

NathanSnail wants to merge 2 commits into
LuaLS:masterfrom
NathanSnail:operator_iterators

Conversation

@NathanSnail

Copy link
Copy Markdown
Contributor

Currently classes with operator call defined do not work as iterators, this fixes that.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中文:在 compiler.lua 中,vm.getNode(source):isTyped() 存在潜在的空指针风险。如果 source 节点尚未被设置或为 nil,直接调用 getNode 并链式访问 isTyped() 会抛出错误。建议先确认 vm.getNode(source) 返回非 nil,例如 local sourceNode = vm.getNode(source); if sourceNode and not sourceNode:isTyped() then ...。
English: In compiler.lua, vm.getNode(source):isTyped() carries a potential nil dereference risk. If the source node has not been set yet or is nil, calling getNode and then chaining isTyped() will throw an error. Verify that vm.getNode(source) returns non-nil first, e.g. local sourceNode = vm.getNode(source); if sourceNode and not sourceNode:isTyped() then ....

中文:新增的逻辑仅在返回类型处于“未定型”(not isTyped())时尝试 call 运算符,但对该分支缺乏对 func 本身的类型校验。如果 func 不是一个可调用对象(例如只是一个普通表或未定义运算符的类型),vm.runOperator('call', ...) 的行为和返回依赖运行时实现,建议在此明确 func 为 callable 类时才执行,以避免意外副作用或误推断。
English: The new logic only attempts the call operator when the return type is untyped (not isTyped()), but this branch lacks type validation of func itself. If func is not a callable object (e.g. an ordinary table or a type without a defined operator), the behavior and return value of vm.runOperator('call', ...) depend on the runtime implementation. Explicitly restrict execution to the case where func is a callable class to avoid unintended side effects or incorrect inference.

中文:index 参数被传给 vm.runOperator('call', func, nil, index),注释声称“call() 目前不能返回多个值,所以 index 不会大于 1”,但这是一种脆弱假设。若未来 call 运算支持多返回值,此处的 index 逻辑可能需要重新审视;建议添加断言或至少记录该限制,并在相关测试中覆盖 index 不为 1 的情况。
English: The index parameter is passed to vm.runOperator('call', func, nil, index), and the comment claims "call() currently cannot return multiple values, so index will never be anything other than 1", but this is a fragile assumption. If the call operator later supports multiple return values, the index logic here may need to be revisited; consider adding an assertion or at least documenting this limitation, and cover the case where index is not 1 in the related tests.

中文:测试中第三个用例 Iterator<string?> 旨在验证“nil 不会被重复叠加”(即推断结果为 string 而非 string?),但仅以 TEST 'string' 断言最终类型,未显式验证 nil 处理路径。建议补充对可空类型嵌套归一化的显式测试(例如 ---@operator call(): T? 配合 T = string? 应得到 string? 而不是 string??),以增强回归覆盖。
English: The third test case Iterator<string?> intends to verify that "nil is not doubled up" (i.e. the inferred result is string rather than string?), but it only asserts the final type via TEST 'string' without explicitly verifying the nil-handling path. Consider adding an explicit test for nullable-type nesting normalization (e.g. ---@operator call(): T? with T = string? should yield string? rather than string??) to strengthen regression coverage.

中文:review 总结:本次改动为泛型 for 循环增加了从可调用迭代器(含 call 运算符)推断循环变量的支持,方向合理且附带测试。主要风险点在于 vm.getNode(source) 可能为 nil 的防御性检查缺失、func 是否为 callable 的校验不足,以及对 index != 1 假设的脆弱性。测试覆盖基本到位,但对可空类型归一化路径的显式验证可进一步加强。
English: Review summary: This change adds support for inferring the loop variable of a generic for loop from a callable iterator (one with a call operator), which is a reasonable direction and comes with tests. The main risks are the missing defensive check for vm.getNode(source) possibly being nil, insufficient validation of whether func is callable, and the fragility of the index != 1 assumption. Test coverage is mostly adequate, but explicit verification of the nullable-type normalization path could be strengthened further.

@NathanSnail

Copy link
Copy Markdown
Contributor Author

Testing when it should return T? instead of T?? is out of scope, this is for iterators. Other suggestions applied.

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

Successfully merging this pull request may close these issues.

1 participant