Skip to content

Commit

Permalink
Updating all vars in rule ref
Browse files Browse the repository at this point in the history
Fixing issue where strict-mode would only see the last var in a rule head's ref as used,
if that var is assigned in the rule body but only used in the ref.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
  • Loading branch information
johanfylling authored and ashutosh-narkar committed Sep 25, 2023
1 parent b07e60a commit 276702f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ast/compile.go
Expand Up @@ -2385,8 +2385,9 @@ func (c *Compiler) rewriteLocalVarsInRule(rule *Rule, unusedArgs VarSet, argsSta
// Rewrite assignments in body.
used := NewVarSet()

last := rule.Head.Ref()[len(rule.Head.Ref())-1]
used.Update(last.Vars())
for _, t := range rule.Head.Ref()[1:] {
used.Update(t.Vars())
}

if rule.Head.Key != nil {
used.Update(rule.Head.Key.Vars())
Expand Down
25 changes: 25 additions & 0 deletions ast/compile_test.go
Expand Up @@ -6861,6 +6861,8 @@ func TestCompilerMockVirtualDocumentPartially(t *testing.T) {
}

func TestCompilerCheckUnusedAssignedVar(t *testing.T) {
t.Setenv("EXPERIMENTAL_GENERAL_RULE_REFS", "true")

type testCase struct {
note string
module string
Expand Down Expand Up @@ -7278,6 +7280,29 @@ func TestCompilerCheckUnusedAssignedVar(t *testing.T) {
&Error{Message: "assigned var y unused"},
},
},
{
note: "general ref in rule head",
module: `package test
p[q].r[s] := 1 {
q := "foo"
s := "bar"
t := "baz"
}
`,
expectedErrors: Errors{
&Error{Message: "assigned var t unused"},
},
},
{
note: "general ref in rule head (no errors)",
module: `package test
p[q].r[s] := 1 {
q := "foo"
s := "bar"
}
`,
expectedErrors: Errors{},
},
}

makeTestRunner := func(tc testCase, strict bool) func(t *testing.T) {
Expand Down

0 comments on commit 276702f

Please sign in to comment.