Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix boolean short-circuit diagnostic handling #719

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions hclsyntax/expression_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ var (
switch {
// if both are unknown, we don't short circuit anything
case !lhs.IsKnown() && !rhs.IsKnown():
// short-circuit left-to-right when encountering a good unknown
// value and both are unknown.
if !lhsDiags.HasErrors() {
return cty.UnknownVal(cty.Bool).RefineNotNull(), lhsDiags
}
// If the LHS has an error, the RHS might too. Don't
// short-circuit so both diags get collected.
return cty.NilVal, nil

// for ||, a single true is the controlling condition
Expand All @@ -46,7 +53,7 @@ var (
case rhs.IsKnown() && rhs.True():
return cty.True, rhsDiags

// if the opposing side is false we can't sort-circuit based on
// if the opposing side is false we can't short-circuit based on
// boolean logic, so an unknown becomes the controlling condition
case !lhs.IsKnown() && rhs.False():
return cty.UnknownVal(cty.Bool).RefineNotNull(), lhsDiags
Expand All @@ -62,9 +69,16 @@ var (
Type: cty.Bool,

ShortCircuit: func(lhs, rhs cty.Value, lhsDiags, rhsDiags hcl.Diagnostics) (cty.Value, hcl.Diagnostics) {

switch {
// if both are unknown, we don't short circuit anything
case !lhs.IsKnown() && !rhs.IsKnown():
// short-circuit left-to-right when encountering a good unknown
// value and both are unknown.
if !lhsDiags.HasErrors() {
return cty.UnknownVal(cty.Bool).RefineNotNull(), lhsDiags
}
// If the LHS has an error, the RHS might too. Don't
// short-circuit so both diags get collected.
return cty.NilVal, nil

// For &&, a single false is the controlling condition
Expand All @@ -73,14 +87,13 @@ var (
case rhs.IsKnown() && rhs.False():
return cty.False, rhsDiags

// if the opposing side is true we can't sort-circuit based on
// if the opposing side is true we can't short-circuit based on
// boolean logic, so an unknown becomes the controlling condition
case !lhs.IsKnown() && rhs.True():
return cty.UnknownVal(cty.Bool).RefineNotNull(), lhsDiags
case !rhs.IsKnown() && lhs.True():
return cty.UnknownVal(cty.Bool).RefineNotNull(), rhsDiags
}

return cty.NilVal, nil
},
}
Expand Down Expand Up @@ -242,9 +255,6 @@ func (e *BinaryOpExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
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
// executing the operation.
if e.Op.ShortCircuit != nil {
forceResult, diags := e.Op.ShortCircuit(lhsVal, rhsVal, lhsDiags, rhsDiags)
if forceResult != cty.NilVal {
Expand All @@ -256,6 +266,8 @@ func (e *BinaryOpExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
}
}

diags = append(diags, lhsDiags...)
diags = append(diags, rhsDiags...)
if diags.HasErrors() {
// Don't actually try the call if we have errors, since the this will
// probably just produce confusing duplicate diagnostics.
Expand Down
10 changes: 10 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2616,6 +2616,16 @@ func TestExpressionErrorMessages(t *testing.T) {
"Function calls not allowed",
`Functions may not be called here.`,
},
{
`map != null || map["key"] == "value"`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"map": cty.NullVal(cty.Map(cty.String)),
},
},
"Attempt to index null value",
`This value is null, so it does not have any indices.`,
},
}

for _, test := range tests {
Expand Down
Loading