Assigned types should be control-flow sensitive
- 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
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