Skip to content
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

Allow protected members to be accessed within the enclosing class #60001

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #59989

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Sep 18, 2024
@@ -33868,7 +33868,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// get the original type -- represented as the type constraint of the 'this' type
containingType = (containingType as TypeParameter).isThisType ? getConstraintOfTypeParameter(containingType as TypeParameter)! : getBaseConstraintOfType(containingType as TypeParameter)!; // TODO: GH#18217 Use a different variable that's allowed to be undefined
}
if (!containingType || !hasBaseType(containingType, enclosingClass)) {
if (!containingType || !hasBaseType(containingType, enclosingClass) && !isNodeWithinClass(location, getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)!)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the added check is the same one as for private properties a couple of lines above this (in this function)

Copy link
Member

Choose a reason for hiding this comment

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

It does besides the null checking; is it really safe to ! all of these? The other case doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other case does exactly the same. I copy-pasted this :p

        // Private property is accessible if the property is within the declaring class
        if (flags & ModifierFlags.Private) {
            const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)!;
            if (!isNodeWithinClass(location, declaringClassDeclaration)) {
                if (errorNode) {
                    error(errorNode, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(getDeclaringClass(prop)!));
                }
                return false;
            }
            return true;
        }

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yeah, I can't read

Copy link
Member

Choose a reason for hiding this comment

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

Why don't we just do this check unconditionally at the top? It sure seems like the result of this code is that "you can access any property of the current class as long as you're within that class", such that we don't need to do all of this runaround.

@Andarist Andarist force-pushed the fix/protected-members-within-class branch from 71bbed7 to f00d00b Compare September 19, 2024 16:42
@jakebailey
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 24, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

@jakebailey
Copy link
Member

In a way this feels wrong; I remember on my first checker bugfix that we didn't want to let one spread out private props which feels similar: #47078 (comment)

But, allowing private and not protected seems the oddest of all.

@typescript-bot
Copy link
Collaborator

Hey @jakebailey, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@Andarist
Copy link
Contributor Author

Honestly, I don't really know what are the expectations around private/protected when it comes to closures etc. Private properties are allowed in those positions quite explicitly by the code (and IIRC there are tests for it) so this merely matches the behavior of protected properties with that.

I don't feel anything in the linked comment says strongly this might be wrong:

access to private/protected members is only supposed to be allowed through references of the actual class type from within the class

Those references to this class here are within the class ;p I feel like the linked comment is mainly about object types created by spreads than about other cases like this here.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user tests with tsc comparing main and refs/pull/60001/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,340 62,340 ~ ~ ~ p=1.000 n=6
Types 50,378 50,378 ~ ~ ~ p=1.000 n=6
Memory used 193,272k (± 0.74%) 195,068k (± 0.93%) +1,797k (+ 0.93%) 192,674k 196,305k p=0.045 n=6
Parse Time 1.57s (± 0.74%) 1.57s (± 0.74%) ~ 1.55s 1.58s p=0.868 n=6
Bind Time 0.86s (± 1.14%) 0.86s (± 0.60%) ~ 0.86s 0.87s p=0.931 n=6
Check Time 11.35s (± 0.25%) 11.33s (± 0.36%) ~ 11.28s 11.39s p=0.520 n=6
Emit Time 3.21s (± 0.47%) 3.23s (± 0.39%) ~ 3.22s 3.25s p=0.137 n=6
Total Time 17.00s (± 0.21%) 17.00s (± 0.19%) ~ 16.97s 17.04s p=1.000 n=6
angular-1 - node (v18.15.0, x64)
Errors 7 7 ~ ~ ~ p=1.000 n=6
Symbols 947,102 947,102 ~ ~ ~ p=1.000 n=6
Types 410,738 410,738 ~ ~ ~ p=1.000 n=6
Memory used 1,224,014k (± 0.00%) 1,223,954k (± 0.00%) ~ 1,223,902k 1,224,026k p=0.093 n=6
Parse Time 7.89s (± 0.62%) 7.92s (± 0.66%) ~ 7.84s 7.99s p=0.571 n=6
Bind Time 2.25s (± 0.96%) 2.23s (± 0.66%) ~ 2.21s 2.24s p=0.151 n=6
Check Time 36.58s (± 0.39%) 36.48s (± 0.40%) ~ 36.31s 36.74s p=0.297 n=6
Emit Time 17.90s (± 0.37%) 17.94s (± 0.59%) ~ 17.79s 18.06s p=0.575 n=6
Total Time 64.62s (± 0.24%) 64.56s (± 0.29%) ~ 64.32s 64.82s p=0.575 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,521,651 2,521,651 ~ ~ ~ p=1.000 n=6
Types 936,037 936,037 ~ ~ ~ p=1.000 n=6
Memory used 2,351,772k (± 0.00%) 2,351,782k (± 0.00%) ~ 2,351,725k 2,351,846k p=0.936 n=6
Parse Time 11.01s (± 0.30%) 11.00s (± 0.25%) ~ 10.96s 11.04s p=0.468 n=6
Bind Time 2.56s (± 0.46%) 2.56s (± 0.21%) ~ 2.55s 2.56s p=0.342 n=6
Check Time 86.74s (± 0.48%) 86.74s (± 0.67%) ~ 86.02s 87.50s p=1.000 n=6
Emit Time 0.33s (± 1.68%) 0.32s (± 2.36%) ~ 0.31s 0.33s p=0.137 n=6
Total Time 100.64s (± 0.41%) 100.62s (± 0.59%) ~ 99.90s 101.39s p=0.936 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,250,056 1,250,056 ~ ~ ~ p=1.000 n=6
Types 264,970 264,970 ~ ~ ~ p=1.000 n=6
Memory used 2,404,663k (± 0.02%) 2,405,152k (± 0.03%) ~ 2,404,132k 2,406,434k p=0.230 n=6
Parse Time 7.66s (± 0.75%) 7.65s (± 0.58%) ~ 7.59s 7.72s p=0.748 n=6
Bind Time 2.82s (± 0.63%) 2.80s (± 0.66%) ~ 2.78s 2.83s p=0.168 n=6
Check Time 51.44s (± 0.56%) 51.60s (± 0.34%) ~ 51.31s 51.82s p=0.521 n=6
Emit Time 4.44s (± 3.16%) 4.37s (± 0.90%) ~ 4.30s 4.41s p=0.688 n=6
Total Time 66.37s (± 0.36%) 66.44s (± 0.31%) ~ 66.11s 66.73s p=0.810 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,250,056 1,250,056 ~ ~ ~ p=1.000 n=6
Types 264,970 264,970 ~ ~ ~ p=1.000 n=6
Memory used 2,479,123k (± 0.02%) 2,479,521k (± 0.04%) ~ 2,478,149k 2,480,427k p=0.471 n=6
Parse Time 7.88s (± 1.04%) 7.87s (± 1.14%) ~ 7.76s 7.97s p=0.936 n=6
Bind Time 2.53s (± 1.12%) 2.54s (± 0.41%) ~ 2.52s 2.55s p=0.747 n=6
Check Time 51.93s (± 0.62%) 51.86s (± 0.46%) ~ 51.49s 52.14s p=0.521 n=6
Emit Time 4.39s (± 1.14%) 4.41s (± 3.95%) ~ 4.29s 4.76s p=0.421 n=6
Total Time 66.73s (± 0.59%) 66.69s (± 0.35%) ~ 66.40s 67.04s p=0.689 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 260,936 260,936 ~ ~ ~ p=1.000 n=6
Types 106,385 106,385 ~ ~ ~ p=1.000 n=6
Memory used 436,629k (± 0.01%) 436,643k (± 0.01%) ~ 436,599k 436,671k p=0.575 n=6
Parse Time 4.26s (± 0.43%) 4.26s (± 0.91%) ~ 4.21s 4.31s p=0.935 n=6
Bind Time 1.60s (± 0.32%) 1.61s (± 0.73%) ~ 1.59s 1.62s p=0.351 n=6
Check Time 22.40s (± 0.45%) 22.46s (± 0.46%) ~ 22.37s 22.65s p=0.630 n=6
Emit Time 1.86s (± 1.00%) 1.86s (± 1.00%) ~ 1.84s 1.89s p=0.935 n=6
Total Time 30.13s (± 0.38%) 30.19s (± 0.35%) ~ 30.06s 30.37s p=0.518 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 68 68 ~ ~ ~ p=1.000 n=6
Symbols 225,916 225,916 ~ ~ ~ p=1.000 n=6
Types 94,414 94,414 ~ ~ ~ p=1.000 n=6
Memory used 370,865k (± 0.02%) 370,875k (± 0.01%) ~ 370,805k 370,952k p=0.521 n=6
Parse Time 2.76s (± 0.62%) 2.77s (± 0.87%) ~ 2.73s 2.79s p=0.222 n=6
Bind Time 1.56s (± 1.69%) 1.57s (± 1.24%) ~ 1.54s 1.59s p=0.627 n=6
Check Time 15.77s (± 0.26%) 15.83s (± 0.27%) ~ 15.79s 15.90s p=0.145 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.10s (± 0.17%) 20.17s (± 0.21%) +0.07s (+ 0.36%) 20.10s 20.23s p=0.020 n=6
vscode - node (v18.15.0, x64)
Errors 1 1 ~ ~ ~ p=1.000 n=6
Symbols 3,099,752 3,099,752 ~ ~ ~ p=1.000 n=6
Types 1,068,967 1,068,967 ~ ~ ~ p=1.000 n=6
Memory used 3,196,818k (± 0.00%) 3,196,764k (± 0.00%) -54k (- 0.00%) 3,196,715k 3,196,795k p=0.037 n=6
Parse Time 11.66s (± 0.27%) 11.69s (± 0.33%) ~ 11.66s 11.76s p=0.465 n=6
Bind Time 3.69s (± 1.96%) 3.67s (± 0.24%) ~ 3.66s 3.68s p=0.804 n=6
Check Time 71.25s (± 0.19%) 71.06s (± 0.33%) ~ 70.64s 71.34s p=0.092 n=6
Emit Time 18.94s (± 0.48%) 18.90s (± 0.37%) ~ 18.81s 19.02s p=0.809 n=6
Total Time 105.55s (± 0.14%) 105.32s (± 0.22%) ~ 104.95s 105.65s p=0.066 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 278,920 278,920 ~ ~ ~ p=1.000 n=6
Types 113,837 113,837 ~ ~ ~ p=1.000 n=6
Memory used 428,357k (± 0.02%) 428,383k (± 0.02%) ~ 428,262k 428,456k p=0.689 n=6
Parse Time 3.94s (± 0.37%) 3.95s (± 0.21%) ~ 3.94s 3.96s p=0.099 n=6
Bind Time 1.71s (± 0.44%) 1.72s (± 0.52%) ~ 1.71s 1.73s p=0.149 n=6
Check Time 17.65s (± 0.52%) 17.60s (± 0.31%) ~ 17.53s 17.67s p=0.470 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 23.30s (± 0.40%) 23.28s (± 0.25%) ~ 23.22s 23.35s p=0.936 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 540,027 540,027 ~ ~ ~ p=1.000 n=6
Types 181,292 181,292 ~ ~ ~ p=1.000 n=6
Memory used 483,567k (± 0.01%) 483,560k (± 0.00%) ~ 483,513k 483,581k p=0.378 n=6
Parse Time 2.70s (± 0.47%) 2.70s (± 0.43%) ~ 2.68s 2.71s p=0.931 n=6
Bind Time 0.96s (± 0.43%) 0.96s (± 0.43%) ~ 0.95s 0.96s p=1.000 n=6
Check Time 15.68s (± 0.22%) 15.72s (± 0.32%) ~ 15.66s 15.78s p=0.197 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 19.34s (± 0.20%) 19.39s (± 0.31%) ~ 19.31s 19.46s p=0.172 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top 400 repos with tsc comparing main and refs/pull/60001/merge:

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Cannot access protected field but private field inside nested class
3 participants