aboutsummaryrefslogtreecommitdiffstats
path: root/prog/expr.go
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2024-09-11 16:04:43 +0200
committerAleksandr Nogikh <nogikh@google.com>2024-09-11 14:55:22 +0000
commit7308f9d5a4da8ea4e2a9a0748ceb0921333c11f4 (patch)
tree3c4c0d6bdfd082e089a5514c0c06e7eabec864f9 /prog/expr.go
parentfd0ad8a3f899365059471f5fdefdc73082ce0777 (diff)
prog: allow deeper nesting of conditional fields patching
There is a totally valid situation when we could be recursively patching conditional fields: if by changing a field's value we insert new resource constructor calls. It's a bug to skip conditional field patching for them. Allow up to 2 nested patchConditionalFields() calls and panic if there happen to be more. Add a test that reproduces the situation described above.
Diffstat (limited to 'prog/expr.go')
-rw-r--r--prog/expr.go13
1 files changed, 8 insertions, 5 deletions
diff --git a/prog/expr.go b/prog/expr.go
index cfe6eb70b..e9446ab2c 100644
--- a/prog/expr.go
+++ b/prog/expr.go
@@ -67,11 +67,14 @@ func makeArgFinder(t *Target, c *Call, unionArg *UnionArg, parents parentStack)
}
func (r *randGen) patchConditionalFields(c *Call, s *state) (extra []*Call, changed bool) {
- if r.inPatchConditional {
- return nil, false
- }
- r.inPatchConditional = true
- defer func() { r.inPatchConditional = false }()
+ if r.patchConditionalDepth > 1 {
+ // Some nested patchConditionalFields() calls are fine as we could trigger a resource
+ // constructor via generateArg(). But since nested createResource() calls are prohibited,
+ // patchConditionalFields() should never be nested more than 2 times.
+ panic("third nested patchConditionalFields call")
+ }
+ r.patchConditionalDepth++
+ defer func() { r.patchConditionalDepth-- }()
var extraCalls []*Call
var anyPatched bool