Skip to content

Commit

Permalink
fix(validation): fix false-positive unresolved generic symbol validat…
Browse files Browse the repository at this point in the history
…ion for formal parameters (PLC-lang#1066)

* fix(validation): fix false-positive unresolved generic symbol validation for formal parameters

* remove unreferenced snapshots
  • Loading branch information
mhasel authored Feb 2, 2024
1 parent 7e2e61a commit 6111391
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/validation/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,10 @@ fn validate_type_nature<T: AnnotationMap>(
{
if let DataTypeInformation::Generic { generic_symbol, nature, .. } = type_hint.get_type_information()
{
// we might be validating an identifier of a formal parameter assignment (FOO(x := 0))
if let AstStatement::Identifier(_) = statement.get_stmt() {
return;
}
validator.push_diagnostic(
Diagnostic::error(format!("Could not resolve generic type {generic_symbol} with {nature}"))
.with_error_code("E064")
Expand Down
30 changes: 30 additions & 0 deletions src/validation/tests/generic_validation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,3 +1599,33 @@ fn any_date_multiple_parameters() {
let diagnostics = parse_and_validate_buffered(src);
assert_snapshot!(&diagnostics);
}

#[test]
fn generic_call_with_formal_parameter() {
let src = "
FUNCTION FOO < T: ANY_NUM >: T
VAR_INPUT
x: T;
END_VAR
END_FUNCTION
FUNCTION FOO__DINT: DINT
VAR_INPUT
x: DINT;
END_VAR
FOO__DINT := x + 0;
END_FUNCTION
FUNCTION main: DINT
VAR
myLocalNumber: DINT := 2;
END_VAR
myLocalNumber := FOO(x := myLocalNumber); // okay
myLocalNumber := FOO(y := 0); // unresolved reference
myLocalNumber := FOO(x := 'INVALID TYPE NATURE'); // invalid type nature
END_FUNCTION
";

let diagnostics = parse_and_validate_buffered(src);
insta::assert_snapshot!(diagnostics);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: src/validation/tests/generic_validation_tests.rs
expression: diagnostics
---
error: Invalid call parameters
┌─ <internal>:20:30
20 │ myLocalNumber := FOO(y := 0); // unresolved reference
│ ^^^^^^ Invalid call parameters

error: Could not resolve reference to y
┌─ <internal>:20:30
20 │ myLocalNumber := FOO(y := 0); // unresolved reference
│ ^ Could not resolve reference to y

error: Invalid type nature for generic argument. __STRING_19 is no ANY_NUMBER
┌─ <internal>:21:35
21 │ myLocalNumber := FOO(x := 'INVALID TYPE NATURE'); // invalid type nature
│ ^^^^^^^^^^^^^^^^^^^^^ Invalid type nature for generic argument. __STRING_19 is no ANY_NUMBER

error: Invalid assignment: cannot assign 'STRING' to 'USINT'
┌─ <internal>:21:30
21 │ myLocalNumber := FOO(x := 'INVALID TYPE NATURE'); // invalid type nature
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid assignment: cannot assign 'STRING' to 'USINT'


0 comments on commit 6111391

Please sign in to comment.