@@ -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
0 commit comments