-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Prevent readonly symbols widening #54778
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
Andarist
wants to merge
6
commits into
microsoft:main
from
Andarist:prevent-readonly-symbols-widening
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78fe4f4
Prevent readonly unique symbols from widening
Andarist 203ba19
Widen symbols in mutable locations early
Andarist 1994e6c
Bring back symbol widening if the type is not accessible
Andarist 16456d7
Use `type.symbol.valueDeclaration` to compute visibility
Andarist 87dbb90
Add a new test case for the fixed case
Andarist d7dc3f9
Remove a redundant check
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11055,8 +11055,8 @@ 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)) { | ||
// always widen a 'unique symbol' type if the type was created for a different declaration and if it isn't accessible | ||
if (type.flags & TypeFlags.UniqueESSymbol && !declaration.type && type.symbol !== getSymbolOfDeclaration(declaration) && !isValueSymbolAccessible(type.symbol, type.symbol.valueDeclaration)) { | ||
type = esSymbolType; | ||
} | ||
|
||
|
@@ -37570,7 +37570,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
function widenTypeInferredFromInitializer(declaration: HasExpressionInitializer, type: Type) { | ||
const widened = getCombinedNodeFlagsCached(declaration) & NodeFlags.Constant || isDeclarationReadonly(declaration) ? type : getWidenedLiteralType(type); | ||
const widened = getCombinedNodeFlagsCached(declaration) & NodeFlags.Constant || isDeclarationReadonly(declaration) ? type : getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that perhaps |
||
if (isInJSFile(declaration)) { | ||
if (isEmptyLiteralType(widened)) { | ||
reportImplicitAny(declaration, anyType); | ||
|
36 changes: 36 additions & 0 deletions
36
tests/baselines/reference/indirectUniqueSymbolDeclarationEmit2.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//// [tests/cases/compiler/indirectUniqueSymbolDeclarationEmit2.ts] //// | ||
|
||
//// [indirectUniqueSymbolDeclarationEmit2.ts] | ||
// repro from https://github.com/microsoft/TypeScript/issues/53276 | ||
|
||
export const a = Symbol.toStringTag; | ||
|
||
export class F { | ||
[a](){ return "" } | ||
} | ||
|
||
export const b = (new F())[a]; | ||
|
||
|
||
//// [indirectUniqueSymbolDeclarationEmit2.js] | ||
"use strict"; | ||
// repro from https://github.com/microsoft/TypeScript/issues/53276 | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.b = exports.F = exports.a = void 0; | ||
exports.a = Symbol.toStringTag; | ||
var F = /** @class */ (function () { | ||
function F() { | ||
} | ||
F.prototype[exports.a] = function () { return ""; }; | ||
return F; | ||
}()); | ||
exports.F = F; | ||
exports.b = (new F())[exports.a]; | ||
|
||
|
||
//// [indirectUniqueSymbolDeclarationEmit2.d.ts] | ||
export declare const a: typeof Symbol.toStringTag; | ||
export declare class F { | ||
[a](): string; | ||
} | ||
export declare const b: () => string; |
24 changes: 24 additions & 0 deletions
24
tests/baselines/reference/indirectUniqueSymbolDeclarationEmit2.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//// [tests/cases/compiler/indirectUniqueSymbolDeclarationEmit2.ts] //// | ||
|
||
=== indirectUniqueSymbolDeclarationEmit2.ts === | ||
// repro from https://github.com/microsoft/TypeScript/issues/53276 | ||
|
||
export const a = Symbol.toStringTag; | ||
>a : Symbol(a, Decl(indirectUniqueSymbolDeclarationEmit2.ts, 2, 12)) | ||
>Symbol.toStringTag : Symbol(SymbolConstructor.toStringTag, Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) | ||
>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, --, --)) | ||
>toStringTag : Symbol(SymbolConstructor.toStringTag, Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) | ||
|
||
export class F { | ||
>F : Symbol(F, Decl(indirectUniqueSymbolDeclarationEmit2.ts, 2, 36)) | ||
|
||
[a](){ return "" } | ||
>[a] : Symbol(F[a], Decl(indirectUniqueSymbolDeclarationEmit2.ts, 4, 16)) | ||
>a : Symbol(a, Decl(indirectUniqueSymbolDeclarationEmit2.ts, 2, 12)) | ||
} | ||
|
||
export const b = (new F())[a]; | ||
>b : Symbol(b, Decl(indirectUniqueSymbolDeclarationEmit2.ts, 8, 12)) | ||
>F : Symbol(F, Decl(indirectUniqueSymbolDeclarationEmit2.ts, 2, 36)) | ||
>a : Symbol(a, Decl(indirectUniqueSymbolDeclarationEmit2.ts, 2, 12)) | ||
|
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/indirectUniqueSymbolDeclarationEmit2.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//// [tests/cases/compiler/indirectUniqueSymbolDeclarationEmit2.ts] //// | ||
|
||
=== indirectUniqueSymbolDeclarationEmit2.ts === | ||
// repro from https://github.com/microsoft/TypeScript/issues/53276 | ||
|
||
export const a = Symbol.toStringTag; | ||
>a : unique symbol | ||
>Symbol.toStringTag : unique symbol | ||
>Symbol : SymbolConstructor | ||
>toStringTag : unique symbol | ||
|
||
export class F { | ||
>F : F | ||
|
||
[a](){ return "" } | ||
>[a] : () => string | ||
>a : unique symbol | ||
>"" : "" | ||
} | ||
|
||
export const b = (new F())[a]; | ||
>b : () => string | ||
>(new F())[a] : () => string | ||
>(new F()) : F | ||
>new F() : F | ||
>F : typeof F | ||
>a : unique symbol | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/cases/compiler/indirectUniqueSymbolDeclarationEmit2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @strict: true | ||
// @lib: esnext | ||
// @declaration: true | ||
|
||
// repro from https://github.com/microsoft/TypeScript/issues/53276 | ||
|
||
export const a = Symbol.toStringTag; | ||
|
||
export class F { | ||
[a](){ return "" } | ||
} | ||
|
||
export const b = (new F())[a]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a feeling that passing
type.symbol.valueDeclaration
toisValueSymbolAccessible
here might not be 100% correct but so far I wasn't able to write a test that would fail with this