Skip to content

Commit b48c2c5

Browse files
ahejlsbergKingwl
authored andcommitted
Remove CFA discriminant check restrictions (microsoft#36114)
* Remove unnecessary containsMatchingReferenceDiscriminant logic * Accept new baselines
1 parent 80eeb70 commit b48c2c5

File tree

3 files changed

+4
-227
lines changed

3 files changed

+4
-227
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18450,32 +18450,6 @@ namespace ts {
1845018450
return false;
1845118451
}
1845218452

18453-
// Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared
18454-
// type of xxx is a union type, and yyy is a property that is possibly a discriminant. We consider a property
18455-
// a possible discriminant if its type differs in the constituents of containing union type, and if every
18456-
// choice is a unit type or a union of unit types.
18457-
function containsMatchingReferenceDiscriminant(source: Node, target: Node) {
18458-
let name;
18459-
return isAccessExpression(target) &&
18460-
containsMatchingReference(source, target.expression) &&
18461-
(name = getAccessedPropertyName(target)) !== undefined &&
18462-
isDiscriminantProperty(getDeclaredTypeOfReference(target.expression), name);
18463-
}
18464-
18465-
function getDeclaredTypeOfReference(expr: Node): Type | undefined {
18466-
if (expr.kind === SyntaxKind.Identifier) {
18467-
return getTypeOfSymbol(getResolvedSymbol(<Identifier>expr));
18468-
}
18469-
if (isAccessExpression(expr)) {
18470-
const type = getDeclaredTypeOfReference(expr.expression);
18471-
if (type) {
18472-
const propName = getAccessedPropertyName(expr);
18473-
return propName !== undefined ? getTypeOfPropertyOfType(type, propName) : undefined;
18474-
}
18475-
}
18476-
return undefined;
18477-
}
18478-
1847918453
function isDiscriminantProperty(type: Type | undefined, name: __String) {
1848018454
if (type && type.flags & TypeFlags.Union) {
1848118455
const prop = getUnionOrIntersectionProperty(<UnionType>type, name);
@@ -19507,9 +19481,6 @@ namespace ts {
1950719481
type = narrowTypeByDiscriminant(type, expr as AccessExpression,
1950819482
t => narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd));
1950919483
}
19510-
else if (containsMatchingReferenceDiscriminant(reference, expr)) {
19511-
type = declaredType;
19512-
}
1951319484
}
1951419485
return createFlowType(type, isIncomplete(flowType));
1951519486
}
@@ -19694,9 +19665,6 @@ namespace ts {
1969419665
if (isMatchingReferenceDiscriminant(expr, declaredType)) {
1969519666
return narrowTypeByDiscriminant(type, <AccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy));
1969619667
}
19697-
if (containsMatchingReferenceDiscriminant(reference, expr)) {
19698-
return declaredType;
19699-
}
1970019668
return type;
1970119669
}
1970219670

@@ -19756,9 +19724,6 @@ namespace ts {
1975619724
if (isMatchingReferenceDiscriminant(right, declaredType)) {
1975719725
return narrowTypeByDiscriminant(type, <AccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
1975819726
}
19759-
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
19760-
return declaredType;
19761-
}
1976219727
break;
1976319728
case SyntaxKind.InstanceOfKeyword:
1976419729
return narrowTypeByInstanceof(type, expr, assumeTrue);
@@ -20170,9 +20135,6 @@ namespace ts {
2017020135
if (isMatchingReferenceDiscriminant(expr, declaredType)) {
2017120136
return narrowTypeByDiscriminant(type, <AccessExpression>expr, t => getTypeWithFacts(t, assumePresent ? TypeFacts.NEUndefinedOrNull : TypeFacts.EQUndefinedOrNull));
2017220137
}
20173-
if (containsMatchingReferenceDiscriminant(reference, expr)) {
20174-
return declaredType;
20175-
}
2017620138
return type;
2017720139
}
2017820140
}

tests/baselines/reference/discriminantPropertyCheck.errors.txt

Lines changed: 0 additions & 185 deletions
This file was deleted.

tests/baselines/reference/discriminantPropertyCheck.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ function goo2(x: Item) {
8282

8383
x.foo.length; // Error, intervening discriminant guard
8484
>x.foo.length : number
85-
>x.foo : string | undefined
85+
>x.foo : string
8686
>x : Item1
87-
>foo : string | undefined
87+
>foo : string
8888
>length : number
8989
}
9090
}
@@ -226,9 +226,9 @@ function foo6(x: Item) {
226226

227227
x.foo.length; // Error, intervening discriminant guard
228228
>x.foo.length : number
229-
>x.foo : string | undefined
229+
>x.foo : string
230230
>x : Item1
231-
>foo : string | undefined
231+
>foo : string
232232
>length : number
233233
}
234234
}

0 commit comments

Comments
 (0)