Skip to content

Commit

Permalink
fix: Handle aliases in scheduler (#285)
Browse files Browse the repository at this point in the history
Earlier scheduler only recognized rules and would raise an
`unsafe var` error on alias.

Register alias var names to fix this.

fixes #284

Also fix clippy warning treated as error

Signed-off-by: Anand Krishnamoorthi <[email protected]>
  • Loading branch information
anakrish authored Jul 27, 2024
1 parent 6e1f8cd commit 7095e26
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![allow(unknown_lints)]
#![allow(clippy::doc_lazy_continuation)]
// Use README.md as crate documentation.
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
// We'll default to building for no_std - use core, alloc instead of std.
Expand Down
12 changes: 9 additions & 3 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl Analyzer {
}

pub fn analyze(mut self, modules: &[Ref<Module>]) -> Result<Schedule> {
self.add_rules(modules)?;
self.add_rules_and_aliases(modules)?;
self.functions = gather_functions(modules)?;

for m in modules {
Expand All @@ -430,7 +430,7 @@ impl Analyzer {
modules: &[Ref<Module>],
query: &Ref<Query>,
) -> Result<Schedule> {
self.add_rules(modules)?;
self.add_rules_and_aliases(modules)?;
self.analyze_query(None, None, query, Scope::default())?;

Ok(Schedule {
Expand All @@ -439,7 +439,7 @@ impl Analyzer {
})
}

fn add_rules(&mut self, modules: &[Ref<Module>]) -> Result<()> {
fn add_rules_and_aliases(&mut self, modules: &[Ref<Module>]) -> Result<()> {
for m in modules {
let path = get_path_string(&m.package.refr, Some("data"))?;
let scope: &mut Scope = self.packages.entry(path).or_default();
Expand All @@ -456,6 +456,12 @@ impl Analyzer {
};
scope.unscoped.insert(var);
}

for import in &m.imports {
if let Some(var) = &import.r#as {
scope.unscoped.insert(var.source_str());
}
}
}

Ok(())
Expand Down
6 changes: 6 additions & 0 deletions tests/interpreter/cases/import/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cases:
- |
package b
import rego.v1
# Both the following imports are overridden by rules
#import data.a.b as a
#import data.a.b
Expand All @@ -43,13 +44,18 @@ cases:
a = 10
c = C + b
r if {
some v in [C]
}
query: data
want_result:
a:
b: 11
b:
a: 10
c: 22
r: true

- note: import overridden by rule
modules:
Expand Down

0 comments on commit 7095e26

Please sign in to comment.