Skip to content

Provide accurate constraints in index part of references #1596

Open
@radeksimko

Description

@radeksimko

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

  • 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] or var.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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions