Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Introduce the "comparable" relation #5517

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 24 commits into from
Mar 30, 2016
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
002bb6f
Added tests.
DanielRosenwasser Oct 27, 2015
7426aca
Accepted baselines.
DanielRosenwasser Oct 27, 2015
43f158d
Added "comparability" relation. It's currently equivalent to assignab…
DanielRosenwasser Oct 27, 2015
1edb007
Check for partial satisfiability when using the comparable relationship.
DanielRosenwasser Oct 28, 2015
9a5e3e3
Avoid a redundant stricter check when using the new relation.
DanielRosenwasser Oct 28, 2015
42b3ce4
Rename the relationship to "matchable by".
DanielRosenwasser Oct 28, 2015
929808e
Accepted baselines.
DanielRosenwasser Oct 29, 2015
262352e
Added tests on intersection types.
DanielRosenwasser Oct 28, 2015
bdb1db5
Matchable should have no effect on intersections.
DanielRosenwasser Oct 28, 2015
441dd78
Accepted baselines.
DanielRosenwasser Oct 29, 2015
841789d
Renamed the relationship back to "comparable".
DanielRosenwasser Oct 29, 2015
5cb95d4
Renamed test directory.
DanielRosenwasser Oct 29, 2015
ab7c4e5
Accepted baselines.
DanielRosenwasser Oct 29, 2015
e224083
Updated error message.
DanielRosenwasser Nov 4, 2015
fa6e181
Accepted baselines.
DanielRosenwasser Nov 4, 2015
80a50aa
Appease the almighty linter.
DanielRosenwasser Nov 4, 2015
42c49ce
Style.
DanielRosenwasser Nov 4, 2015
6c8c122
'with' to 'to'
DanielRosenwasser Nov 4, 2015
f6eacb9
Accepted baselines.
DanielRosenwasser Nov 4, 2015
a37b731
Changed type assertion error message.
DanielRosenwasser Nov 5, 2015
e0385a4
Accepted baselines.
DanielRosenwasser Nov 5, 2015
0a706c4
Merge branch 'master' into comparableRelation
DanielRosenwasser Nov 5, 2015
a3faca5
Merge branch 'master' into comparableRelation
DanielRosenwasser Mar 30, 2016
3cc64cb
Undo comment override from merge.
DanielRosenwasser Mar 30, 2016
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
Prev Previous commit
Next Next commit
Renamed the relationship back to "comparable".
  • Loading branch information
DanielRosenwasser committed Oct 29, 2015
commit 841789d162e91f6a88e1db411b7bd22ffc6e23f2
28 changes: 14 additions & 14 deletions 28 src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ namespace ts {

let subtypeRelation: Map<RelationComparisonResult> = {};
let assignableRelation: Map<RelationComparisonResult> = {};
let matchableRelation: Map<RelationComparisonResult> = {};
let comparableRelation: Map<RelationComparisonResult> = {};
let identityRelation: Map<RelationComparisonResult> = {};

// This is for caching the result of getSymbolDisplayBuilder. Do not access directly.
Expand Down Expand Up @@ -4743,8 +4743,8 @@ namespace ts {
* This is *not* a bi-directional relationship.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true of isTypeAssignableTo too? Its former usage in this diff indicates 'yes'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true of isTypeAssignableTo but isTypeAssignableTo has a clear direction; on the other hand, isTypeComparableTo is vague enough that it warrants a comment. One would think that A being comparable to B implies that B is comarable to A, but that's not quite the case.

I could have made isTypeComparableTo a bidirectional relation, but I decided not to based on the way in which we use the relation in type assertions. Specifically, with type assertions, we check whether the expression type is comparable to the asserted type and that the asserted type is comparable to the widened type of the expression.

Actually, I'm not sure if there is any reason we don't check against the widened type for both. I'll experiment with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, I think that's a bug. I will change that in a later PR

* If one needs to check both directions for comparability, use a second call to this function or 'checkTypeComparableTo'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All uses do need to check both directions, so probably you should have a version that does that. You might not even need the uni-directional ones except for consistency with the other relations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
function isTypeMatchableBy(source: Type, target: Type): boolean {
return checkTypeMatchableBy(source, target, /*errorNode*/ undefined);
function isTypeComparableTo(source: Type, target: Type): boolean {
return checkTypeComparableTo(source, target, /*errorNode*/ undefined);
}

function checkTypeSubtypeOf(source: Type, target: Type, errorNode: Node, headMessage?: DiagnosticMessage, containingMessageChain?: DiagnosticMessageChain): boolean {
Expand All @@ -4757,10 +4757,10 @@ namespace ts {

/**
* This is *not* a bi-directional relationship.
* If one needs to check both directions for comparability, use a second call to this function or 'isTypeMatchableBy'.
* If one needs to check both directions for comparability, use a second call to this function or 'isTypeComparableTo'.
*/
function checkTypeMatchableBy(source: Type, target: Type, errorNode: Node, headMessage?: DiagnosticMessage, containingMessageChain?: DiagnosticMessageChain): boolean {
return checkTypeRelatedTo(source, target, matchableRelation, errorNode, headMessage, containingMessageChain);
function checkTypeComparableTo(source: Type, target: Type, errorNode: Node, headMessage?: DiagnosticMessage, containingMessageChain?: DiagnosticMessageChain): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This function is hard to read on a portrait (1200 px) monitor with github's generous margins cutting it down to about 700 px. Might not be worth fixing.

return checkTypeRelatedTo(source, target, comparableRelation, errorNode, headMessage, containingMessageChain);
}

function isSignatureAssignableTo(source: Signature, target: Signature): boolean {
Expand All @@ -4773,7 +4773,7 @@ namespace ts {
* Checks if 'source' is related to 'target' (e.g.: is a assignable to).
* @param source The left-hand-side of the relation.
* @param target The right-hand-side of the relation.
* @param relation The relation considered. One of 'identityRelation', 'assignableRelation', 'subTypeRelation', or 'matchableRelation'.
* @param relation The relation considered. One of 'identityRelation', 'assignableRelation', 'subTypeRelation', or 'comparableRelation'.
* Used as both to determine which checks are performed and as a cache of previously computed results.
* @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used.
* @param headMessage If the error chain should be prepended by a head message, then headMessage will be used.
Expand All @@ -4798,7 +4798,7 @@ namespace ts {

Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");

const isAssignableOrComparableRelation = relation === assignableRelation || relation === matchableRelation;
const isAssignableOrComparableRelation = relation === assignableRelation || relation === comparableRelation;
let result = isRelatedTo(source, target, errorNode !== undefined, headMessage);
if (overflow) {
error(errorNode, Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target));
Expand Down Expand Up @@ -4877,7 +4877,7 @@ namespace ts {

// Note that the "each" checks must precede the "some" checks to produce the correct results
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment needs to reflect the more complex checks.

if (source.flags & TypeFlags.Union) {
if (relation === matchableRelation) {
if (relation === comparableRelation) {
result = someTypeRelatedToType(source as UnionType, target, reportErrors);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please choose one cast syntax and make at least the changed lines use it, preferably something like all the casts in the function.

}
else {
Expand Down Expand Up @@ -9410,8 +9410,8 @@ namespace ts {
if (produceDiagnostics && targetType !== unknownType) {
let widenedType = getWidenedType(exprType);

if (!isTypeMatchableBy(targetType, widenedType)) {
checkTypeMatchableBy(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other);
if (!isTypeComparableTo(targetType, widenedType)) {
checkTypeComparableTo(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other);
}
}
return targetType;
Expand Down Expand Up @@ -10254,7 +10254,7 @@ namespace ts {
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
if (!isTypeMatchableBy(leftType, rightType) && !isTypeMatchableBy(rightType, leftType)) {
if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) {
reportOperatorError();
}
return booleanType;
Expand Down Expand Up @@ -12722,8 +12722,8 @@ namespace ts {
// In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update 'assignable' to 'comparable'

let caseType = checkExpression(caseClause.expression);

if (!isTypeMatchableBy(expressionType, caseType)) {
checkTypeMatchableBy(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
if (!isTypeComparableTo(expressionType, caseType)) {
checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
}
}
forEach(clause.statements, checkSourceElement);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.