From a085fdcd82b92760f23e0a47d889c71d586a5c8b Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 12 Dec 2018 15:51:40 -0800 Subject: [PATCH] hcl/hclsyntax: Don't eat errors in IndexExpr collection or key Due to incorrect diagnostics discipline here, the result of the index operation was overwriting any diagnostics from either the collection or the key expressions. --- hcl/hclsyntax/expression.go | 5 +++-- hcl/hclsyntax/expression_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hcl/hclsyntax/expression.go b/hcl/hclsyntax/expression.go index db3cbfd..7f8d2b0 100644 --- a/hcl/hclsyntax/expression.go +++ b/hcl/hclsyntax/expression.go @@ -604,8 +604,9 @@ func (e *IndexExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { diags = append(diags, collDiags...) diags = append(diags, keyDiags...) - val, diags := hcl.Index(coll, key, &e.SrcRange) - setDiagEvalContext(diags, e, ctx) + val, indexDiags := hcl.Index(coll, key, &e.SrcRange) + setDiagEvalContext(indexDiags, e, ctx) + diags = append(diags, indexDiags...) return val, diags } diff --git a/hcl/hclsyntax/expression_test.go b/hcl/hclsyntax/expression_test.go index fe44eb2..a3c5f75 100644 --- a/hcl/hclsyntax/expression_test.go +++ b/hcl/hclsyntax/expression_test.go @@ -981,6 +981,16 @@ upper( cty.StringVal("hello"), 0, }, + { + `["boop"].foo[index]`, // index is a variable to force IndexExpr instead of traversal + &hcl.EvalContext{ + Variables: map[string]cty.Value{ + "index": cty.NumberIntVal(0), + }, + }, + cty.DynamicVal, + 1, // expression ["boop"] does not have attributes + }, { `foo`,