Skip to content

Don't widen unique symbol for different declarations #55943

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 0 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11422,11 +11422,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
reportErrorsFromWidening(declaration, type);
}

// always widen a 'unique symbol' type if the type was created for a different declaration.
if (type.flags & TypeFlags.UniqueESSymbol && (isBindingElement(declaration) || !declaration.type) && type.symbol !== getSymbolOfDeclaration(declaration)) {
type = esSymbolType;
}

return getWidenedType(type);
}

Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/issue55901.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [tests/cases/compiler/issue55901.ts] ////

//// [issue55901.ts]
export const l = "A";
export const l1 = l as typeof l;
export const l2 = l;
export let l3 = l as typeof l;
export let a4 = l;

export const s = Symbol();
export const s1 = s as typeof s;
export const s2 = s;
export let s3 = s as typeof s;
export let s4 = s;


//// [issue55901.js]
export const l = "A";
export const l1 = l;
export const l2 = l;
export let l3 = l;
export let a4 = l;
export const s = Symbol();
export const s1 = s;
export const s2 = s;
export let s3 = s;
export let s4 = s;


//// [issue55901.d.ts]
export declare const l = "A";
export declare const l1: "A";
export declare const l2 = "A";
export declare let l3: "A";
export declare let a4: string;
export declare const s: unique symbol;
export declare const s1: typeof s;
export declare const s2: typeof s;
export declare let s3: typeof s;
export declare let s4: typeof s;
46 changes: 46 additions & 0 deletions tests/baselines/reference/issue55901.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/issue55901.ts] ////

=== issue55901.ts ===
export const l = "A";
>l : Symbol(l, Decl(issue55901.ts, 0, 12))

export const l1 = l as typeof l;
>l1 : Symbol(l1, Decl(issue55901.ts, 1, 12))
>l : Symbol(l, Decl(issue55901.ts, 0, 12))
>l : Symbol(l, Decl(issue55901.ts, 0, 12))

export const l2 = l;
>l2 : Symbol(l2, Decl(issue55901.ts, 2, 12))
>l : Symbol(l, Decl(issue55901.ts, 0, 12))

export let l3 = l as typeof l;
>l3 : Symbol(l3, Decl(issue55901.ts, 3, 10))
>l : Symbol(l, Decl(issue55901.ts, 0, 12))
>l : Symbol(l, Decl(issue55901.ts, 0, 12))

export let a4 = l;
>a4 : Symbol(a4, Decl(issue55901.ts, 4, 10))
>l : Symbol(l, Decl(issue55901.ts, 0, 12))

export const s = Symbol();
>s : Symbol(s, Decl(issue55901.ts, 6, 12))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))

export const s1 = s as typeof s;
>s1 : Symbol(s1, Decl(issue55901.ts, 7, 12))
>s : Symbol(s, Decl(issue55901.ts, 6, 12))
>s : Symbol(s, Decl(issue55901.ts, 6, 12))

export const s2 = s;
>s2 : Symbol(s2, Decl(issue55901.ts, 8, 12))
>s : Symbol(s, Decl(issue55901.ts, 6, 12))

export let s3 = s as typeof s;
>s3 : Symbol(s3, Decl(issue55901.ts, 9, 10))
>s : Symbol(s, Decl(issue55901.ts, 6, 12))
>s : Symbol(s, Decl(issue55901.ts, 6, 12))

export let s4 = s;
>s4 : Symbol(s4, Decl(issue55901.ts, 10, 10))
>s : Symbol(s, Decl(issue55901.ts, 6, 12))

52 changes: 52 additions & 0 deletions tests/baselines/reference/issue55901.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//// [tests/cases/compiler/issue55901.ts] ////

=== issue55901.ts ===
export const l = "A";
>l : "A"
>"A" : "A"

export const l1 = l as typeof l;
>l1 : "A"
>l as typeof l : "A"
>l : "A"
>l : "A"

export const l2 = l;
>l2 : "A"
>l : "A"

export let l3 = l as typeof l;
>l3 : "A"
>l as typeof l : "A"
>l : "A"
>l : "A"

export let a4 = l;
>a4 : string
>l : "A"

export const s = Symbol();
>s : unique symbol
>Symbol() : unique symbol
>Symbol : SymbolConstructor

export const s1 = s as typeof s;
>s1 : unique symbol
>s as typeof s : unique symbol
>s : unique symbol
>s : unique symbol

export const s2 = s;
>s2 : unique symbol
>s : unique symbol

export let s3 = s as typeof s;
>s3 : unique symbol
>s as typeof s : unique symbol
>s : unique symbol
>s : unique symbol

export let s4 = s;
>s4 : unique symbol
>s : unique symbol

64 changes: 32 additions & 32 deletions tests/baselines/reference/uniqueSymbols.types
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const constTypeAndCall: unique symbol = Symbol();

// declaration from initializer
const constInitToConstCall = constCall;
>constInitToConstCall : symbol
>constInitToConstCall : unique symbol
>constCall : unique symbol

const constInitToLetCall = letCall;
Expand All @@ -41,11 +41,11 @@ const constInitToVarCall = varCall;
>varCall : symbol

const constInitToConstDeclAmbient = constType;
>constInitToConstDeclAmbient : symbol
>constInitToConstDeclAmbient : unique symbol
>constType : unique symbol

let letInitToConstCall = constCall;
>letInitToConstCall : symbol
>letInitToConstCall : unique symbol
>constCall : unique symbol

let letInitToLetCall = letCall;
Expand All @@ -57,11 +57,11 @@ let letInitToVarCall = varCall;
>varCall : symbol

let letInitToConstDeclAmbient = constType;
>letInitToConstDeclAmbient : symbol
>letInitToConstDeclAmbient : unique symbol
>constType : unique symbol

var varInitToConstCall = constCall;
>varInitToConstCall : symbol
>varInitToConstCall : unique symbol
>constCall : unique symbol

var varInitToLetCall = letCall;
Expand All @@ -73,7 +73,7 @@ var varInitToVarCall = varCall;
>varCall : symbol

var varInitToConstDeclAmbient = constType;
>varInitToConstDeclAmbient : symbol
>varInitToConstDeclAmbient : unique symbol
>constType : unique symbol

// declaration from initializer with type query
Expand Down Expand Up @@ -201,19 +201,19 @@ declare const c: C;
>c : C

const constInitToCReadonlyStaticCall = C.readonlyStaticCall;
>constInitToCReadonlyStaticCall : symbol
>constInitToCReadonlyStaticCall : unique symbol
>C.readonlyStaticCall : unique symbol
>C : typeof C
>readonlyStaticCall : unique symbol

const constInitToCReadonlyStaticType = C.readonlyStaticType;
>constInitToCReadonlyStaticType : symbol
>constInitToCReadonlyStaticType : unique symbol
>C.readonlyStaticType : unique symbol
>C : typeof C
>readonlyStaticType : unique symbol

const constInitToCReadonlyStaticTypeAndCall = C.readonlyStaticTypeAndCall;
>constInitToCReadonlyStaticTypeAndCall : symbol
>constInitToCReadonlyStaticTypeAndCall : unique symbol
>C.readonlyStaticTypeAndCall : unique symbol
>C : typeof C
>readonlyStaticTypeAndCall : unique symbol
Expand Down Expand Up @@ -311,7 +311,7 @@ declare const i: I;
>i : I

const constInitToIReadonlyType = i.readonlyType;
>constInitToIReadonlyType : symbol
>constInitToIReadonlyType : unique symbol
>i.readonlyType : unique symbol
>i : I
>readonlyType : unique symbol
Expand Down Expand Up @@ -349,13 +349,13 @@ declare const l: L;
>l : L

const constInitToLReadonlyType = l.readonlyType;
>constInitToLReadonlyType : symbol
>constInitToLReadonlyType : unique symbol
>l.readonlyType : unique symbol
>l : L
>readonlyType : unique symbol

const constInitToLReadonlyNestedType = l.nested.readonlyNestedType;
>constInitToLReadonlyNestedType : symbol
>constInitToLReadonlyNestedType : unique symbol
>l.nested.readonlyNestedType : unique symbol
>l.nested : { readonly readonlyNestedType: unique symbol; }
>l : L
Expand Down Expand Up @@ -486,8 +486,8 @@ f(N["s"]);

// property assignments/methods
const o2 = {
>o2 : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: symbol): symbol; }
>{ a: s, b: N.s, c: N["s"], method1() { return s; }, async method2() { return s; }, async * method3() { yield s; }, * method4() { yield s; }, method5(p = s) { return p; },} : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: symbol): symbol; }
>o2 : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: unique symbol): symbol; }
>{ a: s, b: N.s, c: N["s"], method1() { return s; }, async method2() { return s; }, async * method3() { yield s; }, * method4() { yield s; }, method5(p = s) { return p; },} : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: unique symbol): symbol; }

a: s,
>a : symbol
Expand Down Expand Up @@ -524,10 +524,10 @@ const o2 = {
>s : unique symbol

method5(p = s) { return p; },
>method5 : (p?: symbol) => symbol
>p : symbol
>method5 : (p?: unique symbol) => symbol
Copy link
Member Author

Choose a reason for hiding this comment

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

These kinds of changes don't make me happy and probably indicate that this change is probably not good as-is.

>p : unique symbol
>s : unique symbol
>p : symbol
>p : unique symbol

};

Expand All @@ -536,65 +536,65 @@ class C0 {
>C0 : C0

static readonly a = s;
>a : symbol
>a : unique symbol
>s : unique symbol

static readonly b = N.s;
>b : symbol
>b : unique symbol
>N.s : unique symbol
>N : typeof N
>s : unique symbol

static readonly c = N["s"];
>c : symbol
>c : unique symbol
>N["s"] : unique symbol
>N : typeof N
>"s" : "s"

static d = s;
>d : symbol
>d : unique symbol
>s : unique symbol

static e = N.s;
>e : symbol
>e : unique symbol
>N.s : unique symbol
>N : typeof N
>s : unique symbol

static f = N["s"];
>f : symbol
>f : unique symbol
>N["s"] : unique symbol
>N : typeof N
>"s" : "s"

readonly a = s;
>a : symbol
>a : unique symbol
>s : unique symbol

readonly b = N.s;
>b : symbol
>b : unique symbol
>N.s : unique symbol
>N : typeof N
>s : unique symbol

readonly c = N["s"];
>c : symbol
>c : unique symbol
>N["s"] : unique symbol
>N : typeof N
>"s" : "s"

d = s;
>d : symbol
>d : unique symbol
>s : unique symbol

e = N.s;
>e : symbol
>e : unique symbol
>N.s : unique symbol
>N : typeof N
>s : unique symbol

f = N["s"];
>f : symbol
>f : unique symbol
>N["s"] : unique symbol
>N : typeof N
>"s" : "s"
Expand All @@ -618,10 +618,10 @@ class C0 {
>s : unique symbol

method5(p = s) { return p; }
>method5 : (p?: symbol) => symbol
>p : symbol
>method5 : (p?: unique symbol) => symbol
>p : unique symbol
>s : unique symbol
>p : symbol
>p : unique symbol
}

// non-widening positions
Expand Down
Loading