Skip to content
Draft
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
4 changes: 0 additions & 4 deletions cmd/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,6 @@ query:1 %.*% | Index data.test.p (matched 1 rule, early exit) {}
%.*%/test.rego:12 | | Eval x = data.test.v {}
%.*%/test.rego:12 | | Index data.test.v (matched 1 rule, early exit) {}
%.*%/test.rego:4 | | Enter data.test.v {}
%.*%/test.rego:4 | | | Eval true {}
%.*%/test.rego:4 | | | Exit data.test.v early {}
%.*%/test.rego:14 | | Eval x.foo[_] = "a" {x: {"bar": ["a", "b", "c", "d", ...}
%.*%/test.rego:11 | | Exit data.test.p early {}
Expand All @@ -1764,7 +1763,6 @@ query:1 %.*% | Redo data.test.p = _ {_:
%.*%/test.rego:11 | Redo data.test.p {}
%.*%/test.rego:14 | | Redo x.foo[_] = "a" {_: 0, x: {"bar": ["a", "b", "c", "d", ...}
%.*%/test.rego:12 | | Redo x = data.test.v {data.test.v: {"bar": ["a", "b", "c", "d", ..., x: {"bar": ["a", "b", "c", "d", ...}
%.*%/test.rego:4 | | | Redo true {}
true
`,
},
Expand Down Expand Up @@ -1911,7 +1909,6 @@ query:1 %.*% | Index data.test.p (matched 1 rule, early exit) {}
%.*%/test.rego:7 | | Eval __local2__ = data.test.a {}
%.*%/test.rego:7 | | Index data.test.a (matched 1 rule, early exit) {}
%.*%/test.rego:4 | | Enter data.test.a {}
%.*%/test.rego:4 | | | Eval true {}
%.*%/test.rego:4 | | | Exit data.test.a early {}
%.*%/test.rego:7 | | Eval plus(__local2__, 1, __local0__) {__local2__: 1}
%.*%/test.rego:7 | | Eval __local0__ = 2 {__local0__: 2}
Expand All @@ -1930,7 +1927,6 @@ query:1 %.*% | Redo data.test.p = _ {_:
%.*%/test.rego:7 | | Redo __local0__ = 2 {__local0__: 2}
%.*%/test.rego:7 | | Redo plus(__local2__, 1, __local0__) {__local0__: 2, __local2__: 1}
%.*%/test.rego:7 | | Redo __local2__ = data.test.a {__local2__: 1, data.test.a: 1}
%.*%/test.rego:4 | | | Redo true {}
true
`,
},
Expand Down
89 changes: 5 additions & 84 deletions cmd/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func TestParseExit0(t *testing.T) {
p
1
body
expr index=0
true
`

if got, want := string(stdout), expectedOutput; got != want {
Expand All @@ -62,7 +60,6 @@ func TestParseExit1(t *testing.T) {
}

func TestParseJSONOutput(t *testing.T) {

files := map[string]string{
"x.rego": `package x

Expand Down Expand Up @@ -94,15 +91,7 @@ func TestParseJSONOutput(t *testing.T) {
},
"rules": [
{
"body": [
{
"index": 0,
"terms": {
"type": "boolean",
"value": true
}
}
],
"body": [],
"head": {
"name": "p",
"value": {
Expand Down Expand Up @@ -178,27 +167,7 @@ p = 1
},
"rules": [
{
"body": [
{
"index": 0,
"location": {
"file": "TEMPDIR/x.rego",
"row": 3,
"col": 5,
"text": "MQ=="
},
"terms": {
"location": {
"file": "TEMPDIR/x.rego",
"row": 3,
"col": 5,
"text": "MQ=="
},
"type": "boolean",
"value": true
}
}
],
"body": [],
"head": {
"name": "p",
"value": {
Expand Down Expand Up @@ -665,15 +634,7 @@ func TestParseRefsJSONOutput(t *testing.T) {
},
"rules": [
{
"body": [
{
"index": 0,
"terms": {
"type": "boolean",
"value": true
}
}
],
"body": [],
"head": {
"value": {
"type": "boolean",
Expand Down Expand Up @@ -757,27 +718,7 @@ a.b.c := true
},
"rules": [
{
"body": [
{
"index": 0,
"location": {
"file": "TEMPDIR/x.rego",
"row": 3,
"col": 10,
"text": "dHJ1ZQ=="
},
"terms": {
"location": {
"file": "TEMPDIR/x.rego",
"row": 3,
"col": 10,
"text": "dHJ1ZQ=="
},
"type": "boolean",
"value": true
}
}
],
"body": [],
"head": {
"value": {
"location": {
Expand Down Expand Up @@ -956,27 +897,7 @@ allow = true if {
],
"rules": [
{
"body": [
{
"index": 0,
"location": {
"file": "TEMPDIR/x.rego",
"row": 4,
"col": 1,
"text": "ZGVmYXVsdA=="
},
"terms": {
"location": {
"file": "TEMPDIR/x.rego",
"row": 4,
"col": 1,
"text": "ZGVmYXVsdA=="
},
"type": "boolean",
"value": true
}
}
],
"body": [],
"default": true,
"head": {
"name": "allow",
Expand Down
28 changes: 14 additions & 14 deletions v1/ast/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6066,9 +6066,14 @@ func rewriteExprTermsInHead(gen *localVarGenerator, rule *Rule) {

// isEmptyBody true for a rule like `pi := 3.14 if { true}`
func isEmptyBody(body Body) bool {
if len(body) == 1 {
if term, ok := body[0].Terms.(*Term); ok {
return Boolean(true).Equal(term.Value)
n := len(body)
if n == 0 {
return true
}
if n == 1 {
expr := body[0]
if term, ok := expr.Terms.(*Term); ok {
return !expr.Negated && expr.With == nil && Boolean(true).Equal(term.Value)
}
}

Expand Down Expand Up @@ -6097,14 +6102,16 @@ func appendToBody(body Body, exprs ...*Expr) Body {
if len(exprs) == 0 {
return body
}

blen := len(body)
if blen == 1 && isEmptyBody(body) {
// body will no longer be empty, so instead of appending,
// replace the 'true' expression with the new expression.
if blen == 0 {
// body is empty, so just append the new expressions.
} else if isEmptyBody(body) {
// lone 'true' expression — instead of appending,
// replace it with the new expression.
exprs[0].Index = 0
body[0], exprs = exprs[0], exprs[1:]
}

for i, expr := range exprs {
expr.Index = blen + i
}
Expand Down Expand Up @@ -6498,13 +6505,6 @@ func rewriteDeclaredVarsInBody(g *localVarGenerator, stack *localDeclaredVars, u
}
}

// If the body only contained a var statement it will be empty at this
// point. Append true to the body to ensure that it's non-empty (zero length
// bodies are not supported.)
if len(cpy) == 0 {
cpy.Append(NewExpr(BooleanTerm(true)))
}

errs = checkUnusedAssignedVars(body, stack, used, errs, strict)
return cpy, checkUnusedDeclaredVars(body, stack, used, cpy, errs)
}
Expand Down
3 changes: 0 additions & 3 deletions v1/ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5103,7 +5103,6 @@ p = [x | x := rego.metadata.chain()]`,

p = [__local0__ | __local1__ = __local2__; __local0__ = __local1__] if {
__local2__ = [{"path": ["test", "p"]}]
true
}`,
},
{
Expand Down Expand Up @@ -5131,7 +5130,6 @@ p = {x | x := rego.metadata.chain()}`,

p = {__local0__ | __local1__ = __local2__; __local0__ = __local1__} if {
__local2__ = [{"path": ["test", "p"]}]
true
}`,
},
{
Expand Down Expand Up @@ -5159,7 +5157,6 @@ p = {i: x | x := rego.metadata.chain()[i]}`,

p = {i: __local0__ | __local1__ = __local2__; __local0__ = __local1__[i]} if {
__local2__ = [{"path": ["test", "p"]}]
true
}`,
},
{
Expand Down
21 changes: 8 additions & 13 deletions v1/ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,10 @@ func (p *Parser) parseRules() []*Rule {
return nil
}

if len(rule.Head.Args) > 0 {
if !p.validateDefaultRuleArgs(&rule) {
return nil
}
if len(rule.Head.Args) > 0 && !p.validateDefaultRuleArgs(&rule) {
return nil
}

rule.Body = NewBody(NewExpr(BooleanTerm(true).SetLocation(rule.Location)).SetLocation(rule.Location))
return []*Rule{&rule}
}

Expand Down Expand Up @@ -925,10 +922,7 @@ func (p *Parser) parseRules() []*Rule {
p.scan()

case usesContains:
rule.Body = NewBody(NewExpr(BooleanTerm(true).SetLocation(rule.Location)).SetLocation(rule.Location))
rule.generatedBody = true
rule.Location = rule.Head.Location

return []*Rule{&rule}

default:
Expand Down Expand Up @@ -958,7 +952,6 @@ func (p *Parser) parseRules() []*Rule {
rules := []*Rule{&rule}

for p.s.tok == tokens.LBrace {

if rule.Else != nil {
p.error(p.s.Loc(), "expected else keyword")
return nil
Expand Down Expand Up @@ -1178,20 +1171,22 @@ func (p *Parser) parseBody(end tokens.Token) Body {
}

func (p *Parser) parseQuery(requireSemi bool, end tokens.Token) Body {
body := Body{}

if p.s.tok == end {
p.error(p.s.Loc(), "found empty body")
return nil
}

var body Body
for {
expr := p.parseLiteral()
if expr == nil {
return nil
}

body.Append(expr)
if body == nil {
body = NewBody(expr)
} else {
body.Append(expr)
}

if p.s.tok == tokens.Semicolon {
p.scan()
Expand Down
Loading
Loading