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.
This commit is contained in:
Martin Atkins 2018-12-12 15:51:40 -08:00
parent 854da97291
commit a085fdcd82
2 changed files with 13 additions and 2 deletions

View File

@ -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
}

View File

@ -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`,