Enhance `union(object...)` return type inference
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Is your feature request related to a problem? Please describe.**
The `union` function today will perform a deep merge on objects, but the type system will cast the return type down to `object` unless every argument has a literal type. This causes errors that would be evident from manual inspection to pass unremarked by the type checker, such as:
```bicep
// type: { a: 'a' }
var a = { a: 'a' }
// type: { b: 'b' }
var b = { b: 'b'}
// type: { a: 'a', b: 'b' }
var c = union(a, b)
// the compiler will catch this and raise a diagnostic
output c = c.c
param one { one: string }
param two { two: string }
// type: object
var three = union(one, two)
// no diagnostic, but this will cause a runtime error
output three = three.three
```
**Describe the solution you'd like**
Even with non-literal types, `union(object...)` could perform some inference based on its arguments. This will be rather time consuming because `union(object...)` performs a deep merge.
Contributor guide
Research direction
Start by tracing how union(object...) is represented by the type system and how the compiler infers its return type for literal and non-literal arguments. Compare the current diagnostics for c.c and three.three in the issue examples. Done means deep-merge inference preserves useful properties for non-literal object arguments and reports the invalid three.three access.
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