Error if attempting to use marked value as key

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.
This commit is contained in:
Pam Selle 2020-12-15 16:07:30 -05:00
parent 8045083719
commit c227eb4f81
2 changed files with 26 additions and 0 deletions

View File

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

View File

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