Skip to content

Commit

Permalink
add error code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasel committed Oct 29, 2024
1 parent 5ffea13 commit 9421474
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ lazy_static! {
E106, Warning, include_str!("./error_codes/E106.md"), // VAR_EXTERNAL have no effect
E107, Error, include_str!("./error_codes/E107.md"), // Missing configuration for template variable
E108, Error, include_str!("./error_codes/E108.md"), // Template variable is configured multiple times
E109, Error, include_str!("./error_codes/E109.md"), // Stateful pointer variable initialized with temporary value
);
}

Expand Down
16 changes: 16 additions & 0 deletions compiler/plc_diagnostics/src/diagnostics/error_codes/E109.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Stateful member variable initialized with temporary reference

Stack-local variables do not yet exist at the time of initialization. Additionally, pointing to a temporary variable will lead to a dangling pointer
as soon as it goes out of scope - potential use after free.

Erroneous code example:
```
FUNCTION_BLOCK foo
VAR
a : REF_TO BOOL := REF(b);
END_VAR
VAR_TEMP
b : BOOL;
END_VAR
END_FUNCTION_BLOCK
```
11 changes: 5 additions & 6 deletions src/validation/tests/variable_validation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,24 +1288,23 @@ fn assigning_a_temp_reference_to_stateful_var_is_error() {
"#,
);

assert_snapshot!(diagnostics, @r#"
error[E001]: Cannot assign address of temporary variable to a member-variable
assert_snapshot!(diagnostics, @r###"
error[E109]: Cannot assign address of temporary variable to a member-variable
┌─ <internal>:4:40
4 │ s1: REF_TO DINT := REF(t1); // error
│ ^^ Cannot assign address of temporary variable to a member-variable
error[E001]: Cannot assign address of temporary variable to a member-variable
error[E109]: Cannot assign address of temporary variable to a member-variable
┌─ <internal>:5:23
5 │ s2 AT t1 : DINT; // error
│ ^^ Cannot assign address of temporary variable to a member-variable
error[E001]: Cannot assign address of temporary variable to a member-variable
error[E109]: Cannot assign address of temporary variable to a member-variable
┌─ <internal>:6:45
6 │ s3 : REFERENCE TO DINT REF= t1; // error
│ ^^ Cannot assign address of temporary variable to a member-variable
"#)
"###)
}
3 changes: 2 additions & 1 deletion src/validation/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ fn report_temporary_address_in_pointer_initializer<T: AnnotationMap>(

validator.diagnostics.push(
Diagnostic::new("Cannot assign address of temporary variable to a member-variable")
.with_location(location),
.with_location(location)
.with_error_code("E109"),
);
}

Expand Down

0 comments on commit 9421474

Please sign in to comment.