Skip to content

Commit b48ba6e

Browse files
committed
pass marks through unknown ForExpr values
Some of the unknown value paths within ForExpr were dropping marks from the result.
1 parent bbfec2d commit b48ba6e

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

hclsyntax/expression.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,9 +1430,9 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
14301430
})
14311431
return cty.DynamicVal, diags
14321432
}
1433-
if !collVal.IsKnown() {
1434-
return cty.DynamicVal, diags
1435-
}
1433+
1434+
// Grab the CondExpr marks when we're returning early with an unknown
1435+
var condMarks cty.ValueMarks
14361436

14371437
// Before we start we'll do an early check to see if any CondExpr we've
14381438
// been given is of the wrong type. This isn't 100% reliable (it may
@@ -1460,6 +1460,9 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
14601460
})
14611461
return cty.DynamicVal, diags
14621462
}
1463+
1464+
_, condMarks = result.Unmark()
1465+
14631466
_, err := convert.Convert(result, cty.Bool)
14641467
if err != nil {
14651468
diags = append(diags, &hcl.Diagnostic{
@@ -1478,6 +1481,10 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
14781481
}
14791482
}
14801483

1484+
if !collVal.IsKnown() {
1485+
return cty.DynamicVal.WithMarks(append(marks, condMarks)...), diags
1486+
}
1487+
14811488
if e.KeyExpr != nil {
14821489
// Producing an object
14831490
var vals map[string]cty.Value
@@ -1518,6 +1525,12 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
15181525
known = false
15191526
continue
15201527
}
1528+
1529+
// Extract and merge marks from the include expression into the
1530+
// main set of marks
1531+
_, includeMarks := includeRaw.Unmark()
1532+
marks = append(marks, includeMarks)
1533+
15211534
include, err := convert.Convert(includeRaw, cty.Bool)
15221535
if err != nil {
15231536
if known {
@@ -1541,7 +1554,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
15411554

15421555
// Extract and merge marks from the include expression into the
15431556
// main set of marks
1544-
includeUnmarked, includeMarks := include.Unmark()
1557+
includeUnmarked, _ := include.Unmark()
15451558
marks = append(marks, includeMarks)
15461559
if includeUnmarked.False() {
15471560
// Skip this element
@@ -1566,6 +1579,10 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
15661579
known = false
15671580
continue
15681581
}
1582+
1583+
_, keyMarks := keyRaw.Unmark()
1584+
marks = append(marks, keyMarks)
1585+
15691586
if !keyRaw.IsKnown() {
15701587
known = false
15711588
continue
@@ -1588,8 +1605,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
15881605
continue
15891606
}
15901607

1591-
key, keyMarks := key.Unmark()
1592-
marks = append(marks, keyMarks)
1608+
key, _ = key.Unmark()
15931609

15941610
val, valDiags := e.ValExpr.Value(childCtx)
15951611
diags = append(diags, valDiags...)
@@ -1619,7 +1635,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
16191635
}
16201636

16211637
if !known {
1622-
return cty.DynamicVal, diags
1638+
return cty.DynamicVal.WithMarks(marks...), diags
16231639
}
16241640

16251641
if e.Group {
@@ -1665,6 +1681,12 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
16651681
known = false
16661682
continue
16671683
}
1684+
1685+
// Extract and merge marks from the include expression into the
1686+
// main set of marks
1687+
_, includeMarks := includeRaw.Unmark()
1688+
marks = append(marks, includeMarks)
1689+
16681690
if !includeRaw.IsKnown() {
16691691
// We will eventually return DynamicVal, but we'll continue
16701692
// iterating in case there are other diagnostics to gather
@@ -1690,10 +1712,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
16901712
continue
16911713
}
16921714

1693-
// Extract and merge marks from the include expression into the
1694-
// main set of marks
1695-
includeUnmarked, includeMarks := include.Unmark()
1696-
marks = append(marks, includeMarks)
1715+
includeUnmarked, _ := include.Unmark()
16971716
if includeUnmarked.False() {
16981717
// Skip this element
16991718
continue
@@ -1706,7 +1725,7 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
17061725
}
17071726

17081727
if !known {
1709-
return cty.DynamicVal, diags
1728+
return cty.DynamicVal.WithMarks(marks...), diags
17101729
}
17111730

17121731
return cty.TupleVal(vals).WithMarks(marks...), diags

hclsyntax/expression_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,49 @@ upper(
11431143
}).Mark("sensitive"),
11441144
0,
11451145
},
1146+
{
1147+
`{ for k, v in things: k => v if k != unknown_secret }`,
1148+
&hcl.EvalContext{
1149+
Variables: map[string]cty.Value{
1150+
"things": cty.MapVal(map[string]cty.Value{
1151+
"a": cty.True,
1152+
"b": cty.False,
1153+
"c": cty.False,
1154+
}),
1155+
"unknown_secret": cty.UnknownVal(cty.String).Mark("sensitive"),
1156+
},
1157+
},
1158+
cty.DynamicVal.Mark("sensitive"),
1159+
0,
1160+
},
1161+
{
1162+
`[ for v in things: v if v != unknown_secret ]`,
1163+
&hcl.EvalContext{
1164+
Variables: map[string]cty.Value{
1165+
"things": cty.TupleVal([]cty.Value{
1166+
cty.StringVal("a"),
1167+
cty.StringVal("b"),
1168+
}),
1169+
"unknown_secret": cty.UnknownVal(cty.String).Mark("sensitive"),
1170+
},
1171+
},
1172+
cty.DynamicVal.Mark("sensitive"),
1173+
0,
1174+
},
1175+
{
1176+
`[ for v in things: v if v != secret ]`,
1177+
&hcl.EvalContext{
1178+
Variables: map[string]cty.Value{
1179+
"things": cty.TupleVal([]cty.Value{
1180+
cty.UnknownVal(cty.String).Mark("mark"),
1181+
cty.StringVal("b"),
1182+
}),
1183+
"secret": cty.StringVal("b").Mark("sensitive"),
1184+
},
1185+
},
1186+
cty.DynamicVal.WithMarks(cty.NewValueMarks("mark", "sensitive")),
1187+
0,
1188+
},
11461189
{
11471190
`[{name: "Steve"}, {name: "Ermintrude"}].*.name`,
11481191
nil,

0 commit comments

Comments
 (0)