-
Notifications
You must be signed in to change notification settings - Fork 757
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
Use target rather than expression type as narrowing base case #15398
Open
jeskew
wants to merge
1
commit into
main
Choose a base branch
from
jeskew/15114
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
Contributor
Test this change out locally with the following install scripts (Action run 11503531047) VSCode
Azure CLI
|
jeskew
added a commit
that referenced
this pull request
Oct 27, 2024
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)
shenglol
reviewed
Dec 3, 2024
@@ -43,10 +43,10 @@ func typedArg(input string[]) positiveInt => length(input) | |||
//@[05:013) Function typedArg. Type: string[] => int. Declaration start char: 0, length: 58 | |||
|
|||
func barTest() array => ['abc', 'def'] | |||
//@[05:012) Function barTest. Type: () => ['abc', 'def']. Declaration start char: 0, length: 38 | |||
//@[05:012) Function barTest. Type: () => [any, any]. Declaration start char: 0, length: 38 |
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.
Should the return types of these functions be array
as well?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #15114
Microsoft Reviewers: Open in CodeFlow