Skip to content

Commit

Permalink
fix: Handle aliases in scheduler
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

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
  • Loading branch information
anakrish committed Jul 27, 2024
1 parent 6e1f8cd commit 017a391
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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 017a391

Please sign in to comment.