Skip to content

Commit a418cb7

Browse files
committed
Compare without reporting errors before reporting them
Reporting the errors of a comparison builds their messages as it goes, printing the types involved, also for nested comparisons whose errors are discarded once the comparison falls back to one that succeeds, as comparing type arguments by variance does before comparing types structurally. With noErrorTruncation, printing large types that way can take minutes and gigabytes even when no error is reported at all, and the instantiations it makes can report excessive depth (TS2589) where checking alone does not. A comparison that reports errors first compares without reporting them, and compares again, reporting them, only if that fails. One elaboration changes, as the comparison that reports errors finds a different failing member first, and traces list types in a different order.
1 parent ee7491f commit a418cb7

7 files changed

Lines changed: 1312 additions & 111 deletions

‎tsc/internal/checker/relater.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,19 @@ func (c *Checker) checkTypeRelatedToEx(
370370
) bool {
371371
r := c.getRelater()
372372
r.relation = relation
373-
r.errorNode = errorNode
374373
r.relationCount = (16_000_000 - relation.size()) / 8
375-
result := r.isRelatedToEx(source, target, RecursionFlagsBoth, errorNode != nil /*reportErrors*/, headMessage, IntersectionStateNone)
374+
// Comparing first without reporting errors keeps the comparison from
375+
// building error messages, which print the types involved, for nested
376+
// comparisons that fail on the way to a comparison that succeeds.
377+
result := r.isRelatedToEx(source, target, RecursionFlagsBoth, false /*reportErrors*/, headMessage, IntersectionStateNone)
378+
if errorNode != nil && result == TernaryFalse && !r.overflow {
379+
c.putRelater(r)
380+
r = c.getRelater()
381+
r.relation = relation
382+
r.errorNode = errorNode
383+
r.relationCount = (16_000_000 - relation.size()) / 8
384+
result = r.isRelatedToEx(source, target, RecursionFlagsBoth, true /*reportErrors*/, headMessage, IntersectionStateNone)
385+
}
376386
if r.overflow {
377387
// Record this relation as having failed such that we don't attempt the overflowing operation again.
378388
id, _ := getRelationKey(source, target, IntersectionStateNone, relation == c.identityRelation, false /*ignoreConstraints*/)

‎tsc/testdata/baselines/reference/compiler/complexRecursiveCollections.errors.txt‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ immutable.ts(289,22): error TS2320: Interface 'Indexed<T>' cannot simultaneously
66
immutable.ts(305,22): error TS2320: Interface 'Set<T>' cannot simultaneously extend types 'Seq<never, T>' and 'Set<T>'.
77
Named property 'size' of types 'Seq<never, T>' and 'Set<T>' are not identical.
88
immutable.ts(323,20): error TS2430: Interface 'Seq<K, V>' incorrectly extends interface 'Collection<K, V>'.
9-
The types returned by 'map(...).size' are incompatible between these types.
10-
Type 'number | undefined' is not assignable to type 'number'.
11-
Type 'undefined' is not assignable to type 'number'.
9+
The types returned by 'map(...).filter' are incompatible between these types.
10+
Type '{ <F extends M>(predicate: (value: M, key: K, iter: Seq<K, M>) => value is F, context?: any): Seq<K, F>; (predicate: (value: M, key: K, iter: Seq<K, M>) => any, context?: any): Seq<K, M>; }' is not assignable to type '{ <F extends M>(predicate: (value: M, key: K, iter: Collection<K, M>) => value is F, context?: any): Collection<K, F>; (predicate: (value: M, key: K, iter: Collection<K, M>) => any, context?: any): Collection<K, M>; }'.
11+
Types of parameters 'predicate' and 'predicate' are incompatible.
12+
Type '(value: M, key: K, iter: Collection<K, M>) => any' is not assignable to type '(value: M, key: K, iter: Seq<K, M>) => value is any'.
13+
Signature '(value: M, key: K, iter: Collection<K, M>): any' must be a type predicate.
1214
immutable.ts(341,22): error TS2430: Interface 'Keyed<K, V>' incorrectly extends interface 'Collection<K, V>'.
1315
The types returned by 'toSeq()' are incompatible between these types.
1416
Type 'Keyed<K, V>' is not assignable to type 'this'.
@@ -382,9 +384,11 @@ immutable.ts(391,22): error TS2430: Interface 'Set<T>' incorrectly extends inter
382384
export interface Seq<K, V> extends Collection<K, V> {
383385
~~~
384386
!!! error TS2430: Interface 'Seq<K, V>' incorrectly extends interface 'Collection<K, V>'.
385-
!!! error TS2430: The types returned by 'map(...).size' are incompatible between these types.
386-
!!! error TS2430: Type 'number | undefined' is not assignable to type 'number'.
387-
!!! error TS2430: Type 'undefined' is not assignable to type 'number'.
387+
!!! error TS2430: The types returned by 'map(...).filter' are incompatible between these types.
388+
!!! error TS2430: Type '{ <F extends M>(predicate: (value: M, key: K, iter: Seq<K, M>) => value is F, context?: any): Seq<K, F>; (predicate: (value: M, key: K, iter: Seq<K, M>) => any, context?: any): Seq<K, M>; }' is not assignable to type '{ <F extends M>(predicate: (value: M, key: K, iter: Collection<K, M>) => value is F, context?: any): Collection<K, F>; (predicate: (value: M, key: K, iter: Collection<K, M>) => any, context?: any): Collection<K, M>; }'.
389+
!!! error TS2430: Types of parameters 'predicate' and 'predicate' are incompatible.
390+
!!! error TS2430: Type '(value: M, key: K, iter: Collection<K, M>) => any' is not assignable to type '(value: M, key: K, iter: Seq<K, M>) => value is any'.
391+
!!! error TS2430: Signature '(value: M, key: K, iter: Collection<K, M>): any' must be a type predicate.
388392
readonly size: number | undefined;
389393
// Force evaluation
390394
cacheResult(): this;

0 commit comments

Comments
 (0)