From dcf4a4871c0e3a375bf1e1268ec156318c99437b Mon Sep 17 00:00:00 2001 From: Nathan <47441022+NathanSnail@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:04:29 +1300 Subject: [PATCH 1/2] fix: make operator call work with iterators --- changelog.md | 1 + script/vm/compiler.lua | 8 +++++ test/type_inference/generic_operator.lua | 37 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/changelog.md b/changelog.md index daefdb5ae..55f172fc4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ # changelog ## Unreleased +* `FIX` Generic `for` loops now infer loop variables from the result of calling a callable iterator (e.g. a class with `---@operator call(): ...`) instead of requiring an actual function. ## 3.19.2 diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 20ab3ac41..0a9741285 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -2391,6 +2391,14 @@ local compilerSwitch = util.switch() end end end + -- Classes can have a call operator which we need to check + -- Note that currently call() cannot return multiple values so index != 1 will never be something + if not vm.getNode(source):isTyped() then + local operatorNode = vm.runOperator('call', func, nil, index) + if operatorNode then + vm.setNode(source, operatorNode) + end + end end) : case 'main' : call(function (source) diff --git a/test/type_inference/generic_operator.lua b/test/type_inference/generic_operator.lua index 69c1c931e..ba24187c6 100644 --- a/test/type_inference/generic_operator.lua +++ b/test/type_inference/generic_operator.lua @@ -200,3 +200,40 @@ TESTGENERICNAMES '' [[ ---@operator call(): integer local Plain ]] + +-- Generic for loop over a callable class: the loop variable comes from the +-- result of calling the iterator, not from a function return. +TEST 'string' [[ +---@class StringIterator +---@operator call(): string? + +---@type StringIterator +local iterator + +for in iterator do +end +]] + +-- Same through an instantiated generic callable class +TEST 'string' [[ +---@class Iterator +---@operator call(): T? + +---@type Iterator +local b + +for in b do +end +]] + +-- Nil is not doubled up +TEST 'string' [[ +---@class Iterator +---@operator call(): T? + +---@type Iterator +local b + +for in b do +end +]] From bdee1f0f5f70964aa5dc471f2cac7b9e5b0d8a25 Mon Sep 17 00:00:00 2001 From: Nathan <47441022+NathanSnail@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:19:10 +1300 Subject: [PATCH 2/2] fix: make callable class iterators infer correctly when there are multiple iteration variables --- script/vm/compiler.lua | 6 +++--- test/type_inference/generic_operator.lua | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 0a9741285..93bc76eb6 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -2392,9 +2392,9 @@ local compilerSwitch = util.switch() end end -- Classes can have a call operator which we need to check - -- Note that currently call() cannot return multiple values so index != 1 will never be something - if not vm.getNode(source):isTyped() then - local operatorNode = vm.runOperator('call', func, nil, index) + -- TODO: when @operator call() can have multiple returns this should be updated. + if index == 1 then + local operatorNode = vm.runOperator('call', func) if operatorNode then vm.setNode(source, operatorNode) end diff --git a/test/type_inference/generic_operator.lua b/test/type_inference/generic_operator.lua index ba24187c6..dff76198a 100644 --- a/test/type_inference/generic_operator.lua +++ b/test/type_inference/generic_operator.lua @@ -237,3 +237,26 @@ local b for in b do end ]] + +-- Bad iterators don't error +TEST 'unknown' [[ +---@class Iterator + +---@type Iterator +local b + +for in b do +end +]] + +-- Multiple returns don't have incorrect inference +TEST 'unknown' [[ +---@class Iterator +---@operator call(): string + +---@type Iterator +local b + +for k, in b do +end +]]