Skip to content

Commit d0b4a68

Browse files
author
Pam Selle
authored
Merge pull request #433 from hashicorp/pselle/marked-for
Return an error for invalid for expressions with marks
2 parents baa494e + dfa6aff commit d0b4a68

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

hclsyntax/expression.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,19 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
11881188
continue
11891189
}
11901190

1191+
if key.IsMarked() {
1192+
diags = append(diags, &hcl.Diagnostic{
1193+
Severity: hcl.DiagError,
1194+
Summary: "Invalid object key",
1195+
Detail: "Marked values cannot be used as object keys.",
1196+
Subject: e.KeyExpr.Range().Ptr(),
1197+
Context: &e.SrcRange,
1198+
Expression: e.KeyExpr,
1199+
EvalContext: childCtx,
1200+
})
1201+
continue
1202+
}
1203+
11911204
val, valDiags := e.ValExpr.Value(childCtx)
11921205
diags = append(diags, valDiags...)
11931206

hclsyntax/expression_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,37 @@ upper(
902902
}).Mark("sensitive"),
903903
0,
904904
},
905-
905+
{ // Marked map member carries marks through
906+
`{for k, v in things: k => !v}`,
907+
&hcl.EvalContext{
908+
Variables: map[string]cty.Value{
909+
"things": cty.MapVal(map[string]cty.Value{
910+
"a": cty.True.Mark("sensitive"),
911+
"b": cty.False,
912+
}),
913+
},
914+
},
915+
cty.ObjectVal(map[string]cty.Value{
916+
"a": cty.False.Mark("sensitive"),
917+
"b": cty.True,
918+
}),
919+
0,
920+
},
921+
{ // Error when using marked value as object key
922+
`{for v in things: v => "${v}-friend"}`,
923+
&hcl.EvalContext{
924+
Variables: map[string]cty.Value{
925+
"things": cty.MapVal(map[string]cty.Value{
926+
"a": cty.StringVal("rosie").Mark("sensitive"),
927+
"b": cty.StringVal("robin"),
928+
}),
929+
},
930+
},
931+
cty.ObjectVal(map[string]cty.Value{
932+
"robin": cty.StringVal("robin-friend"),
933+
}),
934+
1,
935+
},
906936
{
907937
`[{name: "Steve"}, {name: "Ermintrude"}].*.name`,
908938
nil,

0 commit comments

Comments
 (0)