Skip to content

Fix discriminant in loop #10028

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 5 commits into from
Aug 1, 2016
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
49 changes: 28 additions & 21 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7764,16 +7764,17 @@ namespace ts {
}

function isMatchingReference(source: Node, target: Node): boolean {
if (source.kind === target.kind) {
switch (source.kind) {
case SyntaxKind.Identifier:
return getResolvedSymbol(<Identifier>source) === getResolvedSymbol(<Identifier>target);
case SyntaxKind.ThisKeyword:
return true;
case SyntaxKind.PropertyAccessExpression:
return (<PropertyAccessExpression>source).name.text === (<PropertyAccessExpression>target).name.text &&
isMatchingReference((<PropertyAccessExpression>source).expression, (<PropertyAccessExpression>target).expression);
}
switch (source.kind) {
case SyntaxKind.Identifier:
return target.kind === SyntaxKind.Identifier && getResolvedSymbol(<Identifier>source) === getResolvedSymbol(<Identifier>target) ||
(target.kind === SyntaxKind.VariableDeclaration || target.kind === SyntaxKind.BindingElement) &&
getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(<Identifier>source)) === getSymbolOfNode(target);
case SyntaxKind.ThisKeyword:
return target.kind === SyntaxKind.ThisKeyword;
case SyntaxKind.PropertyAccessExpression:
return target.kind === SyntaxKind.PropertyAccessExpression &&
(<PropertyAccessExpression>source).name.text === (<PropertyAccessExpression>target).name.text &&
isMatchingReference((<PropertyAccessExpression>source).expression, (<PropertyAccessExpression>target).expression);
}
return false;
}
Expand All @@ -7788,6 +7789,10 @@ namespace ts {
return false;
}

function rootContainsMatchingReference(source: Node, target: Node) {
return target.kind === SyntaxKind.PropertyAccessExpression && containsMatchingReference(source, (<PropertyAccessExpression>target).expression);
}

function isOrContainsMatchingReference(source: Node, target: Node) {
return isMatchingReference(source, target) || containsMatchingReference(source, target);
}
Expand Down Expand Up @@ -8031,6 +8036,12 @@ namespace ts {
getInitialTypeOfBindingElement(<BindingElement>node);
}

function getInitialOrAssignedType(node: VariableDeclaration | BindingElement | Expression) {
return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement ?
getInitialType(<VariableDeclaration | BindingElement>node) :
getAssignedType(<Expression>node);
}

function getReferenceCandidate(node: Expression): Expression {
switch (node.kind) {
case SyntaxKind.ParenthesizedExpression:
Expand Down Expand Up @@ -8153,19 +8164,9 @@ namespace ts {
const node = flow.node;
// Assignments only narrow the computed type if the declared type is a union type. Thus, we
// only need to evaluate the assigned type if the declared type is a union type.
if ((node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement) &&
reference.kind === SyntaxKind.Identifier &&
getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(<Identifier>reference)) === getSymbolOfNode(node)) {
return declaredType.flags & TypeFlags.Union ?
getAssignmentReducedType(<UnionType>declaredType, getInitialType(<VariableDeclaration | BindingElement>node)) :
declaredType;
}
// If the node is not a variable declaration or binding element, it is an identifier
// or a dotted name that is the target of an assignment. If we have a match, reduce
// the declared type by the assigned type.
if (isMatchingReference(reference, node)) {
return declaredType.flags & TypeFlags.Union ?
getAssignmentReducedType(<UnionType>declaredType, getAssignedType(<Expression>node)) :
getAssignmentReducedType(<UnionType>declaredType, getInitialOrAssignedType(node)) :
declaredType;
}
// We didn't have a direct match. However, if the reference is a dotted name, this
Expand Down Expand Up @@ -8297,6 +8298,9 @@ namespace ts {
if (isMatchingPropertyAccess(expr)) {
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy));
}
if (rootContainsMatchingReference(reference, expr)) {
return declaredType;
}
return type;
}

Expand Down Expand Up @@ -8329,6 +8333,9 @@ namespace ts {
if (isMatchingPropertyAccess(right)) {
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
}
if (rootContainsMatchingReference(reference, left) || rootContainsMatchingReference(reference, right)) {
return declaredType;
}
break;
case SyntaxKind.InstanceOfKeyword:
return narrowTypeByInstanceof(type, expr, assumeTrue);
Expand Down
139 changes: 139 additions & 0 deletions tests/baselines/reference/narrowingByDiscriminantInLoop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//// [narrowingByDiscriminantInLoop.ts]

// Repro from #9977

type IDLMemberTypes = OperationMemberType | ConstantMemberType;

interface IDLTypeDescription {
origin: string;
}

interface InterfaceType {
members: IDLMemberTypes[];
}

interface OperationMemberType {
type: "operation";
idlType: IDLTypeDescription;
}

interface ConstantMemberType {
type: "const";
idlType: string;
}

function insertInterface(callbackType: InterfaceType) {
for (const memberType of callbackType.members) {
if (memberType.type === "const") {
memberType.idlType; // string
}
else if (memberType.type === "operation") {
memberType.idlType.origin; // string
(memberType.idlType as IDLTypeDescription);
}
}
}

function insertInterface2(callbackType: InterfaceType) {
for (const memberType of callbackType.members) {
if (memberType.type === "operation") {
memberType.idlType.origin; // string
}
}
}

function foo(memberType: IDLMemberTypes) {
if (memberType.type === "const") {
memberType.idlType; // string
}
else if (memberType.type === "operation") {
memberType.idlType.origin; // string
}
}

// Repro for issue similar to #8383

interface A {
kind: true;
prop: { a: string; };
}

interface B {
kind: false;
prop: { b: string; }
}

function f1(x: A | B) {
while (true) {
x.prop;
if (x.kind === true) {
x.prop.a;
}
if (x.kind === false) {
x.prop.b;
}
}
}

function f2(x: A | B) {
while (true) {
if (x.kind) {
x.prop.a;
}
if (!x.kind) {
x.prop.b;
}
}
}

//// [narrowingByDiscriminantInLoop.js]
// Repro from #9977
function insertInterface(callbackType) {
for (var _i = 0, _a = callbackType.members; _i < _a.length; _i++) {
var memberType = _a[_i];
if (memberType.type === "const") {
memberType.idlType; // string
}
else if (memberType.type === "operation") {
memberType.idlType.origin; // string
memberType.idlType;
}
}
}
function insertInterface2(callbackType) {
for (var _i = 0, _a = callbackType.members; _i < _a.length; _i++) {
var memberType = _a[_i];
if (memberType.type === "operation") {
memberType.idlType.origin; // string
}
}
}
function foo(memberType) {
if (memberType.type === "const") {
memberType.idlType; // string
}
else if (memberType.type === "operation") {
memberType.idlType.origin; // string
}
}
function f1(x) {
while (true) {
x.prop;
if (x.kind === true) {
x.prop.a;
}
if (x.kind === false) {
x.prop.b;
}
}
}
function f2(x) {
while (true) {
if (x.kind) {
x.prop.a;
}
if (!x.kind) {
x.prop.b;
}
}
}
Loading