Skip to content

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

Conversation

weswigham
Copy link
Member

Fixes #32928

… facts when an equivalent positive fact is present
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

The intent seems good but I think I missed how the code works entirely.

@weswigham weswigham requested a review from sandersn June 12, 2020 18:47
@weswigham weswigham force-pushed the control-flow-function-typeof branch from b95ac8a to 269f5ba Compare June 12, 2020 18:54
// 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)
const positiveFacts = result & (TypeFacts.NegativeTypeofFacts >> 8);
Copy link
Member

Choose a reason for hiding this comment

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

It seems like the test coverage here is not adequate to ensure that this math continues to work for all cases in the future. Let's have a testcase that makes sure that e.g. someone reordering the EQ members without also pairwise reordering the NE members sees a baseline diff.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not quite sure how to make a baseline for that, but a unittest can be made if the TypeFacts gets export + @internal:

describe("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
        let ExampleFacts = TypeFacts.TypeofEQNumber | TypeFacts.TypeofNENumber | TypeFacts.TypeofNEBigInt;
        // Using this line of code (with the 8 which effectively represents the number of EQ types)
        const positiveFacts = ExampleFacts & (TypeFacts.NegativeTypeofFacts >> 8);
        // Then this should drop the negative version of the same type
        ExampleFacts &= ~(positiveFacts << 8);
        assert.isTrue((ExampleFacts & TypeFacts.TypeofNENumber) === 0);

        // And it keeps the rest
        assert.isTrue((ExampleFacts & TypeFacts.TypeofEQNumber) !== 0);
        assert.isTrue((ExampleFacts & TypeFacts.TypeofNEBigInt) !== 0);

        // If this test has failed, you _probably_ need to adjust the 8 here, and in
        // getTypeFactsOfTypes.
    });
});```

@sandersn sandersn assigned orta and unassigned weswigham Sep 4, 2020
@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Sep 4, 2020
@sandersn sandersn assigned sandersn and unassigned orta Sep 4, 2020
@orta
Copy link
Contributor

orta commented Sep 18, 2020

There we go, added that test

@weswigham
Copy link
Member Author

weswigham commented Sep 18, 2020

@RyanCavanaugh (and @sandersn ) 👍 👎 when you get a chance?

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Couple of style suggestions.

@@ -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,
Copy link
Member

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

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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

@sandersn sandersn assigned RyanCavanaugh and unassigned sandersn Mar 11, 2021
@sandersn
Copy link
Member

Unfortunately, we never finished reviewing this PR. It is pretty old now, so I'm going to close it to reduce the number of open PRs.

@sandersn sandersn closed this May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

union types not type guarded
5 participants