Skip to content

Commit 39015a0

Browse files
committed
hclsyntax: Fix for expressions over marked values
A for expression over a marked collection should result in a new collection with the same marks. Previously this would fail with a type error.
1 parent a0de289 commit 39015a0

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

hclsyntax/expression.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,9 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
971971
if collVal.Type() == cty.DynamicPseudoType {
972972
return cty.DynamicVal, diags
973973
}
974+
// Unmark collection before checking for iterability, because marked
975+
// values cannot be iterated
976+
collVal, marks := collVal.Unmark()
974977
if !collVal.CanIterateElements() {
975978
diags = append(diags, &hcl.Diagnostic{
976979
Severity: hcl.DiagError,
@@ -1178,7 +1181,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
11781181
}
11791182
}
11801183

1181-
return cty.ObjectVal(vals), diags
1184+
return cty.ObjectVal(vals).WithMarks(marks), diags
11821185

11831186
} else {
11841187
// Producing a tuple
@@ -1254,7 +1257,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
12541257
return cty.DynamicVal, diags
12551258
}
12561259

1257-
return cty.TupleVal(vals), diags
1260+
return cty.TupleVal(vals).WithMarks(marks), diags
12581261
}
12591262
}
12601263

hclsyntax/expression_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,41 @@ upper(
854854
}),
855855
0,
856856
},
857+
{ // Marked sequence results in a marked tuple
858+
`[for x in things: x if x != ""]`,
859+
&hcl.EvalContext{
860+
Variables: map[string]cty.Value{
861+
"things": cty.ListVal([]cty.Value{
862+
cty.StringVal("a"),
863+
cty.StringVal("b"),
864+
cty.StringVal(""),
865+
cty.StringVal("c"),
866+
}).Mark("sensitive"),
867+
},
868+
},
869+
cty.TupleVal([]cty.Value{
870+
cty.StringVal("a"),
871+
cty.StringVal("b"),
872+
cty.StringVal("c"),
873+
}).Mark("sensitive"),
874+
0,
875+
},
876+
{ // Marked map results in a marked object
877+
`{for k, v in things: k => !v}`,
878+
&hcl.EvalContext{
879+
Variables: map[string]cty.Value{
880+
"things": cty.MapVal(map[string]cty.Value{
881+
"a": cty.True,
882+
"b": cty.False,
883+
}).Mark("sensitive"),
884+
},
885+
},
886+
cty.ObjectVal(map[string]cty.Value{
887+
"a": cty.False,
888+
"b": cty.True,
889+
}).Mark("sensitive"),
890+
0,
891+
},
857892

858893
{
859894
`[{name: "Steve"}, {name: "Ermintrude"}].*.name`,

0 commit comments

Comments
 (0)