Skip to content

Commit

Permalink
fix: don't produce additional errors when a module is banned.
Browse files Browse the repository at this point in the history
When a module is banned, an `import` statement importing the banned module causes an error, however we don't want additional errors when the module is used in the condition of some rule.
  • Loading branch information
plusvic committed Sep 27, 2024
1 parent c1d2021 commit a890b61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,18 +1432,6 @@ impl<'a> Compiler<'a> {
// Yes, module exists.
let module = module.unwrap();

// Is the module banned? If yes, produce an error.
if let Some((error_title, error_msg)) =
self.banned_modules.get(module_name)
{
return Err(CustomError::build(
&self.report_builder,
error_title.clone(),
error_msg.clone(),
import.span().into(),
));
}

// If the module has not been added to `self.root_struct` and
// `self.imported_modules`, do it.
if !self.root_struct.has_field(module_name) {
Expand Down Expand Up @@ -1504,6 +1492,21 @@ impl<'a> Compiler<'a> {
);
}

// Is the module banned? If yes, produce an error. Notice however that
// this check is done after the module has been added to the symbol
// table because we don't want additional errors due to undefined
// identifiers when the banned module is used in some rule condition.
if let Some((error_title, error_msg)) =
self.banned_modules.get(module_name)
{
return Err(CustomError::build(
&self.report_builder,
error_title.clone(),
error_msg.clone(),
import.span().into(),
));
}

Ok(())
}

Expand Down
6 changes: 6 additions & 0 deletions lib/src/compiler/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ fn banned_modules() {
.add_source(
r#"
import "test_proto2"
rule test { condition: test_proto2.int32_zero == 0}
"#,
)
.expect_err("expected error")
Expand All @@ -606,6 +607,11 @@ fn banned_modules() {
| ^^^^^^^^^^^^^^^^^^^^ module `test_proto2` is used here
|"#
);

// The only error should be the error about the use of a banned module,
// the condition `test_proto2.int32_zero == 0` should not produce any
// error.
assert_eq!(compiler.errors().len(), 1);
}

#[cfg(feature = "test_proto2-module")]
Expand Down

0 comments on commit a890b61

Please sign in to comment.