hclsyntax: Detect and reject invalid nested splat result #724
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Unfortunately two of the weird splat rules we included to mimic Terraform v0.11's bizarre splat behavior interact in a broken way when combined using nested splat expressions:
When both of these rules are combined using a nested splat -- rule 2 inside rule 1 -- the second rule causes the nested results to have varying types based on the scalar value rather than only on the list element type, which therefore violates the assumption made by rule 1 that because all elements of a list or set have the same type therefore all splat results on those elements must have the same type.
This previously caused a crash due to asking cty to construct an invalid list. We'll now handle this as a normal error diagnostic instead of a crash. The error message is not great -- it uses terminology that probably only an expert HCL user would understand fully -- but thankfully this is an edge case that apparently very few people have actually tried, since it took five years for anyone to trip over the bug and report it.
Ideally we'd make this situation work and generate a valid result of a different type, such as a list of lists rather than an invalid list of different tuple types. However, it's not clear that we can make that change retroactively without breaking existing code, and combining splat expressions in this way seems to be an edge case anyway -- it took many years before someone actually tried this combination and found the bug. Perhaps a future change will find a way to resolve this without an error, but for now we'll compromise on treating this as invalid since we know it wasn't working before anyway but at least this avoids crashing the host program completely.
This fixes #723 and would ultimately (after release/upgrade) fix both hashicorp/terraform#36284 and opentofu/opentofu#2327, although only addresses the fact that it was panicking, rather than making the reported situation actually work.