regal icon indicating copy to clipboard operation
regal copied to clipboard

Figure out how to best determine vars in scope

Open anderseknert opened this issue 1 year ago • 0 comments

A few rules need to know which variables (and rules from the package) are in scope at a given location. This is needed for example when something like input[x] is encountered, and a rule needs to determine whether x is used as an input or an output variable. Currently, this is done by walking the rule and grabbing all variables along the way until the given location (well, the walk doesn't really stop there, but we'll ignore all vars after that). This however means that we might also grab some vars which aren't in scope, like those declared inside of a comprehension or an every block. Consider:

package policy

a := 1

b {
     c := [d | d := input[e]]

     # a, b, c in "scope" here, but *not* d or e
}

The "global" vars, or rules, are obviously easy, but inside of the rule there could be any levels of nesting, and we can't

  1. Modify the AST provided as input and add a "scope" or "nesting" level attribute to all nodes pointing to its parent. This would be cleaner to deal with in Rego alone, but taint the AST with a lot of noise.
  2. Introduce a new built-in function similar to walk but which traverses "upwards" from a given location, and does not traverse deeper than the original location. This would also have the benefit of not traversing the whole rule which we have to do with walk.
  3. Something else?

Ideas welcome!

anderseknert avatar Aug 16 '23 07:08 anderseknert