Skip to content

Fix crash when this is typed as a generic T with no type constraints #47991

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

Merged
merged 6 commits into from
Mar 5, 2022
Merged
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
26 changes: 18 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20621,7 +20621,7 @@ namespace ts {

// Return true if the given class derives from each of the declaring classes of the protected
// constituents of the given property.
function isClassDerivedFromDeclaringClasses(checkClass: Type, prop: Symbol, writing: boolean) {
function isClassDerivedFromDeclaringClasses<T extends Type>(checkClass: T, prop: Symbol, writing: boolean) {
return forEachProperty(prop, p => getDeclarationModifierFlagsFromSymbol(p, writing) & ModifierFlags.Protected ?
!hasBaseType(checkClass, getDeclaringClass(p)) : false) ? undefined : checkClass;
}
Expand Down Expand Up @@ -28315,14 +28315,15 @@ namespace ts {
// of the property as base classes
let enclosingClass = forEachEnclosingClass(location, enclosingDeclaration => {
const enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration)!) as InterfaceType;
return isClassDerivedFromDeclaringClasses(enclosingClass, prop, writing) ? enclosingClass : undefined;
return isClassDerivedFromDeclaringClasses(enclosingClass, prop, writing);
});
// A protected property is accessible if the property is within the declaring class or classes derived from it
if (!enclosingClass) {
// allow PropertyAccessibility if context is in function with this parameter
// static member access is disallow
let thisParameter: ParameterDeclaration | undefined;
if (flags & ModifierFlags.Static || !(thisParameter = getThisParameterFromNodeContext(location)) || !thisParameter.type) {
// static member access is disallowed
enclosingClass = getEnclosingClassFromThisParameter(location);
enclosingClass = enclosingClass && isClassDerivedFromDeclaringClasses(enclosingClass, prop, writing);
if (flags & ModifierFlags.Static || !enclosingClass) {
if (errorNode) {
error(errorNode,
Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses,
Expand All @@ -28331,9 +28332,6 @@ namespace ts {
}
return false;
}

const thisType = getTypeFromTypeNode(thisParameter.type);
enclosingClass = (((thisType.flags & TypeFlags.TypeParameter) ? getConstraintOfTypeParameter(thisType as TypeParameter) : thisType) as TypeReference).target;
}
// No further restrictions for static properties
if (flags & ModifierFlags.Static) {
Expand All @@ -28354,6 +28352,18 @@ namespace ts {
return true;
}

function getEnclosingClassFromThisParameter(node: Node): InterfaceType | undefined {
const thisParameter = getThisParameterFromNodeContext(node);
let thisType = thisParameter?.type && getTypeFromTypeNode(thisParameter.type);
if (thisType && thisType.flags & TypeFlags.TypeParameter) {
thisType = getConstraintOfTypeParameter(thisType as TypeParameter);
}
if (thisType && getObjectFlags(thisType) & (ObjectFlags.ClassOrInterface | ObjectFlags.Reference)) {
return getTargetType(thisType) as InterfaceType;
}
return undefined;
}

function getThisParameterFromNodeContext(node: Node) {
const thisContainer = getThisContainer(node, /* includeArrowFunctions */ false);
return thisContainer && isFunctionLike(thisContainer) ? getThisParameter(thisContainer) : undefined;
Expand Down
147 changes: 147 additions & 0 deletions tests/baselines/reference/protectedMembersThisParameter.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
tests/cases/compiler/protectedMembersThisParameter.ts(9,9): error TS2445: Property 'secret' is protected and only accessible within class 'Message' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(30,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(41,7): error TS2446: Property 'a' is protected and only accessible through an instance of class 'C'. This is an instance of class 'B'.
tests/cases/compiler/protectedMembersThisParameter.ts(42,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(46,7): error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(47,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(51,7): error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(52,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(55,7): error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(56,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(64,9): error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(68,9): error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(76,9): error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(77,9): error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(80,9): error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses.
tests/cases/compiler/protectedMembersThisParameter.ts(81,9): error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses.


==== tests/cases/compiler/protectedMembersThisParameter.ts (16 errors) ====
class Message {
protected secret(): void {}
}
class MessageWrapper {
message: Message = new Message();
wrap<T>() {
let m = this.message;
let f = function(this: T) {
m.secret(); // should error
~~~~~~
!!! error TS2445: Property 'secret' is protected and only accessible within class 'Message' and its subclasses.
}
}
}

class A {
protected a() {}
}
class B extends A {
protected b() {}
}
class C extends A {
protected c() {}
}
class Z {
protected z() {}
}

function bA<T extends A>(this: T, arg: B) {
this.a();
arg.a();
arg.b(); // should error to avoid cross-hierarchy protected access https://www.typescriptlang.org/docs/handbook/2/classes.html#cross-hierarchy-protected-access
~
!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
}
function bB<T extends B>(this: T, arg: B) {
this.a();
this.b();
arg.a();
arg.b();
}
function bC<T extends C>(this: T, arg: B) {
this.a();
this.c();
arg.a(); // should error
~
!!! error TS2446: Property 'a' is protected and only accessible through an instance of class 'C'. This is an instance of class 'B'.
arg.b(); // should error
~
!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
}
function bZ<T extends Z>(this: T, arg: B) {
this.z();
arg.a(); // should error
~
!!! error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses.
arg.b(); // should error
~
!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
}
function bString<T extends string>(this: T, arg: B) {
this.toLowerCase();
arg.a(); // should error
~
!!! error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses.
arg.b(); // should error
~
!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
}
function bAny<T>(this: T, arg: B) {
arg.a(); // should error
~
!!! error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses.
arg.b(); // should error
~
!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses.
}

class D {
protected d() {}

derived1(arg: D1) {
arg.d();
arg.d1(); // should error
~~
!!! error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses.
}
derived1ThisD(this: D, arg: D1) {
arg.d();
arg.d1(); // should error
~~
!!! error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses.
}
derived1ThisD1(this: D1, arg: D1) {
arg.d();
arg.d1();
}

derived2(arg: D2) {
arg.d(); // should error because of overridden method in D2
~
!!! error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses.
arg.d2(); // should error
~~
!!! error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses.
}
derived2ThisD(this: D, arg: D2) {
arg.d(); // should error because of overridden method in D2
~
!!! error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses.
arg.d2(); // should error
~~
!!! error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses.
}
derived2ThisD2(this: D2, arg: D2) {
arg.d();
arg.d2();
}
}
class D1 extends D {
protected d1() {}
}
class D2 extends D {
protected d() {}
protected d2() {}
}


Loading