Skip to content

Commit

Permalink
Fix invalid address-of operations (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
volsa committed Dec 2, 2022
1 parent 31c536e commit 42cf27a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub enum ErrNo {
type__unknown_nature,
type__unresolved_generic,
type__incompatible_size,
type__invalid_operation,

//codegen related
codegen__general,
Expand Down Expand Up @@ -761,6 +762,14 @@ impl Diagnostic {
err_no: ErrNo::duplicate_symbol,
}
}

pub fn invalid_operation(message: &str, range: SourceRange) -> Diagnostic {
Diagnostic::SyntaxError {
message: message.to_string(),
range: vec![range],
err_no: ErrNo::type__invalid_operation,
}
}
}

/// a diagnostics severity
Expand Down
19 changes: 19 additions & 0 deletions src/validation/stmt_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ impl StatementValidator {
AstStatement::Reference { name, location, .. } => {
self.validate_reference(statement, name, location, context);
}

AstStatement::UnaryExpression {
operator,
value,
location,
..
} => {
if operator == &Operator::Address {
match value.as_ref() {
AstStatement::Reference { .. } | AstStatement::ArrayAccess { .. } => (),

_ => self.diagnostics.push(Diagnostic::invalid_operation(
"Invalid address-of operation",
location.to_owned(),
)),
}
}
}

AstStatement::CastStatement {
location,
target,
Expand Down
36 changes: 36 additions & 0 deletions src/validation/tests/statement_validation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,39 @@ fn program_call_parameter_validation() {
]
);
}

#[test]
fn address_of_operations() {
let diagnostics: Vec<Diagnostic> = parse_and_validate(
"
PROGRAM main
VAR
a: INT;
b: ARRAY[0..5] OF INT;
END_VAR
// Should work
&(a);
&b[1];
// Should not work
&&a;
&100;
&(a+3);
END_PROGRAM
",
);

assert_eq!(diagnostics.len(), 3);

let ranges = vec![(243..246), (260..264), (278..283)];
for (idx, diagnostic) in diagnostics.iter().enumerate() {
assert_eq!(
diagnostic,
&Diagnostic::invalid_operation(
"Invalid address-of operation",
ranges[idx].to_owned().into()
)
);
}
}

0 comments on commit 42cf27a

Please sign in to comment.