-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Omit late bound privates from declaration emit if their name is inaccessable #20169
Closed
weswigham
wants to merge
1
commit into
microsoft:master
from
weswigham:omit-late-bound-privates-with-unaccessable-names
Closed
Changes from all commits
Commits
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 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
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/privateSymbolNoDeclarationEmit.js
This file contains 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,23 @@ | ||
//// [privateSymbolNoDeclarationEmit.ts] | ||
const _data = Symbol('data'); | ||
|
||
export class User { | ||
private [_data] : any; | ||
}; | ||
|
||
//// [privateSymbolNoDeclarationEmit.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
var _data = Symbol('data'); | ||
var User = /** @class */ (function () { | ||
function User() { | ||
} | ||
return User; | ||
}()); | ||
exports.User = User; | ||
; | ||
|
||
|
||
//// [privateSymbolNoDeclarationEmit.d.ts] | ||
export declare class User { | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/privateSymbolNoDeclarationEmit.symbols
This file contains 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,12 @@ | ||
=== tests/cases/compiler/privateSymbolNoDeclarationEmit.ts === | ||
const _data = Symbol('data'); | ||
>_data : Symbol(_data, Decl(privateSymbolNoDeclarationEmit.ts, 0, 5)) | ||
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --)) | ||
|
||
export class User { | ||
>User : Symbol(User, Decl(privateSymbolNoDeclarationEmit.ts, 0, 29)) | ||
|
||
private [_data] : any; | ||
>_data : Symbol(_data, Decl(privateSymbolNoDeclarationEmit.ts, 0, 5)) | ||
|
||
}; |
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/privateSymbolNoDeclarationEmit.types
This file contains 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,14 @@ | ||
=== tests/cases/compiler/privateSymbolNoDeclarationEmit.ts === | ||
const _data = Symbol('data'); | ||
>_data : unique symbol | ||
>Symbol('data') : unique symbol | ||
>Symbol : SymbolConstructor | ||
>'data' : "data" | ||
|
||
export class User { | ||
>User : User | ||
|
||
private [_data] : any; | ||
>_data : unique symbol | ||
|
||
}; |
This file contains 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,7 @@ | ||
// @lib: es6 | ||
// @declaration: true | ||
const _data = Symbol('data'); | ||
|
||
export class User { | ||
private [_data] : any; | ||
}; |
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.
we want to keep the private in the type.. why remove late-bound names?
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.
If the name is impossible to posses outside of the local scope, then it is impossible to conflict with, ergo retaining the private holds no value.
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.
Plus, computed names like these were elided pre-dynamic-names anyway, so this is a conservative behavior.
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.
We do not know that.. you can have a function that returns you the same symbol.. the symbol can "escape" in multiple ways..
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.
In value-space, yes, the name can escape (all you'd need to do is alias the type or something); but it can't as far as our type system in concerned - if the late bound name is exported then we'll write the private, since it is visible and we know the name can be referenced elsewhere, if it is not visible, then there's no public way to access the same name, ergo you can't conflict with it (or at least we have no way of checking).
For instance:
getDataSymbol
returnssymbol
(not a reference to_data
'sunique symbol
), even though it's easy to see that you could make a subclass that attempts to mangle the same symbol, we have no way of verifying that it is, in fact, the same symbol (since it didn't come from the same origin).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.
And if you annotate
getDataSymbol
to explicitly returntypeof _data
, we'll happily tell you at that point in time that_data
isn't exported and cannot be named (and once it is exported we'll write the private member out).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.
The problem is this case (simplified):
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.
Hmm... I suppose the correct thing to check isn't visibility from the current declaration, but rather if the the name is reachable via the exports of its containing file at all. And then adding a visibility check to ensure an alias to it is visible in scope and complaining that it cannot be referenced if not. Meaning
is an error because
x
cannot be named in the declaration file (without modifying its shape, anyway), whileis fine since the name can be written by its alias from
a
.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.
so why not just keep the private, and the const with the unique symbol, but just add a
export {};
to the file to make the scoping work correctly.. we talked about this before in the context of other declaration emit issues.