Skip to content

Report iteration errors on all error nodes #1061

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

Merged
merged 5 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ const (
IterationUseSpreadFlag IterationUse = 1 << 5
IterationUseDestructuringFlag IterationUse = 1 << 6
IterationUsePossiblyOutOfBounds IterationUse = 1 << 7
IterationUseReportError IterationUse = 1 << 8
// Spread, Destructuring, Array element assignment
IterationUseElement = IterationUseAllowsSyncIterablesFlag
IterationUseSpread = IterationUseAllowsSyncIterablesFlag | IterationUseSpreadFlag
Expand All @@ -481,7 +480,7 @@ const (
IterationUseAsyncYieldStar = IterationUseAllowsSyncIterablesFlag | IterationUseAllowsAsyncIterablesFlag | IterationUseYieldStarFlag
IterationUseGeneratorReturnType = IterationUseAllowsSyncIterablesFlag
IterationUseAsyncGeneratorReturnType = IterationUseAllowsAsyncIterablesFlag
IterationUseCacheFlags = IterationUseAllowsSyncIterablesFlag | IterationUseAllowsAsyncIterablesFlag | IterationUseForOfFlag | IterationUseReportError
IterationUseCacheFlags = IterationUseAllowsSyncIterablesFlag | IterationUseAllowsAsyncIterablesFlag | IterationUseForOfFlag
)

type IterationTypes struct {
Expand Down Expand Up @@ -5900,18 +5899,26 @@ func (c *Checker) getIterationTypesOfIterable(t *Type, use IterationUse, errorNo
if IsTypeAny(t) {
return IterationTypes{c.anyType, c.anyType, c.anyType}
}
key := IterationTypesKey{typeId: t.id, use: use&IterationUseCacheFlags | core.IfElse(errorNode != nil, IterationUseReportError, 0)}
key := IterationTypesKey{typeId: t.id, use: use & IterationUseCacheFlags}
// If we are reporting errors and encounter a cached `noIterationTypes`, we should ignore the cached value and continue as if nothing was cached.
// In addition, we should not cache any new results for this call.
noCache := false
if cached, ok := c.iterationTypesCache[key]; ok {
return cached
if errorNode == nil || cached.hasTypes() {
return cached
}
noCache = true
}
result := c.getIterationTypesOfIterableWorker(t, use, errorNode, noCache)
if !noCache {
c.iterationTypesCache[key] = result
}
result := c.getIterationTypesOfIterableWorker(t, use, errorNode)
c.iterationTypesCache[key] = result
return result
}

func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, errorNode *ast.Node) IterationTypes {
func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, errorNode *ast.Node, noCache bool) IterationTypes {
if t.flags&TypeFlagsUnion != 0 {
return c.combineIterationTypes(core.Map(t.Types(), func(t *Type) IterationTypes { return c.getIterationTypesOfIterableWorker(t, use, errorNode) }))
return c.combineIterationTypes(core.Map(t.Types(), func(t *Type) IterationTypes { return c.getIterationTypesOfIterableWorker(t, use, errorNode, noCache) }))
}
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
iterationTypes := c.getIterationTypesOfIterableFast(t, c.asyncIterationTypesResolver)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
omittedExpressionForOfLoop.ts(1,19): error TS2304: Cannot find name 'doesNotExist'.
omittedExpressionForOfLoop.ts(4,19): error TS18050: The value 'undefined' cannot be used here.
omittedExpressionForOfLoop.ts(7,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
omittedExpressionForOfLoop.ts(10,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.


==== omittedExpressionForOfLoop.ts (3 errors) ====
==== omittedExpressionForOfLoop.ts (4 errors) ====
for (const [,] of doesNotExist) {
~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'doesNotExist'.
Expand All @@ -20,4 +21,6 @@ omittedExpressionForOfLoop.ts(7,12): error TS2488: Type 'never' must have a '[Sy
}

for (const [] of []) {
~~
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
for-of16.ts(8,11): error TS2488: Type 'MyStringIterator' must have a '[Symbol.iterator]()' method that returns an iterator.
for-of16.ts(10,11): error TS2488: Type 'MyStringIterator' must have a '[Symbol.iterator]()' method that returns an iterator.


==== for-of16.ts (1 errors) ====
==== for-of16.ts (2 errors) ====
class MyStringIterator {
[Symbol.iterator]() {
return this;
Expand All @@ -14,4 +15,7 @@ for-of16.ts(8,11): error TS2488: Type 'MyStringIterator' must have a '[Symbol.it
!!! error TS2488: Type 'MyStringIterator' must have a '[Symbol.iterator]()' method that returns an iterator.
!!! related TS2489 for-of16.ts:8:11: An iterator must have a 'next()' method.

for (v of new MyStringIterator) { } // Should still fail (related errors should still be shown even though type is cached).
for (v of new MyStringIterator) { } // Should still fail (related errors should still be shown even though type is cached).
~~~~~~~~~~~~~~~~~~~~
!!! error TS2488: Type 'MyStringIterator' must have a '[Symbol.iterator]()' method that returns an iterator.
!!! related TS2489 for-of16.ts:10:11: An iterator must have a 'next()' method.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ types.forAwait.es2018.2.ts(8,21): error TS2504: Type '{}' must have a '[Symbol.a
types.forAwait.es2018.2.ts(10,16): error TS2322: Type 'number' is not assignable to type 'string'.
types.forAwait.es2018.2.ts(12,16): error TS2322: Type 'number' is not assignable to type 'string'.
types.forAwait.es2018.2.ts(14,21): error TS2488: Type 'AsyncIterable<number>' must have a '[Symbol.iterator]()' method that returns an iterator.
types.forAwait.es2018.2.ts(16,15): error TS2488: Type 'AsyncIterable<number>' must have a '[Symbol.iterator]()' method that returns an iterator.


==== types.forAwait.es2018.2.ts (5 errors) ====
==== types.forAwait.es2018.2.ts (6 errors) ====
declare const asyncIterable: AsyncIterable<number>;
declare const iterable: Iterable<number>;
async function f() {
Expand Down Expand Up @@ -33,6 +34,9 @@ types.forAwait.es2018.2.ts(14,21): error TS2488: Type 'AsyncIterable<number>' mu
!!! related TS2773 types.forAwait.es2018.2.ts:14:21: Did you forget to use 'await'?
}
for (y of asyncIterable) {
~~~~~~~~~~~~~
!!! error TS2488: Type 'AsyncIterable<number>' must have a '[Symbol.iterator]()' method that returns an iterator.
!!! related TS2773 types.forAwait.es2018.2.ts:16:15: Did you forget to use 'await'?
}
}

This file was deleted.

Loading