From d0924f43f031d95447822f2e74651fca2529115a Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 22 Jun 2015 12:05:40 -0700 Subject: [PATCH] moved abstract-method-inheritance test to checkKindsOfPropertyMemberOverrides, changed error message --- src/compiler/checker.ts | 26 +++++-------------- .../classAbstractDeclarations.d.errors.txt | 18 +++++-------- .../classAbstractInheritance.errors.txt | 18 +++++-------- .../classWithAbstractMethods.errors.txt | 12 +++------ 4 files changed, 23 insertions(+), 51 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bf455b87e08..3e0a04b123e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2197,8 +2197,8 @@ namespace ts { // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, // or otherwise the type of the string index signature. type = getTypeOfPropertyOfType(parentType, name.text) || - isNumericLiteralName(name.text) && getIndexTypeOfType(parentType, IndexKind.Number) || - getIndexTypeOfType(parentType, IndexKind.String); + isNumericLiteralName(name.text) && getIndexTypeOfType(parentType, IndexKind.Number) || + getIndexTypeOfType(parentType, IndexKind.String); if (!type) { error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name)); return unknownType; @@ -4584,21 +4584,7 @@ namespace ts { for (let targetProp of properties) { let sourceProp = getPropertyOfType(source, targetProp.name); - if (sourceProp === targetProp) { // source inherits targetProp and doesn't redeclare/override it. - if (source.flags & TypeFlags.Class && target.flags & TypeFlags.Class) { - let targetPropFlags = getDeclarationFlagsFromSymbol(targetProp); - let sourceDecl = getDeclarationOfKind(source.symbol, SyntaxKind.ClassDeclaration); - - // if target is a class and it has an abstract method, then source, inheriting that method, must be declared abstract. - if (targetPropFlags & NodeFlags.Abstract && !(sourceDecl.flags & NodeFlags.Abstract)) { - if (reportErrors) { - reportError(Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_2, - typeToString(source), typeToString(target), symbolToString(targetProp)); - } - return Ternary.False; - } - } - } else { // sourceProp !== targetProp -- ie: source and target have distinct declarations with the same name + if (sourceProp !== targetProp) { // sourceProp !== targetProp -- ie: source and target have distinct declarations with the same name if (!sourceProp) { if (!(targetProp.flags & SymbolFlags.Optional) || requireOptionalProperties) { if (reportErrors) { @@ -10785,7 +10771,9 @@ namespace ts { let derived = getTargetSymbol(getPropertyOfObjectType(type, base.name)); let baseDeclarationFlags = getDeclarationFlagsFromSymbol(base); - if (!derived) { // derived class inherits base without override/redeclaration + Debug.assert(!!derived, "derived should point to something, even if it is the base class' declaration."); + + if (derived === base) { // derived class inherits base without override/redeclaration let derivedClassDecl = getDeclarationOfKind(type.symbol, SyntaxKind.ClassDeclaration); Debug.assert(derivedClassDecl !== undefined); @@ -10794,7 +10782,7 @@ namespace ts { error(derivedClassDecl, Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_2, typeToString(type), typeToString(baseType), symbolToString(baseProperty)); } - } else { // derived !== undefined -- derived overrides base + } else { // derived overrides base let derivedDeclarationFlags = getDeclarationFlagsFromSymbol(derived); if ((baseDeclarationFlags & NodeFlags.Private) || (derivedDeclarationFlags & NodeFlags.Private)) { // either base or derived property is private - not override, skip it diff --git a/tests/baselines/reference/classAbstractDeclarations.d.errors.txt b/tests/baselines/reference/classAbstractDeclarations.d.errors.txt index aef63b296e7..068a1273bb8 100644 --- a/tests/baselines/reference/classAbstractDeclarations.d.errors.txt +++ b/tests/baselines/reference/classAbstractDeclarations.d.errors.txt @@ -1,12 +1,9 @@ tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(2,5): error TS1236: 'abstract' modifier can only appear on a class or member function declaration. tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(2,5): error TS1240: Abstract member functions cannot have an implementation. tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(2,28): error TS1184: An implementation cannot be declared in ambient contexts. -tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(11,15): error TS2415: Class 'CC' incorrectly extends base class 'AA'. - Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. -tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(13,15): error TS2415: Class 'DD' incorrectly extends base class 'BB'. - Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. -tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(17,15): error TS2415: Class 'FF' incorrectly extends base class 'CC'. - Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. +tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(11,15): error TS2515: Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. +tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(13,15): error TS2515: Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. +tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts(17,15): error TS2515: Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. ==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractDeclarations.d.ts (6 errors) ==== @@ -28,20 +25,17 @@ tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbst declare class CC extends AA {} ~~ -!!! error TS2415: Class 'CC' incorrectly extends base class 'AA'. -!!! error TS2415: Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. +!!! error TS2515: Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. declare class DD extends BB {} ~~ -!!! error TS2415: Class 'DD' incorrectly extends base class 'BB'. -!!! error TS2415: Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. +!!! error TS2515: Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. declare abstract class EE extends BB {} declare class FF extends CC {} ~~ -!!! error TS2415: Class 'FF' incorrectly extends base class 'CC'. -!!! error TS2415: Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. +!!! error TS2515: Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. declare abstract class GG extends CC {} diff --git a/tests/baselines/reference/classAbstractInheritance.errors.txt b/tests/baselines/reference/classAbstractInheritance.errors.txt index 39e209b2e05..a663a466757 100644 --- a/tests/baselines/reference/classAbstractInheritance.errors.txt +++ b/tests/baselines/reference/classAbstractInheritance.errors.txt @@ -1,9 +1,6 @@ -tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts(13,7): error TS2415: Class 'CC' incorrectly extends base class 'AA'. - Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. -tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts(15,7): error TS2415: Class 'DD' incorrectly extends base class 'BB'. - Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. -tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts(19,7): error TS2415: Class 'FF' incorrectly extends base class 'CC'. - Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. +tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts(13,7): error TS2515: Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. +tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts(15,7): error TS2515: Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. +tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts(19,7): error TS2515: Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. ==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts (3 errors) ==== @@ -21,19 +18,16 @@ tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbst class CC extends AA {} ~~ -!!! error TS2415: Class 'CC' incorrectly extends base class 'AA'. -!!! error TS2415: Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. +!!! error TS2515: Non-abstract class 'CC' does not implement inherited abstract member 'AA.foo'. class DD extends BB {} ~~ -!!! error TS2415: Class 'DD' incorrectly extends base class 'BB'. -!!! error TS2415: Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. +!!! error TS2515: Non-abstract class 'DD' does not implement inherited abstract member 'BB.foo'. abstract class EE extends BB {} class FF extends CC {} ~~ -!!! error TS2415: Class 'FF' incorrectly extends base class 'CC'. -!!! error TS2415: Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. +!!! error TS2515: Non-abstract class 'FF' does not implement inherited abstract member 'CC.foo'. abstract class GG extends CC {} \ No newline at end of file diff --git a/tests/baselines/reference/classWithAbstractMethods.errors.txt b/tests/baselines/reference/classWithAbstractMethods.errors.txt index 37eba4a6a86..d66303289ab 100644 --- a/tests/baselines/reference/classWithAbstractMethods.errors.txt +++ b/tests/baselines/reference/classWithAbstractMethods.errors.txt @@ -1,9 +1,7 @@ tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(1,7): error TS2514: Classes containing abstract functions must be marked abstract. tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(2,5): error TS1238: Abstract methods can only appear within an abstract class. -tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(5,7): error TS2415: Class 'B' incorrectly extends base class 'A'. - Non-abstract class 'B' does not implement inherited abstract member 'A.foo'. -tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(21,7): error TS2415: Class 'BB' incorrectly extends base class 'AA'. - Non-abstract class 'BB' does not implement inherited abstract member 'AA.foo'. +tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(5,7): error TS2515: Non-abstract class 'B' does not implement inherited abstract member 'A.foo'. +tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(21,7): error TS2515: Non-abstract class 'BB' does not implement inherited abstract member 'AA.foo'. ==== tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts (4 errors) ==== @@ -17,8 +15,7 @@ tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(21 class B extends A {} ~ -!!! error TS2415: Class 'B' incorrectly extends base class 'A'. -!!! error TS2415: Non-abstract class 'B' does not implement inherited abstract member 'A.foo'. +!!! error TS2515: Non-abstract class 'B' does not implement inherited abstract member 'A.foo'. abstract class C extends A {} @@ -36,8 +33,7 @@ tests/cases/conformance/classes/classDeclarations/classWithAbstractMethods.ts(21 class BB extends AA {} ~~ -!!! error TS2415: Class 'BB' incorrectly extends base class 'AA'. -!!! error TS2415: Non-abstract class 'BB' does not implement inherited abstract member 'AA.foo'. +!!! error TS2515: Non-abstract class 'BB' does not implement inherited abstract member 'AA.foo'. abstract class CC extends AA {}