Skip to content

Remove CFA discriminant check restrictions #36114

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 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 0 additions & 38 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18452,32 +18452,6 @@ namespace ts {
return false;
}

// Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared
// type of xxx is a union type, and yyy is a property that is possibly a discriminant. We consider a property
// a possible discriminant if its type differs in the constituents of containing union type, and if every
// choice is a unit type or a union of unit types.
function containsMatchingReferenceDiscriminant(source: Node, target: Node) {
let name;
return isAccessExpression(target) &&
containsMatchingReference(source, target.expression) &&
(name = getAccessedPropertyName(target)) !== undefined &&
isDiscriminantProperty(getDeclaredTypeOfReference(target.expression), name);
}

function getDeclaredTypeOfReference(expr: Node): Type | undefined {
if (expr.kind === SyntaxKind.Identifier) {
return getTypeOfSymbol(getResolvedSymbol(<Identifier>expr));
}
if (isAccessExpression(expr)) {
const type = getDeclaredTypeOfReference(expr.expression);
if (type) {
const propName = getAccessedPropertyName(expr);
return propName !== undefined ? getTypeOfPropertyOfType(type, propName) : undefined;
}
}
return undefined;
}

function isDiscriminantProperty(type: Type | undefined, name: __String) {
if (type && type.flags & TypeFlags.Union) {
const prop = getUnionOrIntersectionProperty(<UnionType>type, name);
Expand Down Expand Up @@ -19509,9 +19483,6 @@ namespace ts {
type = narrowTypeByDiscriminant(type, expr as AccessExpression,
t => narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd));
}
else if (containsMatchingReferenceDiscriminant(reference, expr)) {
type = declaredType;
}
}
return createFlowType(type, isIncomplete(flowType));
}
Expand Down Expand Up @@ -19696,9 +19667,6 @@ namespace ts {
if (isMatchingReferenceDiscriminant(expr, declaredType)) {
return narrowTypeByDiscriminant(type, <AccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy));
}
if (containsMatchingReferenceDiscriminant(reference, expr)) {
return declaredType;
}
return type;
}

Expand Down Expand Up @@ -19758,9 +19726,6 @@ namespace ts {
if (isMatchingReferenceDiscriminant(right, declaredType)) {
return narrowTypeByDiscriminant(type, <AccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
}
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
return declaredType;
}
break;
case SyntaxKind.InstanceOfKeyword:
return narrowTypeByInstanceof(type, expr, assumeTrue);
Expand Down Expand Up @@ -20172,9 +20137,6 @@ namespace ts {
if (isMatchingReferenceDiscriminant(expr, declaredType)) {
return narrowTypeByDiscriminant(type, <AccessExpression>expr, t => getTypeWithFacts(t, assumePresent ? TypeFacts.NEUndefinedOrNull : TypeFacts.EQUndefinedOrNull));
}
if (containsMatchingReferenceDiscriminant(reference, expr)) {
return declaredType;
}
return type;
}
}
Expand Down
185 changes: 0 additions & 185 deletions tests/baselines/reference/discriminantPropertyCheck.errors.txt

This file was deleted.

8 changes: 4 additions & 4 deletions tests/baselines/reference/discriminantPropertyCheck.types
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ function goo2(x: Item) {

x.foo.length; // Error, intervening discriminant guard
>x.foo.length : number
>x.foo : string | undefined
>x.foo : string
>x : Item1
>foo : string | undefined
>foo : string
>length : number
}
}
Expand Down Expand Up @@ -226,9 +226,9 @@ function foo6(x: Item) {

x.foo.length; // Error, intervening discriminant guard
>x.foo.length : number
>x.foo : string | undefined
>x.foo : string
>x : Item1
>foo : string | undefined
>foo : string
>length : number
}
}
Expand Down