Skip to content

Commit

Permalink
Error if attempting to use marked value as key
Browse files Browse the repository at this point in the history
When evaluating an HCL expression attempting
to use a marked value as an object key,
return an error rather than falling through
to the cty panic. The error style mimics similar
errors in the area.
  • Loading branch information
pselle committed Dec 16, 2020
1 parent 8045083 commit c227eb4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,19 @@ func (e *ObjectConsExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics
continue
}

if key.IsMarked() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Marked value as key",
Detail: "Can't use a marked value as a key.",
Subject: item.ValueExpr.Range().Ptr(),
Expression: item.KeyExpr,
EvalContext: ctx,
})
known = false
continue
}

var err error
key, err = convert.Convert(key, cty.String)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,19 @@ upper(
}),
0,
},
{
// Marked values as object keys
`{(var.greeting) = "world", "goodbye" = "earth"}`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.ObjectVal(map[string]cty.Value{
"greeting": cty.StringVal("hello").Mark("marked"),
}),
},
},
cty.DynamicVal,
1,
},
{
`{"${var.greeting}" = "world"}`,
&hcl.EvalContext{
Expand Down

0 comments on commit c227eb4

Please sign in to comment.