Skip to content

Commit bbfec2d

Browse files
committed
pass all marks through conditional expressions
1 parent d20d07f commit bbfec2d

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

hclsyntax/expression.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -788,21 +788,24 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic
788788
})
789789
return cty.UnknownVal(resultType), diags
790790
}
791-
if !condResult.IsKnown() {
792-
// we use the unmarked values throughout the unknown branch
793-
_, condResultMarks := condResult.Unmark()
794-
trueResult, trueResultMarks := trueResult.Unmark()
795-
falseResult, falseResultMarks := falseResult.Unmark()
796791

797-
// use a value to merge marks
798-
_, resMarks := cty.DynamicVal.WithMarks(condResultMarks, trueResultMarks, falseResultMarks).Unmark()
792+
// Now that we have all three values, collect all the marks for the result.
793+
// Since it's possible that a condition value could be unknown, and the
794+
// consumer needs to deal with any marks from either branch anyway, we must
795+
// always combine them for consistent results.
796+
condResult, condResultMarks := condResult.Unmark()
797+
trueResult, trueResultMarks := trueResult.Unmark()
798+
falseResult, falseResultMarks := falseResult.Unmark()
799+
var resMarks []cty.ValueMarks
800+
resMarks = append(resMarks, condResultMarks, trueResultMarks, falseResultMarks)
799801

802+
if !condResult.IsKnown() {
800803
trueRange := trueResult.Range()
801804
falseRange := falseResult.Range()
802805

803806
// if both branches are known to be null, then the result must still be null
804807
if trueResult.IsNull() && falseResult.IsNull() {
805-
return cty.NullVal(resultType).WithMarks(resMarks), diags
808+
return cty.NullVal(resultType).WithMarks(resMarks...), diags
806809
}
807810

808811
// We might be able to offer a refined range for the result based on
@@ -841,7 +844,7 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic
841844
ref = ref.NumberRangeUpperBound(hi, hiInc)
842845
}
843846

844-
return ref.NewValue().WithMarks(resMarks), diags
847+
return ref.NewValue().WithMarks(resMarks...), diags
845848
}
846849

847850
if trueResult.Type().IsCollectionType() && falseResult.Type().IsCollectionType() {
@@ -867,15 +870,15 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic
867870
}
868871

869872
ref = ref.CollectionLengthLowerBound(lo).CollectionLengthUpperBound(hi)
870-
return ref.NewValue().WithMarks(resMarks), diags
873+
return ref.NewValue().WithMarks(resMarks...), diags
871874
}
872875
}
873876

874877
ret := cty.UnknownVal(resultType)
875878
if trueRange.DefinitelyNotNull() && falseRange.DefinitelyNotNull() {
876879
ret = ret.RefineNotNull()
877880
}
878-
return ret.WithMarks(resMarks), diags
881+
return ret.WithMarks(resMarks...), diags
879882
}
880883

881884
condResult, err := convert.Convert(condResult, cty.Bool)
@@ -892,8 +895,6 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic
892895
return cty.UnknownVal(resultType), diags
893896
}
894897

895-
// Unmark result before testing for truthiness
896-
condResult, _ = condResult.UnmarkDeep()
897898
if condResult.True() {
898899
diags = append(diags, trueDiags...)
899900
if convs[0] != nil {
@@ -916,7 +917,7 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic
916917
trueResult = cty.UnknownVal(resultType)
917918
}
918919
}
919-
return trueResult, diags
920+
return trueResult.WithMarks(resMarks...), diags
920921
} else {
921922
diags = append(diags, falseDiags...)
922923
if convs[1] != nil {
@@ -939,7 +940,7 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic
939940
falseResult = cty.UnknownVal(resultType)
940941
}
941942
}
942-
return falseResult, diags
943+
return falseResult.WithMarks(resMarks...), diags
943944
}
944945
}
945946

hclsyntax/expression_template_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,14 @@ trim`,
318318
cty.UnknownVal(cty.String).Refine().NotNull().StringPrefixFull(strings.Repeat("_", 128)).NewValue(),
319319
0,
320320
},
321-
{ // marks from uninterpolated values are ignored
321+
{ // all marks are passed through to ensure result is always consistent
322322
`hello%{ if false } ${target}%{ endif }`,
323323
&hcl.EvalContext{
324324
Variables: map[string]cty.Value{
325325
"target": cty.StringVal("world").Mark("sensitive"),
326326
},
327327
},
328-
cty.StringVal("hello"),
328+
cty.StringVal("hello").Mark("sensitive"),
329329
0,
330330
},
331331
{ // marks from interpolated values are passed through

hclsyntax/expression_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,7 @@ EOT
21752175
}).Mark("sensitive"),
21762176
},
21772177
},
2178-
cty.NumberIntVal(1),
2178+
cty.NumberIntVal(1).Mark("sensitive"),
21792179
0,
21802180
},
21812181
{ // auto-converts collection types

0 commit comments

Comments
 (0)