Skip to content

Commit

Permalink
short-circuiting must retain marks
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Nov 30, 2024
1 parent 945568e commit 7947f74
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
6 changes: 3 additions & 3 deletions hclsyntax/expression_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ func (e *BinaryOpExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
return cty.UnknownVal(e.Op.Type), diags
}

_, lhsMarks := lhsVal.Unmark()
_, rhsMarks := rhsVal.Unmark()
lhsVal, lhsMarks := lhsVal.Unmark()
rhsVal, rhsMarks := rhsVal.Unmark()

// If we short-circuited above and still passed the type-check of RHS then
// we'll halt here and return the short-circuit result rather than actually
Expand Down Expand Up @@ -261,7 +261,7 @@ func (e *BinaryOpExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
return cty.UnknownVal(e.Op.Type), diags
}

return result, diags
return result.WithMarks(lhsMarks, rhsMarks), diags
}

func (e *BinaryOpExpr) Range() hcl.Range {
Expand Down
48 changes: 48 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,54 @@ EOT
cty.UnknownVal(cty.Bool),
0,
},
{
// short circuit calls must still retain marks
`lhsTrue || rhsUnknown`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"lhsTrue": cty.True.Mark("a"),
"rhsUnknown": cty.UnknownVal(cty.Bool).Mark("b"),
},
},
cty.True.Mark("a").Mark("b"),
0,
},
{
// short circuit calls must still retain marks
`lhsUnknown || rhsTrue`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"rhsTrue": cty.True.Mark("a"),
"lhsUnknown": cty.UnknownVal(cty.Bool).Mark("b"),
},
},
cty.True.Mark("a").Mark("b"),
0,
},
{
// short circuit calls must still retain marks
`lhsUnknown && rhsFalse`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"rhsFalse": cty.False.Mark("a"),
"lhsUnknown": cty.UnknownVal(cty.Bool).Mark("b"),
},
},
cty.False.Mark("a").Mark("b"),
0,
},
{
// short circuit calls must still retain marks
`lhsFalse && rhsUnknown`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"lhsFalse": cty.False.Mark("a"),
"rhsUnknown": cty.UnknownVal(cty.Bool).Mark("b"),
},
},
cty.False.Mark("a").Mark("b"),
0,
},
{
`true ? var : null`,
&hcl.EvalContext{
Expand Down

0 comments on commit 7947f74

Please sign in to comment.