-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ternary type inference (#15399)
Resolves #15397 Worked on this as an alternative way to address #15114 (instead of #15398). This PR does two things: 1. If a ternary has a literally-typed condition (i.e., it is definitely true or definitely false), the type engine will use the type of the active branch's expression. E.g., the type of `true ? 'a' : 'b'` is `'a'`, and the type of `false ? 'a' : 'b'` is `'b'`. There was a TODO comment in TypeAssignmentVisitor suggesting this change. 2. If the types of both branches can be combined instead of represented as a union, the type engine will do so. For example, the type of `unknownCondition ? 'a' : 'b'` is `'a' | 'b'` (a union), but the type of `unknownCondition ? [stringParam] : [intParam]` is `[int | string]` (assuming the type of `stringParam` is `string` and the type of `intParam` is `int`). This change relies on existing type collapsing logic, so it will handle things like combining refinements on string types and combining objects into tagged unions if possible. One change I made to the TypeCollapser is to collapse objects that *can't* be combined into a tagged union into an object whose properties are a union of the possible property types of the inputs. This is similar to how we collapse tuple types. Given a template like the following: ```bicep param a { foo: string } param b { bar: string *: int } param condition bool var value = condition ? a : b ``` `value` would have a type of `{ bar: string, foo: int | string, *: int }`, with all properties flagged as optional. ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/15399)
- Loading branch information
Showing
22 changed files
with
429 additions
and
189 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.