Open
Description
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.
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]
orvar.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
- Ensure constraints for the index
[]
are inferred from the type of the reference- This may require some refactoring as we may not know anything about the type until we have parsed the target of the reference
- Produce diagnostics for invalid or poor practice configuration, e.g.
var.map[var.number]
orvar.list[var.string]
- Sort completion candidates by prioritising number type for lists/tuples and string type for maps
- Display constraint in hover separately from real type #1512