Skip to content

Stricter criteria for eliminating types in unions during inference - #32919

Merged
Anders Hejlsberg (ahejlsberg) merged 8 commits into
masterfrom
fix32752
Aug 16, 2019
Merged

Stricter criteria for eliminating types in unions during inference#32919
Anders Hejlsberg (ahejlsberg) merged 8 commits into
masterfrom
fix32752

Conversation

@ahejlsberg

Copy link
Copy Markdown
Member

Fixes#32752.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

This PR slightly tweaks the conditions under which we eliminate types in unions during type inference. Specifically, it modifies the last bullet relating to union types in #32558 to be:

  • Finally, if there were source types from which no inferences or only lower priority inferences were made in the previous step, infer from a union of those source types to each naked type parameter in the target set. Give lower priority to these inferences if the target set contains more than one naked type parameter.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Aug 15, 2019

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the parallelized Definitely Typed test suite on this PR at 6d46850. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

DT run is clean, I think this one is ready to merge.

@RyanCavanaugh

Copy link
Copy Markdown
Member

Anders Hejlsberg (@ahejlsberg) did you specifically want this one in 3.6.1?

Comment threadsrc/compiler/checker.ts Outdated
if (symbol) {
if (contains(symbolStack, symbol)) {
inferenceIncomplete = true;
inferencePriority = -1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't tell if -1 is being used here because it's lower than anything else, has all bits set, or both. A named enum member with the intended semantics of this would be clearer.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

It's because -1 is less than any normal priority value. I'll give it a symbolic name.

Comment threadsrc/compiler/checker.ts Outdated
if (inferenceMatch) matched[i] = true;
inferenceMatch = inferenceMatch || saveInferenceMatch;
if (inferencePriority === priority) matched[i] = true;
inferenceCircularity = inferenceCircularity || inferencePriority < 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it possible for inferencePriority to be a negative value that isn't -1 ?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

No

Comment threadsrc/compiler/checker.ts Outdated
const saveInferenceIncomplete = inferenceIncomplete;
inferenceMatch = false;
inferenceIncomplete = false;
(visited || (visited = createMap<number>())).set(key, -1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the -1 here (used as number) the same -1 lower in the file?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Yes

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

did you specifically want this one in 3.6.1?

I think it is a very safe fix, so yes if possible.

@RyanCavanaugh

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) cherry-pick this into release-3.6

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Ryan Cavanaugh (@RyanCavanaugh), I've opened #32933 for you.

TypeScript Bot (typescript-bot) pushed a commit to typescript-bot/TypeScript that referenced this pull request Aug 16, 2019
Component commits:
a81ce06 Stricter criteria for eliminating types in unions during inference
f929a25 Add regression test
6d46850 Accept new baselines
86d9153 Accept new API baselines
abc61a0 Add InferencePriority.Circularity per CR feedback
ac2f151 Accept new API baselines
c816cf2 Add additional test
af7ccf9 Accept new baselines
Ryan Cavanaugh (RyanCavanaugh) pushed a commit that referenced this pull request Aug 16, 2019
Component commits:
a81ce06 Stricter criteria for eliminating types in unions during inference
f929a25 Add regression test
6d46850 Accept new baselines
86d9153 Accept new API baselines
abc61a0 Add InferencePriority.Circularity per CR feedback
ac2f151 Accept new API baselines
c816cf2 Add additional test
af7ccf9 Accept new baselines
@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression with await type extraction in strict mode

3 participants

@ahejlsberg@typescript-bot@RyanCavanaugh