Skip to content

Commit

Permalink
default implementation not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
abroooo committed Feb 6, 2025
1 parent 84d945d commit 2da769a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ lazy_static! {
E114, Error, include_str!("./error_codes/E114.md"), // Property in unsupported POU type
E115, Error, include_str!("./error_codes/E115.md"), // Property defined in non-supported variable block
E116, Error, include_str!("./error_codes/E116.md"), // Property with invalid number of GET and/or SET blocks
E117, Warning, include_str!("./error_codes/E117.md"), // Interface default Property implementation
);
}

Expand Down
18 changes: 0 additions & 18 deletions src/lowering/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,24 +424,6 @@ mod tests {
unit
}

#[test]
// TODO this code should fail because get/set in interaces
// are not allowed to have an implementation
fn property_in_interface_must_not_have_an_implementation() {
let source = r"
INTERFACE myInterface
PROPERTY foo : DINT
GET
foo := 5;
END_GET
END_PROPERTY
END_INTERFACE
";

let unit = lower(source);
insta::assert_debug_snapshot!(unit.implementations, @"[]");
}

#[test]
fn properties_are_used_within_each_other() {
let source = r"
Expand Down
9 changes: 8 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn parse_interface(lexer: &mut ParseSession) -> (Interface, Vec<Implementation>)
// This is temporary? At some point we'll support them but for now it's a diagnostic
if !imp.statements.is_empty() {
lexer.accept_diagnostic(
Diagnostic::new("Interfaces can not have a default implementations")
Diagnostic::new("Interfaces can not have a default implementations in a Method")
.with_error_code("E113")
.with_location(&imp.location),
);
Expand All @@ -212,6 +212,13 @@ fn parse_interface(lexer: &mut ParseSession) -> (Interface, Vec<Implementation>)

KeywordProperty => {
if let Some(prop) = parse_property(lexer, &name, &location_name, &PouType::FunctionBlock) {
if !prop.implementations.iter().all(|statement| statement.body.is_empty()) {
lexer.accept_diagnostic(
Diagnostic::new("Interfaces can not have a default implementation in a Property")
.with_error_code("E117")
.with_location(prop.name_location.clone()),
);
}
properties.push(prop);
}
}
Expand Down
52 changes: 47 additions & 5 deletions src/parser/tests/interface_parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,49 @@ fn pou_implementing_multiple_interfaces() {
"###);
}

#[test]
// TODO: make the error nicer (error per occurence and better location?)
fn property_in_interface_must_not_have_an_implementation() {
let source = r"
INTERFACE myInterface
PROPERTY foo : DINT
GET
foo := 5;
END_GET
SET
foo := 5;
END_SET
END_PROPERTY
END_INTERFACE
";

let (unit, diagnostics) = parse(source);
insta::assert_debug_snapshot!(diagnostics, @r#"
[
Diagnostic {
message: "Interfaces can not have a default implementation in a Property",
primary_location: SourceLocation {
span: Range(
TextLocation {
line: 2,
column: 25,
offset: 60,
}..TextLocation {
line: 2,
column: 28,
offset: 63,
},
),
},
secondary_locations: None,
error_code: "E117",
sub_diagnostics: [],
internal_error: None,
},
]
"#);
}

#[test]
fn property_inside_interface() {
let source = r#"
Expand Down Expand Up @@ -517,15 +560,14 @@ mod error_handling {
";

let diagnostics = parse_and_validate_buffered(source);
insta::assert_snapshot!(diagnostics, @r###"
warning[E113]: Interfaces can not have a default implementations
insta::assert_snapshot!(diagnostics, @r"
warning[E113]: Interfaces can not have a default implementations in a Method
┌─ <internal>:4:17
4 │ ╭ 1 > 2;
5 │ │ methodA := 5;
│ ╰─────────────────────────────^ Interfaces can not have a default implementations
"###);
│ ╰─────────────────────────────^ Interfaces can not have a default implementations in a Method
");
}

#[test]
Expand Down

0 comments on commit 2da769a

Please sign in to comment.