Follow-up from hashicorp/hcl-lang#365 (comment)
Context
Currently we recognise index expression such as var.foo[var.bar], in the sense that we provide:
- completion
- hover
- semantic tokens
- go-to-definition/references
While this is helpful, we currently treat all [] (indexes) in references as if they were a map, i.e. something indexable with keys of type string.
https://github.com/hashicorp/hcl-lang/blob/f43c27231c1001f4682f6fe72011cdabe692bdef/decoder/expr_any_index.go#L19-L25
Because we check for type convertability and strings/numbers are convertible in cty, this does not impact the functionality in a major way. However, it can make it less helpful in some contexts.
- we're unable to detect invalid or poor practice configuration (e.g.
var.map[var.number] or var.list[var.string])
- we're unable to sort completion candidates by prioritising number type for lists/tuples and string type for maps
- the hover data may not be as accurate, or at least not tell the whole story
Proposal
Follow-up from hashicorp/hcl-lang#365 (comment)
Context
Currently we recognise index expression such as
var.foo[var.bar], in the sense that we provide:While this is helpful, we currently treat all
[](indexes) in references as if they were a map, i.e. something indexable with keys of type string.https://github.com/hashicorp/hcl-lang/blob/f43c27231c1001f4682f6fe72011cdabe692bdef/decoder/expr_any_index.go#L19-L25
Because we check for type convertability and strings/numbers are convertible in cty, this does not impact the functionality in a major way. However, it can make it less helpful in some contexts.
var.map[var.number]orvar.list[var.string])Proposal
[]are inferred from the type of the referencevar.map[var.number]orvar.list[var.string]