Skip to content

Commit d505602

Browse files
committed
Order symbols made from other symbols by what they are made from
Symbols the checker makes from others, such as the properties a spread merges or copies, the members of mapped types, and instantiated symbols, share the declaration, name, and flags of the symbol they are made from, and were ordered by symbol id, which follows the order in which symbols are first used. They are now ordered by the symbols and types they are made from, which are recorded when they are created. The symbols made for unresolved names share their name, and are ordered by the unresolved names that qualify them, as Bidi.Event and Protocol.Cdp.Event are when neither module is found (seen in puppeteer and DefinitelyTyped).
1 parent a970563 commit d505602

5 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎tsc/internal/checker/utilities.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,73 @@ func (c *Checker) compareSymbolsWorker(s1, s2 *ast.Symbol) int {
385385
if r := cmp.Compare(s1.CheckFlags(), s2.CheckFlags()); r != 0 {
386386
return r
387387
}
388+
if r := c.compareSymbolOrigins(s1, s2); r != 0 {
389+
return r
390+
}
388391
// Fall back to symbol IDs. This is a last resort that should happen only when symbols have
389392
// no declaration and duplicate names.
390393
return cmp.Compare(ast.GetSymbolId(s1), ast.GetSymbolId(s2))
391394
}
392395

396+
// compareSymbolOrigins orders transient symbols by the symbols and types they
397+
// are made from, which are recorded when they are created: the properties a
398+
// spread merges or copies, the mapped, union, or intersection type containing
399+
// a synthesized property and its key type, and the symbol an instantiated or
400+
// copied symbol is made from along with its mapper.
401+
func (c *Checker) compareSymbolOrigins(s1, s2 *ast.Symbol) int {
402+
if s1.Flags()&s2.Flags()&ast.SymbolFlagsTransient == 0 {
403+
return 0
404+
}
405+
// Unresolved names are interned by their qualified path, so those sharing a
406+
// name differ in the unresolved names that qualify them.
407+
if s1.CheckFlags()&s2.CheckFlags()&ast.CheckFlagsUnresolved != 0 {
408+
return c.compareSymbols(s1.Parent(), s2.Parent())
409+
}
410+
spread1 := c.spreadLinks.TryGet(s1)
411+
spread2 := c.spreadLinks.TryGet(s2)
412+
if (spread1 != nil) != (spread2 != nil) {
413+
return core.IfElse(spread1 != nil, 1, -1)
414+
}
415+
if spread1 != nil {
416+
if r := c.compareSymbols(spread1.leftSpread, spread2.leftSpread); r != 0 {
417+
return r
418+
}
419+
if r := c.compareSymbols(spread1.rightSpread, spread2.rightSpread); r != 0 {
420+
return r
421+
}
422+
}
423+
var origin1, origin2 *ast.Symbol
424+
var key1, key2 *Type
425+
if links := c.mappedSymbolLinks.TryGet(s1); links != nil {
426+
origin1, key1 = links.syntheticOrigin, links.keyType
427+
}
428+
if links := c.mappedSymbolLinks.TryGet(s2); links != nil {
429+
origin2, key2 = links.syntheticOrigin, links.keyType
430+
}
431+
if r := c.compareSymbols(origin1, origin2); r != 0 {
432+
return r
433+
}
434+
var target1, target2 *ast.Symbol
435+
var mapper1, mapper2 *TypeMapper
436+
var containing1, containing2 *Type
437+
if links := c.valueSymbolLinks.TryGet(s1); links != nil {
438+
target1, mapper1, containing1 = links.target, links.mapper, links.getContainingType()
439+
}
440+
if links := c.valueSymbolLinks.TryGet(s2); links != nil {
441+
target2, mapper2, containing2 = links.target, links.mapper, links.getContainingType()
442+
}
443+
if r := CompareTypes(containing1, containing2); r != 0 {
444+
return r
445+
}
446+
if r := CompareTypes(key1, key2); r != 0 {
447+
return r
448+
}
449+
if r := c.compareSymbols(target1, target2); r != 0 {
450+
return r
451+
}
452+
return compareTypeMappers(mapper1, mapper2)
453+
}
454+
393455
func (c *Checker) compareSymbolDeclarationsAndNames(s1, s2 *ast.Symbol) int {
394456
if len(s1.Declarations()) != 0 && len(s2.Declarations()) != 0 {
395457
if r := c.compareNodes(s1.Declarations()[0], s2.Declarations()[0]); r != 0 {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
unresolvedQualifiedNamesSharingName.ts(1,28): error TS2307: Cannot find module 'missing-a' or its corresponding type declarations.
2+
unresolvedQualifiedNamesSharingName.ts(2,31): error TS2307: Cannot find module 'missing-b' or its corresponding type declarations.
3+
4+
5+
==== unresolvedQualifiedNamesSharingName.ts (2 errors) ====
6+
import type * as Bidi from "missing-a";
7+
~~~~~~~~~~~
8+
!!! error TS2307: Cannot find module 'missing-a' or its corresponding type declarations.
9+
import type { Protocol } from "missing-b";
10+
~~~~~~~~~~~
11+
!!! error TS2307: Cannot find module 'missing-b' or its corresponding type declarations.
12+
13+
type CdpEvent = Protocol.Cdp.Event;
14+
export function f(event: Bidi.Event | CdpEvent) {}
15+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/unresolvedQualifiedNamesSharingName.ts] ////
2+
3+
=== unresolvedQualifiedNamesSharingName.ts ===
4+
import type * as Bidi from "missing-a";
5+
>Bidi : Symbol(Bidi, Decl(unresolvedQualifiedNamesSharingName.ts, 0, 11))
6+
7+
import type { Protocol } from "missing-b";
8+
>Protocol : Symbol(Protocol, Decl(unresolvedQualifiedNamesSharingName.ts, 1, 13))
9+
10+
type CdpEvent = Protocol.Cdp.Event;
11+
>CdpEvent : Symbol(CdpEvent, Decl(unresolvedQualifiedNamesSharingName.ts, 1, 42))
12+
>Protocol : Symbol(Protocol, Decl(unresolvedQualifiedNamesSharingName.ts, 1, 13))
13+
>Cdp : Symbol(Protocol.Cdp)
14+
>Event : Symbol(Protocol.Cdp.Event)
15+
16+
export function f(event: Bidi.Event | CdpEvent) {}
17+
>f : Symbol(f, Decl(unresolvedQualifiedNamesSharingName.ts, 3, 35))
18+
>event : Symbol(event, Decl(unresolvedQualifiedNamesSharingName.ts, 4, 18))
19+
>Bidi : Symbol(Bidi, Decl(unresolvedQualifiedNamesSharingName.ts, 0, 11))
20+
>Event : Symbol(Bidi.Event)
21+
>CdpEvent : Symbol(CdpEvent, Decl(unresolvedQualifiedNamesSharingName.ts, 1, 42))
22+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/unresolvedQualifiedNamesSharingName.ts] ////
2+
3+
=== unresolvedQualifiedNamesSharingName.ts ===
4+
import type * as Bidi from "missing-a";
5+
>Bidi : any
6+
7+
import type { Protocol } from "missing-b";
8+
>Protocol : any
9+
10+
type CdpEvent = Protocol.Cdp.Event;
11+
>CdpEvent : Protocol.Cdp.Event
12+
>Protocol : any
13+
>Cdp : any
14+
15+
export function f(event: Bidi.Event | CdpEvent) {}
16+
>f : (event: Bidi.Event | CdpEvent) => void
17+
>event : any
18+
>Bidi : any
19+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @noEmit: true
2+
// @strict: true
3+
// @module: nodenext
4+
5+
import type * as Bidi from "missing-a";
6+
import type { Protocol } from "missing-b";
7+
8+
type CdpEvent = Protocol.Cdp.Event;
9+
export function f(event: Bidi.Event | CdpEvent) {}

0 commit comments

Comments
 (0)