From 128a2f1879af28f8bca39d5c1984f3793f1ea1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 18 Nov 2020 23:26:15 +0100 Subject: [PATCH] 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. --- hclsyntax/expression.go | 13 +++++++------ hclsyntax/expression_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/hclsyntax/expression.go b/hclsyntax/expression.go index 7576781..ca59461 100644 --- a/hclsyntax/expression.go +++ b/hclsyntax/expression.go @@ -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() diff --git a/hclsyntax/expression_test.go b/hclsyntax/expression_test.go index 8faa2ec..f4b4fdf 100644 --- a/hclsyntax/expression_test.go +++ b/hclsyntax/expression_test.go @@ -907,6 +907,12 @@ upper( }), 0, }, + { + `null[*]`, + nil, + cty.EmptyTupleVal, + 0, + }, { `{name: "Steve"}[*].name`, nil,