-
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
Reduce intersections with conflicting privates, elaborate on reasons #37762
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3aa4762
Elaborate on reasons for 'never' intersections
ahejlsberg 7bf375f
Accept new API baselines
ahejlsberg 3197c17
Accept new baselines
ahejlsberg a3b90b8
Add tests
ahejlsberg a4c8b6e
Merge branch 'master' into elaborateNeverIntersections
ahejlsberg 8870838
Accept new baselines
ahejlsberg 85db315
Address CR feedback
ahejlsberg 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 | ||||
---|---|---|---|---|---|---|
|
@@ -4148,7 +4148,9 @@ namespace ts { | |||||
return undefined!; // TODO: GH#18217 | ||||||
} | ||||||
|
||||||
type = getReducedType(type); | ||||||
if (!(context.flags & NodeBuilderFlags.NoTypeReduction)) { | ||||||
type = getReducedType(type); | ||||||
} | ||||||
|
||||||
if (type.flags & TypeFlags.Any) { | ||||||
context.approximateLength += 3; | ||||||
|
@@ -8460,17 +8462,20 @@ namespace ts { | |||||
error(baseTypeNode.expression, Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); | ||||||
return type.resolvedBaseTypes = emptyArray; | ||||||
} | ||||||
baseType = getReducedType(getReturnTypeOfSignature(constructors[0])); | ||||||
baseType = getReturnTypeOfSignature(constructors[0]); | ||||||
} | ||||||
|
||||||
if (baseType === errorType) { | ||||||
return type.resolvedBaseTypes = emptyArray; | ||||||
} | ||||||
if (!isValidBaseType(baseType)) { | ||||||
error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_an_object_type_or_intersection_of_object_types_with_statically_known_members, typeToString(baseType)); | ||||||
const reducedBaseType = getReducedType(baseType); | ||||||
if (!isValidBaseType(reducedBaseType)) { | ||||||
const elaboration = elaborateNeverIntersection(/*errorInfo*/ undefined, baseType); | ||||||
const diagnostic = chainDiagnosticMessages(elaboration, Diagnostics.Base_constructor_return_type_0_is_not_an_object_type_or_intersection_of_object_types_with_statically_known_members, typeToString(reducedBaseType)); | ||||||
diagnostics.add(createDiagnosticForNodeFromMessageChain(baseTypeNode.expression, diagnostic)); | ||||||
return type.resolvedBaseTypes = emptyArray; | ||||||
} | ||||||
if (type === baseType || hasBaseType(baseType, type)) { | ||||||
if (type === reducedBaseType || hasBaseType(reducedBaseType, type)) { | ||||||
error(type.symbol.valueDeclaration, Diagnostics.Type_0_recursively_references_itself_as_a_base_type, | ||||||
typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.WriteArrayAsGenericType)); | ||||||
return type.resolvedBaseTypes = emptyArray; | ||||||
|
@@ -8482,7 +8487,7 @@ namespace ts { | |||||
// partial instantiation of the members without the base types fully resolved | ||||||
type.members = undefined; | ||||||
} | ||||||
return type.resolvedBaseTypes = [baseType]; | ||||||
return type.resolvedBaseTypes = [reducedBaseType]; | ||||||
} | ||||||
|
||||||
function areAllOuterTypeParametersApplied(type: Type): boolean { // TODO: GH#18217 Shouldn't this take an InterfaceType? | ||||||
|
@@ -10457,7 +10462,7 @@ namespace ts { | |||||
else if (type.flags & TypeFlags.Intersection) { | ||||||
if (!((<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersectionComputed)) { | ||||||
(<IntersectionType>type).objectFlags |= ObjectFlags.IsNeverIntersectionComputed | | ||||||
(some(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isDiscriminantWithNeverType) ? ObjectFlags.IsNeverIntersection : 0); | ||||||
(some(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isNeverReducedProperty) ? ObjectFlags.IsNeverIntersection : 0); | ||||||
} | ||||||
return (<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersection ? neverType : type; | ||||||
} | ||||||
|
@@ -10476,12 +10481,39 @@ namespace ts { | |||||
return reduced; | ||||||
} | ||||||
|
||||||
function isNeverReducedProperty(prop: Symbol) { | ||||||
return isDiscriminantWithNeverType(prop) || isConflictingPrivateProperty(prop); | ||||||
} | ||||||
|
||||||
function isDiscriminantWithNeverType(prop: Symbol) { | ||||||
// Return true for a synthetic non-optional property with non-uniform types, where at least one is | ||||||
// a literal type and none is never, that reduces to never. | ||||||
return !(prop.flags & SymbolFlags.Optional) && | ||||||
(getCheckFlags(prop) & (CheckFlags.Discriminant | CheckFlags.HasNeverType)) === CheckFlags.Discriminant && | ||||||
!!(getTypeOfSymbol(prop).flags & TypeFlags.Never); | ||||||
} | ||||||
|
||||||
function isConflictingPrivateProperty(prop: Symbol) { | ||||||
// Return true for a synthetic property with multiple declarations, at least one of which is private. | ||||||
return !prop.valueDeclaration && !!(getCheckFlags(prop) & CheckFlags.ContainsPrivate); | ||||||
} | ||||||
|
||||||
function elaborateNeverIntersection(errorInfo: DiagnosticMessageChain | undefined, type: Type) { | ||||||
if (getObjectFlags(type) & ObjectFlags.IsNeverIntersection) { | ||||||
const neverProp = find(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isDiscriminantWithNeverType); | ||||||
if (neverProp) { | ||||||
return chainDiagnosticMessages(errorInfo, Diagnostics.The_intersection_0_was_reduced_to_never_because_property_1_has_conflicting_types_in_some_constituents, | ||||||
typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.NoTypeReduction), symbolToString(neverProp)); | ||||||
} | ||||||
const privateProp = find(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isConflictingPrivateProperty); | ||||||
if (privateProp) { | ||||||
return chainDiagnosticMessages(errorInfo, Diagnostics.The_intersection_0_was_reduced_to_never_because_property_1_exists_in_multiple_constituents_and_is_private_in_some, | ||||||
typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.NoTypeReduction), symbolToString(privateProp)); | ||||||
} | ||||||
} | ||||||
return errorInfo; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Return the symbol for the property with the given name in the given type. Creates synthetic union properties when | ||||||
* necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from | ||||||
|
@@ -15609,6 +15641,9 @@ namespace ts { | |||||
return result; | ||||||
} | ||||||
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. You can actually just write
Suggested change
You can also move the check itself into a function that tries to elaborate on the current |
||||||
} | ||||||
else { | ||||||
errorInfo = elaborateNeverIntersection(errorInfo, originalTarget); | ||||||
} | ||||||
if (!headMessage && maybeSuppress) { | ||||||
lastSkippedInfo = [source, target]; | ||||||
// Used by, eg, missing property checking to replace the top-level message with a more informative one | ||||||
|
@@ -16490,14 +16525,7 @@ namespace ts { | |||||
const sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp); | ||||||
const targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp); | ||||||
if (sourcePropFlags & ModifierFlags.Private || targetPropFlags & ModifierFlags.Private) { | ||||||
const hasDifferingDeclarations = sourceProp.valueDeclaration !== targetProp.valueDeclaration; | ||||||
if (getCheckFlags(sourceProp) & CheckFlags.ContainsPrivate && hasDifferingDeclarations) { | ||||||
if (reportErrors) { | ||||||
reportError(Diagnostics.Property_0_has_conflicting_declarations_and_is_inaccessible_in_type_1, symbolToString(sourceProp), typeToString(source)); | ||||||
} | ||||||
return Ternary.False; | ||||||
} | ||||||
if (hasDifferingDeclarations) { | ||||||
if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) { | ||||||
if (reportErrors) { | ||||||
if (sourcePropFlags & ModifierFlags.Private && targetPropFlags & ModifierFlags.Private) { | ||||||
reportError(Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp)); | ||||||
|
@@ -23633,12 +23661,6 @@ namespace ts { | |||||
const flags = getDeclarationModifierFlagsFromSymbol(prop); | ||||||
const errorNode = node.kind === SyntaxKind.QualifiedName ? node.right : node.kind === SyntaxKind.ImportType ? node : node.name; | ||||||
|
||||||
if (getCheckFlags(prop) & CheckFlags.ContainsPrivate) { | ||||||
// Synthetic property with private constituent property | ||||||
error(errorNode, Diagnostics.Property_0_has_conflicting_declarations_and_is_inaccessible_in_type_1, symbolToString(prop), typeToString(type)); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (isSuper) { | ||||||
// TS 1.0 spec (April 2014): 4.8.2 | ||||||
// - In a constructor, instance member function, instance member accessor, or | ||||||
|
@@ -24134,7 +24156,7 @@ namespace ts { | |||||
relatedInfo = suggestion.valueDeclaration && createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestedName); | ||||||
} | ||||||
else { | ||||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType)); | ||||||
errorInfo = chainDiagnosticMessages(elaborateNeverIntersection(errorInfo, containingType), Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType)); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
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
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
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
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
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
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
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
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/intersectionWithConflictingPrivates.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,70 @@ | ||
tests/cases/compiler/intersectionWithConflictingPrivates.ts(5,4): error TS2339: Property 'y' does not exist on type 'never'. | ||
The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
tests/cases/compiler/intersectionWithConflictingPrivates.ts(6,1): error TS2322: Type '{}' is not assignable to type 'never'. | ||
The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
|
||
|
||
==== tests/cases/compiler/intersectionWithConflictingPrivates.ts (2 errors) ==== | ||
class A { private x: unknown; y?: string; } | ||
class B { private x: unknown; y?: string; } | ||
|
||
declare let ab: A & B; | ||
ab.y = 'hello'; | ||
~ | ||
!!! error TS2339: Property 'y' does not exist on type 'never'. | ||
!!! error TS2339: The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
ab = {}; | ||
~~ | ||
!!! error TS2322: Type '{}' is not assignable to type 'never'. | ||
!!! error TS2322: The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
|
||
function f1(node: A | B) { | ||
if (node instanceof A || node instanceof A) { | ||
node; // A | ||
} | ||
else { | ||
node; // B | ||
} | ||
node; // A | B | ||
} | ||
|
||
// Repro from #37659 | ||
|
||
abstract class ViewNode { } | ||
abstract class ViewRefNode extends ViewNode { } | ||
abstract class ViewRefFileNode extends ViewRefNode { } | ||
|
||
class CommitFileNode extends ViewRefFileNode { | ||
private _id: any; | ||
} | ||
|
||
class ResultsFileNode extends ViewRefFileNode { | ||
private _id: any; | ||
} | ||
|
||
class StashFileNode extends CommitFileNode { | ||
private _id2: any; | ||
} | ||
|
||
class StatusFileNode extends ViewNode { | ||
private _id: any; | ||
} | ||
|
||
class Foo { | ||
private async foo(node: CommitFileNode | ResultsFileNode | StashFileNode) { | ||
if ( | ||
!(node instanceof CommitFileNode) && | ||
!(node instanceof StashFileNode) && | ||
!(node instanceof ResultsFileNode) | ||
) { | ||
return; | ||
} | ||
|
||
await this.bar(node); | ||
} | ||
|
||
private async bar(node: CommitFileNode | ResultsFileNode | StashFileNode | StatusFileNode, options?: {}) { | ||
return Promise.resolve(undefined); | ||
} | ||
} | ||
|
Oops, something went wrong.
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.
Should we add related spans pointing at the location(s) of the private declarations? I imagine one of the possible "fixes" for this error is just making the fields public.
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.
We could consider it, but not super high value given that this is a pretty esoteric error.