-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Adjust TypeFact calculation for intersections to omit negative typeof facts when an equivalent positive fact is present #39016
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
20b6302
Adjust TypeFact calculation for intersections to omit negative typeof…
weswigham 109a3f0
Merge branch 'master' into control-flow-function-typeof
weswigham 269f5ba
PR feedback
weswigham 3de676e
Adjust TypeFact calculation for intersections to omit negative typeof…
weswigham 161dda3
PR feedback
weswigham 5369921
Merge branch 'control-flow-function-typeof' of https://github.com/wes…
orta 5425753
Adds a unit test for the bitmasking logic
orta 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 hidden or 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 |
---|---|---|
|
@@ -60,7 +60,8 @@ namespace ts { | |
GeneratorYield, | ||
} | ||
|
||
const enum TypeFacts { | ||
/** @internal */ | ||
export const enum TypeFacts { | ||
None = 0, | ||
TypeofEQString = 1 << 0, // typeof x === "string" | ||
TypeofEQNumber = 1 << 1, // typeof x === "number" | ||
|
@@ -73,7 +74,7 @@ namespace ts { | |
TypeofNEString = 1 << 8, // typeof x !== "string" | ||
TypeofNENumber = 1 << 9, // typeof x !== "number" | ||
TypeofNEBigInt = 1 << 10, // typeof x !== "bigint" | ||
TypeofNEBoolean = 1 << 11, // typeof x !== "boolean" | ||
TypeofNEBoolean = 1 << 11, // typeof x !== "boolean" | ||
TypeofNESymbol = 1 << 12, // typeof x !== "symbol" | ||
TypeofNEObject = 1 << 13, // typeof x !== "object" | ||
TypeofNEFunction = 1 << 14, // typeof x !== "function" | ||
|
@@ -90,6 +91,7 @@ namespace ts { | |
// The following members encode facts about particular kinds of types for use in the getTypeFacts function. | ||
// The presence of a particular fact means that the given test is true for some (and possibly all) values | ||
// of that kind of type. | ||
NegativeTypeofFacts = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject, | ||
BaseStringStrictFacts = TypeofEQString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | NEUndefined | NENull | NEUndefinedOrNull, | ||
BaseStringFacts = BaseStringStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy, | ||
StringStrictFacts = BaseStringStrictFacts | Truthy | Falsy, | ||
|
@@ -20287,11 +20289,18 @@ namespace ts { | |
return declaredType; | ||
} | ||
|
||
function getTypeFactsOfTypes(types: Type[]): TypeFacts { | ||
function getTypeFactsOfTypes(types: Type[], isUnion: boolean): TypeFacts { | ||
let result: TypeFacts = TypeFacts.None; | ||
for (const t of types) { | ||
result |= getTypeFacts(t); | ||
} | ||
if (!isUnion) { | ||
// Get the set of positive facts for the intersection by masking with the negative set shifted left, | ||
// then shift those present positive facts into the negative fact value range, and unset any of those | ||
// bits (by negating that mask and then intersecting it with the original value) | ||
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. I don't like "intersecting" here, but "ANDing" sounds silly too. I think there's a technical term, but it might be too technical because I can't remember it. Consider my vague dislike registered and continue as appropriate. |
||
const positiveFacts = result & (TypeFacts.NegativeTypeofFacts >> 8); | ||
result &= ~(positiveFacts << 8); | ||
} | ||
return result; | ||
} | ||
|
||
|
@@ -20366,7 +20375,7 @@ namespace ts { | |
return getTypeFacts(getBaseConstraintOfType(type) || unknownType); | ||
} | ||
if (flags & TypeFlags.UnionOrIntersection) { | ||
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types); | ||
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types, !!(flags & TypeFlags.Union)); | ||
} | ||
return TypeFacts.All; | ||
} | ||
|
This file contains hidden or 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 hidden or 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,40 @@ | ||
namespace ts { | ||
describe("unittests:: tsc:: getTypeFactsOfTypes::", () => { | ||
// See: https://github.com/microsoft/TypeScript/pull/39016 | ||
it("correctly can strip NegativeTypeofFacts when there are PositiveTypeofFacts of the same type via bitmask logic", () => { | ||
const TypeFacts = (ts as any).TypeFacts; | ||
|
||
// For a set of facts, which include both the positive and negative of each other | ||
const positiveFacts = [ | ||
TypeFacts.TypeofEQString, | ||
TypeFacts.TypeofEQNumber, | ||
TypeFacts.TypeofEQBigInt, | ||
TypeFacts.TypeofEQBoolean, | ||
TypeFacts.TypeofEQSymbol, | ||
TypeFacts.TypeofEQObject, | ||
TypeFacts.TypeofEQFunction, | ||
TypeFacts.TypeofEQHostObject, | ||
]; | ||
|
||
const negativeFacts = [ | ||
TypeFacts.TypeofNEString, | ||
TypeFacts.TypeofNENumber, | ||
TypeFacts.TypeofNEBigInt, | ||
TypeFacts.TypeofNEBoolean, | ||
TypeFacts.TypeofNESymbol, | ||
TypeFacts.TypeofNEObject, | ||
TypeFacts.TypeofNEFunction, | ||
TypeFacts.TypeofNEHostObject, | ||
]; | ||
|
||
// Using this line of code (with the 8 which effectively represents the number of EQ types) | ||
positiveFacts.forEach((pf, i) => { | ||
const nf = pf << 8; | ||
assert.equal(nf, negativeFacts[i]); | ||
}); | ||
|
||
// If this test has failed, you _probably_ need to adjust the 8 here, and in the | ||
// getTypeFactsOfTypes function in checker.ts. | ||
}); | ||
}); | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/baselines/reference/controlFlowTypeofFunctionElseNarrowing.js
This file contains hidden or 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,26 @@ | ||
//// [controlFlowTypeofFunctionElseNarrowing.ts] | ||
// regression for https://github.com/microsoft/TypeScript/issues/32928 | ||
// Callable | ||
type F = (...args: any[]) => any; | ||
// Callable but intersected | ||
type F2 = F & { inject?: string[] } | ||
|
||
declare const a: string | F2 | ||
|
||
if (typeof a == 'function') { | ||
// only F2 | ||
a | ||
} else { | ||
// Should be only a string | ||
a | ||
} | ||
|
||
//// [controlFlowTypeofFunctionElseNarrowing.js] | ||
if (typeof a == 'function') { | ||
// only F2 | ||
a; | ||
} | ||
else { | ||
// Should be only a string | ||
a; | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/controlFlowTypeofFunctionElseNarrowing.symbols
This file contains hidden or 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,29 @@ | ||
=== tests/cases/conformance/controlFlow/controlFlowTypeofFunctionElseNarrowing.ts === | ||
// regression for https://github.com/microsoft/TypeScript/issues/32928 | ||
// Callable | ||
type F = (...args: any[]) => any; | ||
>F : Symbol(F, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 0, 0)) | ||
>args : Symbol(args, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 2, 10)) | ||
|
||
// Callable but intersected | ||
type F2 = F & { inject?: string[] } | ||
>F2 : Symbol(F2, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 2, 33)) | ||
>F : Symbol(F, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 0, 0)) | ||
>inject : Symbol(inject, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 4, 15)) | ||
|
||
declare const a: string | F2 | ||
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13)) | ||
>F2 : Symbol(F2, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 2, 33)) | ||
|
||
if (typeof a == 'function') { | ||
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13)) | ||
|
||
// only F2 | ||
a | ||
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13)) | ||
|
||
} else { | ||
// Should be only a string | ||
a | ||
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13)) | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/controlFlowTypeofFunctionElseNarrowing.types
This file contains hidden or 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,30 @@ | ||
=== tests/cases/conformance/controlFlow/controlFlowTypeofFunctionElseNarrowing.ts === | ||
// regression for https://github.com/microsoft/TypeScript/issues/32928 | ||
// Callable | ||
type F = (...args: any[]) => any; | ||
>F : F | ||
>args : any[] | ||
|
||
// Callable but intersected | ||
type F2 = F & { inject?: string[] } | ||
>F2 : F2 | ||
>inject : string[] | ||
|
||
declare const a: string | F2 | ||
>a : string | F2 | ||
|
||
if (typeof a == 'function') { | ||
>typeof a == 'function' : boolean | ||
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>a : string | F2 | ||
>'function' : "function" | ||
|
||
// only F2 | ||
a | ||
>a : F2 | ||
|
||
} else { | ||
// Should be only a string | ||
a | ||
>a : string | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/cases/conformance/controlFlow/controlFlowTypeofFunctionElseNarrowing.ts
This file contains hidden or 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,15 @@ | ||
// regression for https://github.com/microsoft/TypeScript/issues/32928 | ||
// Callable | ||
type F = (...args: any[]) => any; | ||
// Callable but intersected | ||
type F2 = F & { inject?: string[] } | ||
|
||
declare const a: string | F2 | ||
|
||
if (typeof a == 'function') { | ||
// only F2 | ||
a | ||
} else { | ||
// Should be only a string | ||
a | ||
} |
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.
NegativeTypeofFacts is only ever used to left-shift 8 bits. It would be easier to read if it were just PositiveTypeofFacts, which would need shifting