-
Notifications
You must be signed in to change notification settings - Fork 717
Fix translation error that disabled union discrimination optimization #1757
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
43 changes: 43 additions & 0 deletions
43
testdata/baselines/reference/compiler/missingDiscriminants2.errors.txt
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,43 @@ | ||
missingDiscriminants2.ts(32,23): error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'. | ||
missingDiscriminants2.ts(33,34): error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'. | ||
|
||
|
||
==== missingDiscriminants2.ts (2 errors) ==== | ||
// https://github.com/microsoft/typescript-go/issues/1020 | ||
|
||
// This tests ensures the change also works for discriminated unions with more than 10 cases | ||
|
||
type Thing = | ||
| { str: "a", num: 0 } | ||
| { str: "b" } | ||
| { str: "c" } | ||
| { str: "d" } | ||
| { str: "e" } | ||
| { str: "f" } | ||
| { str: "g" } | ||
| { str: "h" } | ||
| { str: "i" } | ||
| { str: "j" } | ||
| { str: "k" } | ||
| { str: "l" } | ||
| { str: "m" } | ||
| { str: "n" } | ||
| { str: "o" } | ||
| { num: 1 } | ||
|
||
const thing1: Thing = { str: "a", num: 0 } | ||
const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error | ||
const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error | ||
|
||
type Item = | ||
| { kind: "a", subkind: 0, value: string } | ||
| { kind: "a", subkind: 1, value: number } | ||
| { kind: "b" } | ||
|
||
const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a" | ||
~~~~~~~ | ||
!!! error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'. | ||
const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property | ||
~~~~~~~ | ||
!!! error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'. | ||
|
105 changes: 105 additions & 0 deletions
105
testdata/baselines/reference/compiler/missingDiscriminants2.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,105 @@ | ||
//// [tests/cases/compiler/missingDiscriminants2.ts] //// | ||
|
||
=== missingDiscriminants2.ts === | ||
// https://github.com/microsoft/typescript-go/issues/1020 | ||
|
||
// This tests ensures the change also works for discriminated unions with more than 10 cases | ||
|
||
type Thing = | ||
>Thing : Symbol(Thing, Decl(missingDiscriminants2.ts, 0, 0)) | ||
|
||
| { str: "a", num: 0 } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 5, 5)) | ||
>num : Symbol(num, Decl(missingDiscriminants2.ts, 5, 15)) | ||
|
||
| { str: "b" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 6, 5)) | ||
|
||
| { str: "c" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 7, 5)) | ||
|
||
| { str: "d" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 8, 5)) | ||
|
||
| { str: "e" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 9, 5)) | ||
|
||
| { str: "f" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 10, 5)) | ||
|
||
| { str: "g" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 11, 5)) | ||
|
||
| { str: "h" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 12, 5)) | ||
|
||
| { str: "i" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 13, 5)) | ||
|
||
| { str: "j" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 14, 5)) | ||
|
||
| { str: "k" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 15, 5)) | ||
|
||
| { str: "l" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 16, 5)) | ||
|
||
| { str: "m" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 17, 5)) | ||
|
||
| { str: "n" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 18, 5)) | ||
|
||
| { str: "o" } | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 19, 5)) | ||
|
||
| { num: 1 } | ||
>num : Symbol(num, Decl(missingDiscriminants2.ts, 20, 5)) | ||
|
||
const thing1: Thing = { str: "a", num: 0 } | ||
>thing1 : Symbol(thing1, Decl(missingDiscriminants2.ts, 22, 5)) | ||
>Thing : Symbol(Thing, Decl(missingDiscriminants2.ts, 0, 0)) | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 22, 23)) | ||
>num : Symbol(num, Decl(missingDiscriminants2.ts, 22, 33)) | ||
|
||
const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error | ||
>thing2 : Symbol(thing2, Decl(missingDiscriminants2.ts, 23, 5)) | ||
>Thing : Symbol(Thing, Decl(missingDiscriminants2.ts, 0, 0)) | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 23, 23)) | ||
>num : Symbol(num, Decl(missingDiscriminants2.ts, 23, 33)) | ||
|
||
const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error | ||
>thing3 : Symbol(thing3, Decl(missingDiscriminants2.ts, 24, 5)) | ||
>Thing : Symbol(Thing, Decl(missingDiscriminants2.ts, 0, 0)) | ||
>num : Symbol(num, Decl(missingDiscriminants2.ts, 24, 23)) | ||
>str : Symbol(str, Decl(missingDiscriminants2.ts, 24, 31)) | ||
|
||
type Item = | ||
>Item : Symbol(Item, Decl(missingDiscriminants2.ts, 24, 42)) | ||
|
||
| { kind: "a", subkind: 0, value: string } | ||
>kind : Symbol(kind, Decl(missingDiscriminants2.ts, 27, 5)) | ||
>subkind : Symbol(subkind, Decl(missingDiscriminants2.ts, 27, 16)) | ||
>value : Symbol(value, Decl(missingDiscriminants2.ts, 27, 28)) | ||
|
||
| { kind: "a", subkind: 1, value: number } | ||
>kind : Symbol(kind, Decl(missingDiscriminants2.ts, 28, 5)) | ||
>subkind : Symbol(subkind, Decl(missingDiscriminants2.ts, 28, 16)) | ||
>value : Symbol(value, Decl(missingDiscriminants2.ts, 28, 28)) | ||
|
||
| { kind: "b" } | ||
>kind : Symbol(kind, Decl(missingDiscriminants2.ts, 29, 5)) | ||
|
||
const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a" | ||
>item1 : Symbol(item1, Decl(missingDiscriminants2.ts, 31, 5)) | ||
>Item : Symbol(Item, Decl(missingDiscriminants2.ts, 24, 42)) | ||
>subkind : Symbol(subkind, Decl(missingDiscriminants2.ts, 31, 21)) | ||
>kind : Symbol(kind, Decl(missingDiscriminants2.ts, 31, 33)) | ||
|
||
const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property | ||
>item2 : Symbol(item2, Decl(missingDiscriminants2.ts, 32, 5)) | ||
>Item : Symbol(Item, Decl(missingDiscriminants2.ts, 24, 42)) | ||
>kind : Symbol(kind, Decl(missingDiscriminants2.ts, 32, 21)) | ||
>subkind : Symbol(subkind, Decl(missingDiscriminants2.ts, 32, 32)) | ||
|
115 changes: 115 additions & 0 deletions
115
testdata/baselines/reference/compiler/missingDiscriminants2.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,115 @@ | ||
//// [tests/cases/compiler/missingDiscriminants2.ts] //// | ||
|
||
=== missingDiscriminants2.ts === | ||
// https://github.com/microsoft/typescript-go/issues/1020 | ||
|
||
// This tests ensures the change also works for discriminated unions with more than 10 cases | ||
|
||
type Thing = | ||
>Thing : Thing | ||
|
||
| { str: "a", num: 0 } | ||
>str : "a" | ||
>num : 0 | ||
|
||
| { str: "b" } | ||
>str : "b" | ||
|
||
| { str: "c" } | ||
>str : "c" | ||
|
||
| { str: "d" } | ||
>str : "d" | ||
|
||
| { str: "e" } | ||
>str : "e" | ||
|
||
| { str: "f" } | ||
>str : "f" | ||
|
||
| { str: "g" } | ||
>str : "g" | ||
|
||
| { str: "h" } | ||
>str : "h" | ||
|
||
| { str: "i" } | ||
>str : "i" | ||
|
||
| { str: "j" } | ||
>str : "j" | ||
|
||
| { str: "k" } | ||
>str : "k" | ||
|
||
| { str: "l" } | ||
>str : "l" | ||
|
||
| { str: "m" } | ||
>str : "m" | ||
|
||
| { str: "n" } | ||
>str : "n" | ||
|
||
| { str: "o" } | ||
>str : "o" | ||
|
||
| { num: 1 } | ||
>num : 1 | ||
|
||
const thing1: Thing = { str: "a", num: 0 } | ||
>thing1 : Thing | ||
>{ str: "a", num: 0 } : { str: "a"; num: 0; } | ||
>str : "a" | ||
>"a" : "a" | ||
>num : 0 | ||
>0 : 0 | ||
|
||
const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error | ||
>thing2 : Thing | ||
>{ str: "b", num: 1 } : { str: "b"; num: 1; } | ||
>str : "b" | ||
>"b" : "b" | ||
>num : 1 | ||
>1 : 1 | ||
|
||
const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error | ||
>thing3 : Thing | ||
>{ num: 1, str: "b" } : { num: 1; str: "b"; } | ||
>num : 1 | ||
>1 : 1 | ||
>str : "b" | ||
>"b" : "b" | ||
|
||
type Item = | ||
>Item : Item | ||
|
||
| { kind: "a", subkind: 0, value: string } | ||
>kind : "a" | ||
>subkind : 0 | ||
>value : string | ||
|
||
| { kind: "a", subkind: 1, value: number } | ||
>kind : "a" | ||
>subkind : 1 | ||
>value : number | ||
|
||
| { kind: "b" } | ||
>kind : "b" | ||
|
||
const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a" | ||
>item1 : Item | ||
>{ subkind: 1, kind: "b" } : { subkind: number; kind: "b"; } | ||
>subkind : number | ||
>1 : 1 | ||
>kind : "b" | ||
>"b" : "b" | ||
|
||
const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property | ||
>item2 : Item | ||
>{ kind: "b", subkind: 1 } : { kind: "b"; subkind: number; } | ||
>kind : "b" | ||
>"b" : "b" | ||
>subkind : number | ||
>1 : 1 | ||
|
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,36 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/typescript-go/issues/1020 | ||
|
||
// This tests ensures the change also works for discriminated unions with more than 10 cases | ||
|
||
type Thing = | ||
| { str: "a", num: 0 } | ||
| { str: "b" } | ||
| { str: "c" } | ||
| { str: "d" } | ||
| { str: "e" } | ||
| { str: "f" } | ||
| { str: "g" } | ||
| { str: "h" } | ||
| { str: "i" } | ||
| { str: "j" } | ||
| { str: "k" } | ||
| { str: "l" } | ||
| { str: "m" } | ||
| { str: "n" } | ||
| { str: "o" } | ||
| { num: 1 } | ||
|
||
const thing1: Thing = { str: "a", num: 0 } | ||
const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error | ||
const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error | ||
|
||
type Item = | ||
| { kind: "a", subkind: 0, value: string } | ||
| { kind: "a", subkind: 1, value: number } | ||
| { kind: "b" } | ||
|
||
const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a" | ||
const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property |
Oops, something went wrong.
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.
Doesn't this behave differently from the original code? The original code would still continue the loop of the discriminate was undefined.
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.
Yes, but the exit is intended. We only want to build a map when all constituents have a discriminant. When some constituents lack a discriminant, we want to skip the map optimization and go through the regular
discriminateTypeByDiscriminableItems
code path.