Skip to content

Commit

Permalink
Fix ID collision between partial eval and AST pruner (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones committed May 5, 2023
1 parent 75ed853 commit 5b634ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion interpreter/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ type astPruner struct {
// the overloads accordingly.
func PruneAst(expr *exprpb.Expr, macroCalls map[int64]*exprpb.Expr, state EvalState) *exprpb.ParsedExpr {
pruneState := NewEvalState()
maxID := int64(1)
for _, id := range state.IDs() {
v, _ := state.Value(id)
pruneState.SetValue(id, v)
if id > maxID {
maxID = id + 1
}
}
pruner := &astPruner{
expr: expr,
macroCalls: macroCalls,
state: pruneState,
nextExprID: 1}
nextExprID: maxID}
newExpr, _ := pruner.maybePrune(expr)
return &exprpb.ParsedExpr{
Expr: newExpr,
Expand Down
5 changes: 5 additions & 0 deletions interpreter/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ var testCases = []testInfo{
expr: `test in {'a': 1, 'field': [test, 3]}.field`,
out: `test in {"a": 1, "field": [test, 3]}.field`,
},
{
in: partialActivation(map[string]any{"foo": "bar"}, "r.attr"),
expr: `foo == "bar" && r.attr.loc in ["GB", "US"]`,
out: `r.attr.loc in ["GB", "US"]`,
},
// TODO: the output of an expression like this relies on either
// a) doing replacements on the original macro call, or
// b) mutating the macro call tracking data rather than the core
Expand Down

0 comments on commit 5b634ac

Please sign in to comment.