Azure / Azure/bicep

Assigned types should be control-flow sensitive

Open
#12,121 7 comments 1 reaction 0 assignees View on GitHub
enhancement Needs: Upvote type system
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 2h
Merged PRs (30d)
79

Description

**Is your feature request related to a problem? Please describe.**
Within a ternary expression, Bicep can sometimes generate spurious diagnostics when a subexpression within the `true` or `false` clause interacts with a symbol that should be safe given the check in the `if` clause of the ternary.

For example, the following snippet:

```bicep
var db = {
name: 'db1'
}

resource postgreSQL 'Microsoft.DBForPostgreSQL/servers@2017-12-01' existing = {
name: 'name'
resource database 'database' = {
name: db.name
properties: {
charset: contains(db, 'charset') ? db.charset : 'utf8'
collation: contains(db, 'collation') ? db.collation : 'English_United States.1252'
}
}
}
```

will generate the following diagnostics:

```
[BCP053 (Error)] The type "object" does not contain property "charset". Available properties include "name".
[BCP053 (Error)] The type "object" does not contain property "collation". Available properties include "name".
```

**Describe the solution you'd like**
Similar to how C# will recognize that `foo` is non-null inside of a block that starts with `if (foo is not null) {`, the Bicep compiler's type inference should take an expression's position in control flow into account when assigning a type to an expression. Given that Bicep does not have guard statements that control entry into blocks, this should primarily impact ternary statements. If possible, flow-sensitive typing should take into account:
* equality operators
* comparison operators, and
* some core functions in the `sys` namespace (e.g., `empty`, `length`, `contains`, `endsWith`, `startsWith`, and negations thereof).

Contributor guide

Open the contributing guide

Research direction

Start by examining the Bicep compiler's type inference for ternary expressions and how it handles the true and false clauses. Define flow-sensitive behavior for the listed equality, comparison, and sys functions, then verify that the example no longer emits BCP053 diagnostics without regressing other type checks.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.