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

Improve ternary type inference #15399

Merged
merged 3 commits into from
Oct 27, 2024
Merged

Improve ternary type inference #15399

merged 3 commits into from
Oct 27, 2024

Conversation

jeskew
Copy link
Contributor

@jeskew jeskew commented Oct 24, 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:

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

@jeskew jeskew requested a review from a team October 24, 2024 22:01
Copy link
Contributor

github-actions bot commented Oct 24, 2024

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

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

Copy link
Contributor

Dotnet Test Results

    72 files   -     36      72 suites   - 36   29m 18s ⏱️ - 14m 16s
11 376 tests  -     22  11 376 ✅  -     22  0 💤 ±0  0 ❌ ±0 
26 483 runs   - 13 228  26 483 ✅  - 13 228  0 💤 ±0  0 ❌ ±0 

Results for commit 252c1f7. ± Comparison against base commit 48315b8.

Copy link
Contributor

@shenglol shenglol left a comment

Choose a reason for hiding this comment

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

:shipit:

@jeskew jeskew merged commit d759532 into main Oct 27, 2024
47 checks passed
@jeskew jeskew deleted the jeskew/15397 branch October 27, 2024 00:56
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.

Improve ternary type inference
2 participants