Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jeskew
Copy link
Contributor

@jeskew jeskew commented Oct 24, 2024

Resolves #15114

Microsoft Reviewers: Open in CodeFlow

@jeskew jeskew requested a review from a team October 24, 2024 16:29
Copy link
Contributor

Test this change out locally with the following install scripts (Action run 11503531047)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 11503531047
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 11503531047"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 11503531047
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 11503531047"

Copy link
Contributor

Dotnet Test Results

    72 files   -     36      72 suites   - 36   29m 53s ⏱️ - 13m 41s
11 374 tests  -     24  11 374 ✅  -     24  0 💤 ±0  0 ❌ ±0 
26 479 runs   - 13 232  26 479 ✅  - 13 232  0 💤 ±0  0 ❌ ±0 

Results for commit 71afb81. ± Comparison against base commit 48315b8.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keyvault's property VaultUri is not recognized.
1 participant