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

Unable to index into union type from two arrays #2248

Closed
afscrome opened this issue Apr 13, 2021 · 3 comments · Fixed by #2696
Closed

Unable to index into union type from two arrays #2248

afscrome opened this issue Apr 13, 2021 · 3 comments · Fixed by #2696
Assignees
Labels
bug Something isn't working top 10 committed
Milestone

Comments

@afscrome
Copy link
Contributor

afscrome commented Apr 13, 2021

Bicep version
0.3.255 (VS Code)

Describe the bug
In the below code locations always gets set to some form of array, however trying to index into regions results in the error

Cannot index over expression of type "'northeurope'[] | array". Arrays or objects are required.bicep(BCP076)

To Reproduce

param isProdLike bool

var testLocations = [
  'northeurope'
]
var prodLocations = [
  'northeurope'
  'westeurope'
]
var locations = isProdLike ? prodLocations : testLocations
var primaryLocation = regions[0]

Additional context

The problem seems to be because locations is being considered as type 'northeurope'[] | array rather than just array. A workaround for this behaviour is to explicitly testLocations to be an array type, but I wouldn't expect this cast to be needed

// Error
var locations = isProdLike ? prodLocations :  testLocations
// No error
var locations = isProdLike ? prodLocations :  array(testLocations)
@ghost ghost added the Needs: Triage 🔍 label Apr 13, 2021
@majastrz
Copy link
Member

majastrz commented Apr 13, 2021

Yeah, it's a bug in our type system around handling of union types. Something like northeurope[] | array should collapse into array type.

@alex-frankel alex-frankel added the bug Something isn't working label Apr 14, 2021
@alex-frankel alex-frankel added this to the v0.4 milestone Apr 23, 2021
@majastrz
Copy link
Member

Related to #449 and #1735. I think we need a mechanism to simplify redundant union types in the type system. For this specific issue, we need to simplify the type of the item in an array type. But there are other cases. For example, string | 'a' | 'b' is equivalent to string. Similarly string | string is the same as the string type.

@majastrz majastrz self-assigned this May 17, 2021
@majastrz
Copy link
Member

In addition to not simplifying redundant union types, we're also not handling union types correctly when type checking array access expressions. Indexing over a type like 'northeurope'[] | array via an integer should produce an item type of 'northeurope' | any or just any once we have the logic to implify redundant union types.

A similar problem exists with the logic that type checks property access expressions.

@ghost ghost locked as resolved and limited conversation to collaborators May 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working top 10 committed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants