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

ast/parser: generate new wildcards for else #5412

Merged
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
5 changes: 5 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ func (p *Parser) parseElse(head *Head) *Rule {
rule.SetLoc(p.s.Loc())

rule.Head = head.Copy()
for i := range rule.Head.Args {
if v, ok := rule.Head.Args[i].Value.(Var); ok && v.IsWildcard() {
rule.Head.Args[i].Value = Var(p.genwildcard())
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy here had been misleading the "wildcard unmangling" logic in the formatter: https://github.com/open-policy-agent/opa/blob/main/format/format.go#L168

It picked up the else's invisible args and counted them as a second use that would necessitate the unmangling. It really doesn't, though, and is best sidestepped by introducing fresh wildcard variables for the else args in the first place.

rule.Head.SetLoc(p.s.Loc())

defer func() {
Expand Down
27 changes: 27 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,30 @@ func TestRule(t *testing.T) {
})
assertParseError(t, "invalid rule body no separator", `p { a = "foo"bar }`)
assertParseError(t, "invalid rule body no newline", `p { a b c }`)

assertParseRule(t, "wildcard in else args", `f(_) { true } else := false`, &Rule{
Head: &Head{
Name: "f",
Reference: Ref{VarTerm("f")},
Args: Args{
VarTerm("$0"),
},
Value: BooleanTerm(true),
},
Body: MustParseBody(`true`),
Else: &Rule{
Head: &Head{
Name: "f",
Assign: true,
Reference: Ref{VarTerm("f")},
Args: Args{
VarTerm("$1"),
},
Value: BooleanTerm(false),
},
Body: MustParseBody(`true`),
},
})
}

func TestRuleContains(t *testing.T) {
Expand Down Expand Up @@ -4830,6 +4854,9 @@ func assertParseRule(t *testing.T, msg string, input string, correct *Rule, opts
if !rule.Head.Ref().Equal(correct.Head.Ref()) {
t.Errorf("Error on test \"%s\": rule heads not equal: ref = %v (parsed), ref = %v (correct)", msg, rule.Head.Ref(), correct.Head.Ref())
}
if !rule.Head.Equal(correct.Head) {
t.Errorf("Error on test \"%s\": rule heads not equal: %v (parsed), %v (correct)", msg, rule.Head, correct.Head)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just made the comparison a little more helpful. It's not much, but anyways.

if !rule.Equal(correct) {
t.Errorf("Error on test \"%s\": rules not equal: %v (parsed), %v (correct)", msg, rule, correct)
}
Expand Down