hclsyntax: null[*] to return an empty tuple

The rules for the splat operator call for it to return an empty tuple
when its operand is null, but this rule was previously being
overridden by another rule that a value whose type is unknown
causes the operator to return an unknown value of unknown
type.

This was initially reported and discussed in Terraform, under
hashicorp/terraform#26746.
This commit is contained in:
Rémi Lapeyre 2020-11-18 23:26:15 +01:00 committed by GitHub
parent 9dabf7d816
commit 128a2f1879
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -1320,12 +1320,6 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
}
sourceTy := sourceVal.Type()
if sourceTy == cty.DynamicPseudoType {
// If we don't even know the _type_ of our source value yet then
// we'll need to defer all processing, since we can't decide our
// result type either.
return cty.DynamicVal, diags
}
// A "special power" of splat expressions is that they can be applied
// both to tuples/lists and to other values, and in the latter case
@ -1349,6 +1343,13 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
return cty.DynamicVal, diags
}
if sourceTy == cty.DynamicPseudoType {
// If we don't even know the _type_ of our source value yet then
// we'll need to defer all processing, since we can't decide our
// result type either.
return cty.DynamicVal, diags
}
if autoUpgrade {
sourceVal = cty.TupleVal([]cty.Value{sourceVal})
sourceTy = sourceVal.Type()

View File

@ -907,6 +907,12 @@ upper(
}),
0,
},
{
`null[*]`,
nil,
cty.EmptyTupleVal,
0,
},
{
`{name: "Steve"}[*].name`,
nil,