Skip to content

Commit

Permalink
feat: improve error message about missing identifier when the cause i…
Browse files Browse the repository at this point in the history
…s a missing import statement

When the missing identifier matches a module name, a note is added to the error message highlighting the fact that the import statement is missing.
Example:

```text
            r#"error: unknown identifier `test_proto2`
   ╭─[line:4:5]
   │
 4 │     test_proto2.foo
   │     ─────┬─────
   │          ╰─────── this identifier has not been declared
   │
   │ Note: there is a module named `test_proto2`, but the `import "test_proto2"` statement is missing
───╯
```
  • Loading branch information
plusvic committed Nov 18, 2023
1 parent 08f7080 commit be7ca4a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions yara-x/src/compiler/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ pub enum CompileErrorInfo {

#[error("unknown identifier `{identifier}`")]
#[label("this identifier has not been declared", span)]
#[note(note)]
UnknownIdentifier {
detailed_report: String,
identifier: String,
span: Span,
note: Option<String>,
},

#[error("unknown module `{identifier}`")]
Expand Down
14 changes: 14 additions & 0 deletions yara-x/src/compiler/ir/ast2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::compiler::ir::{
use crate::compiler::{
CompileContext, CompileError, CompileErrorInfo, PatternId,
};
use crate::modules::BUILTIN_MODULES;
use crate::re;
use crate::re::parser::Error;
use crate::symbols::{Symbol, SymbolKind, SymbolLookup, SymbolTable};
Expand Down Expand Up @@ -342,6 +343,19 @@ pub(in crate::compiler) fn expr_from_ast(
ctx.report_builder,
ident.name.to_string(),
ident.span(),
// Add a note about the missing import statement if
// the unknown identifier is a module name.
if current_struct.is_none()
&& BUILTIN_MODULES.contains_key(ident.name)
{
Some(format!(
"there is a module named `{}`, but the `import \"{}\"` statement is missing",
ident.name,
ident.name
))
} else {
None
},
),
));
}
Expand Down
21 changes: 21 additions & 0 deletions yara-x/src/compiler/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,27 @@ rule test {
│ ────────┬───────
│ ╰───────── this identifier has not been declared
───╯
"#,
),
////////////////////////////////////////////////////////////
#[cfg(feature = "test_proto2-module")]
(
line!(),
r#"
rule test {
condition:
test_proto2.foo
}
"#,
r#"error: unknown identifier `test_proto2`
╭─[line:4:5]
4 │ test_proto2.foo
│ ─────┬─────
│ ╰─────── this identifier has not been declared
│ Note: there is a module named `test_proto2`, but the `import "test_proto2"` statement is missing
───╯
"#,
),
////////////////////////////////////////////////////////////
Expand Down

0 comments on commit be7ca4a

Please sign in to comment.