-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Fix abstract assignability restriction2 #4205
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3474,8 +3474,10 @@ namespace ts { | |
return emptyArray; | ||
} | ||
|
||
// Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and | ||
// maps primitive types and type parameters are to their apparent types. | ||
/** | ||
* Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and | ||
* maps primitive types and type parameters are to their apparent types. | ||
*/ | ||
function getSignaturesOfType(type: Type, kind: SignatureKind): Signature[] { | ||
return getSignaturesOfStructuredType(getApparentType(type), kind); | ||
} | ||
|
@@ -5090,30 +5092,24 @@ namespace ts { | |
let result = Ternary.True; | ||
let saveErrorInfo = errorInfo; | ||
|
||
// Because the "abstractness" of a class is the same across all construct signatures | ||
// (internally we are checking the corresponding declaration), it is enough to perform | ||
// the check and report an error once over all pairs of source and target construct signatures. | ||
let sourceSig = sourceSignatures[0]; | ||
// Note that in an extends-clause, targetSignatures is stripped, so the check never proceeds. | ||
let targetSig = targetSignatures[0]; | ||
|
||
if (sourceSig && targetSig) { | ||
let sourceErasedSignature = getErasedSignature(sourceSig); | ||
let targetErasedSignature = getErasedSignature(targetSig); | ||
|
||
let sourceReturnType = sourceErasedSignature && getReturnTypeOfSignature(sourceErasedSignature); | ||
let targetReturnType = targetErasedSignature && getReturnTypeOfSignature(targetErasedSignature); | ||
|
||
let sourceReturnDecl = sourceReturnType && sourceReturnType.symbol && getDeclarationOfKind(sourceReturnType.symbol, SyntaxKind.ClassDeclaration); | ||
let targetReturnDecl = targetReturnType && targetReturnType.symbol && getDeclarationOfKind(targetReturnType.symbol, SyntaxKind.ClassDeclaration); | ||
let sourceIsAbstract = sourceReturnDecl && sourceReturnDecl.flags & NodeFlags.Abstract; | ||
let targetIsAbstract = targetReturnDecl && targetReturnDecl.flags & NodeFlags.Abstract; | ||
if (kind === SignatureKind.Construct) { | ||
// Only want to compare the construct signatures for abstractness guarantees. | ||
|
||
// Because the "abstractness" of a class is the same across all construct signatures | ||
// (internally we are checking the corresponding declaration), it is enough to perform | ||
// the check and report an error once over all pairs of source and target construct signatures. | ||
// | ||
// sourceSig and targetSig are (possibly) undefined. | ||
// | ||
// Note that in an extends-clause, targetSignatures is stripped, so the check never proceeds. | ||
let sourceSig = sourceSignatures[0]; | ||
let targetSig = targetSignatures[0]; | ||
|
||
if (sourceIsAbstract && !targetIsAbstract) { | ||
if (reportErrors) { | ||
reportError(Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); | ||
} | ||
return Ternary.False; | ||
result &= abstractSignatureRelatedTo(source, sourceSig, target, targetSig); | ||
if (result !== Ternary.True) { | ||
return result; | ||
} | ||
} | ||
|
||
|
@@ -5137,6 +5133,40 @@ namespace ts { | |
} | ||
} | ||
return result; | ||
|
||
function abstractSignatureRelatedTo(source: Type, sourceSig: Signature, target: Type, targetSig: Signature) { | ||
if (sourceSig && targetSig) { | ||
|
||
let sourceDecl = source.symbol && getDeclarationOfKind(source.symbol, SyntaxKind.ClassDeclaration); | ||
let targetDecl = target.symbol && getDeclarationOfKind(target.symbol, SyntaxKind.ClassDeclaration); | ||
|
||
if (!sourceDecl) { | ||
// If the source object isn't itself a class declaration, it can be freely assigned, regardless | ||
// of whether the constructed object is abstract or not. | ||
return Ternary.True; | ||
} | ||
|
||
let sourceErasedSignature = getErasedSignature(sourceSig); | ||
let targetErasedSignature = getErasedSignature(targetSig); | ||
|
||
let sourceReturnType = sourceErasedSignature && getReturnTypeOfSignature(sourceErasedSignature); | ||
let targetReturnType = targetErasedSignature && getReturnTypeOfSignature(targetErasedSignature); | ||
|
||
let sourceReturnDecl = sourceReturnType && sourceReturnType.symbol && getDeclarationOfKind(sourceReturnType.symbol, SyntaxKind.ClassDeclaration); | ||
let targetReturnDecl = targetReturnType && targetReturnType.symbol && getDeclarationOfKind(targetReturnType.symbol, SyntaxKind.ClassDeclaration); | ||
let sourceIsAbstract = sourceReturnDecl && sourceReturnDecl.flags & NodeFlags.Abstract; | ||
let targetIsAbstract = targetReturnDecl && targetReturnDecl.flags & NodeFlags.Abstract; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider wrapping the |
||
|
||
if (sourceIsAbstract && !(targetIsAbstract && targetDecl)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you distribute the if (sourceIsAbstract && (!targetIsAbstract || !targetDecl)) { |
||
// if target isn't a class-declaration type, then it can be new'd, so we forbid the assignment. | ||
if (reportErrors) { | ||
reportError(Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); | ||
} | ||
return Ternary.False; | ||
} | ||
} | ||
return Ternary.True; | ||
} | ||
} | ||
|
||
function signatureRelatedTo(source: Signature, target: Signature, reportErrors: boolean): Ternary { | ||
|
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
19 changes: 19 additions & 0 deletions
19
tests/baselines/reference/classAbstractAssignabilityConstructorFunction.errors.txt
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,19 @@ | ||
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts(7,1): error TS2322: Type 'typeof A' is not assignable to type 'new () => A'. | ||
Cannot assign an abstract constructor type to a non-abstract constructor type. | ||
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts(8,1): error TS2322: Type 'string' is not assignable to type 'new () => A'. | ||
|
||
|
||
==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts (2 errors) ==== | ||
abstract class A { } | ||
|
||
// var AA: typeof A; | ||
var AAA: new() => A; | ||
|
||
// AA = A; // okay | ||
AAA = A; // error. | ||
~~~ | ||
!!! error TS2322: Type 'typeof A' is not assignable to type 'new () => A'. | ||
!!! error TS2322: Cannot assign an abstract constructor type to a non-abstract constructor type. | ||
AAA = "asdf"; | ||
~~~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'new () => A'. |
21 changes: 21 additions & 0 deletions
21
tests/baselines/reference/classAbstractAssignabilityConstructorFunction.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,21 @@ | ||
//// [classAbstractAssignabilityConstructorFunction.ts] | ||
abstract class A { } | ||
|
||
// var AA: typeof A; | ||
var AAA: new() => A; | ||
|
||
// AA = A; // okay | ||
AAA = A; // error. | ||
AAA = "asdf"; | ||
|
||
//// [classAbstractAssignabilityConstructorFunction.js] | ||
var A = (function () { | ||
function A() { | ||
} | ||
return A; | ||
})(); | ||
// var AA: typeof A; | ||
var AAA; | ||
// AA = A; // okay | ||
AAA = A; // error. | ||
AAA = "asdf"; |
8 changes: 8 additions & 0 deletions
8
...s/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts
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,8 @@ | ||
abstract class A { } | ||
|
||
// var AA: typeof A; | ||
var AAA: new() => A; | ||
|
||
// AA = A; // okay | ||
AAA = A; // error. | ||
AAA = "asdf"; |
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.
I think you could just make this
=