From ba574725c2d01345446f3cdb5135f2cde980d9da Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Thu, 11 Sep 2014 18:08:10 -0700 Subject: [PATCH] Check that private properties originate in the same declaration --- src/compiler/checker.ts | 10 +++--- .../objectTypesIdentityWithPrivates3.js | 36 +++++++++++++++++++ .../objectTypesIdentityWithPrivates3.types | 35 ++++++++++++++++++ .../objectTypesIdentityWithPrivates3.ts | 13 +++++++ 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/objectTypesIdentityWithPrivates3.js create mode 100644 tests/baselines/reference/objectTypesIdentityWithPrivates3.types create mode 100644 tests/cases/conformance/types/typeRelationships/typeAndMemberIdentity/objectTypesIdentityWithPrivates3.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c96b2edc5b1..6e45ea4f4e5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2888,10 +2888,12 @@ module ts { } if (getDeclarationFlagsFromSymbol(sourceProp) & NodeFlags.Private || getDeclarationFlagsFromSymbol(targetProp) & NodeFlags.Private) { - if (reportErrors) { - reportError(Diagnostics.Private_property_0_cannot_be_reimplemented, symbolToString(targetProp)); + if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) { + if (reportErrors) { + reportError(Diagnostics.Private_property_0_cannot_be_reimplemented, symbolToString(targetProp)); + } + return false; } - return false; } if (!isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors)) { if (reportErrors) { @@ -4338,7 +4340,7 @@ module ts { var widenedType = getWidenedType(exprType, /*supressNoImplicitAnyErrors*/ true); if (!(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); - } + } } return targetType; } diff --git a/tests/baselines/reference/objectTypesIdentityWithPrivates3.js b/tests/baselines/reference/objectTypesIdentityWithPrivates3.js new file mode 100644 index 00000000000..bc71d791a61 --- /dev/null +++ b/tests/baselines/reference/objectTypesIdentityWithPrivates3.js @@ -0,0 +1,36 @@ +//// [objectTypesIdentityWithPrivates3.ts] +interface T1 { } +interface T2 { z } + +class C1 { + private x; +} + +class C2 extends C1 { + y; +} + +var c1: C1; +c1; // Should succeed (private x originates in the same declaration) + +//// [objectTypesIdentityWithPrivates3.js] +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +var C1 = (function () { + function C1() { + } + return C1; +})(); +var C2 = (function (_super) { + __extends(C2, _super); + function C2() { + _super.apply(this, arguments); + } + return C2; +})(C1); +var c1; +c1; // Should succeed (private x originates in the same declaration) diff --git a/tests/baselines/reference/objectTypesIdentityWithPrivates3.types b/tests/baselines/reference/objectTypesIdentityWithPrivates3.types new file mode 100644 index 00000000000..d770ededf8c --- /dev/null +++ b/tests/baselines/reference/objectTypesIdentityWithPrivates3.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/types/typeRelationships/typeAndMemberIdentity/objectTypesIdentityWithPrivates3.ts === +interface T1 { } +>T1 : T1 + +interface T2 { z } +>T2 : T2 +>z : any + +class C1 { +>C1 : C1 +>T : T + + private x; +>x : any +} + +class C2 extends C1 { +>C2 : C2 +>C1 : C1 +>T1 : T1 + + y; +>y : any +} + +var c1: C1; +>c1 : C1 +>C1 : C1 +>T2 : T2 + +c1; // Should succeed (private x originates in the same declaration) +>c1 : C2 +>C2 : C2 +>c1 : C1 + diff --git a/tests/cases/conformance/types/typeRelationships/typeAndMemberIdentity/objectTypesIdentityWithPrivates3.ts b/tests/cases/conformance/types/typeRelationships/typeAndMemberIdentity/objectTypesIdentityWithPrivates3.ts new file mode 100644 index 00000000000..1d6997f1a4a --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/typeAndMemberIdentity/objectTypesIdentityWithPrivates3.ts @@ -0,0 +1,13 @@ +interface T1 { } +interface T2 { z } + +class C1 { + private x; +} + +class C2 extends C1 { + y; +} + +var c1: C1; +c1; // Should succeed (private x originates in the same declaration) \ No newline at end of file