Skip to content

Commit

Permalink
fix return type for set in interface properties
Browse files Browse the repository at this point in the history
  • Loading branch information
abroooo committed Feb 7, 2025
1 parent 0cb1d75 commit ac734b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 81 deletions.
13 changes: 11 additions & 2 deletions src/lowering/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl PropertyLowerer {
name = property.name
);

let pou = Pou {
let mut pou = Pou {
name: name.clone(),
kind: PouType::Method {
parent: property.parent_name.clone(),
Expand All @@ -121,6 +121,13 @@ impl PropertyLowerer {
interfaces: Vec::new(),
is_const: false,
};
//
match property_impl.kind {
PropertyKind::Set => {
pou.return_type = None;
}
_ => {}
}

interface.methods.push(pou);

Expand Down Expand Up @@ -192,9 +199,11 @@ impl PropertyLowerer {
));
}

// We have to do two things when dealing with setters:
// We have to do three things when dealing with setters:
// 1. Patch a variable block of type `VAR_INPUT` with a single variable named `__in : <property_type>`
// 2. Prepend a `<property_name> := __in` assignment to the implementation
// 3. change the return type to void because the setter does not return
// anything
PropertyKind::Set => {
let parameter_name = "__in";

Expand Down
99 changes: 21 additions & 78 deletions src/parser/tests/interface_parser_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::test_utils::tests::parse;
use crate::test_utils::tests::{parse, parse_buffered};

#[test]
fn empty_interface() {
Expand Down Expand Up @@ -393,83 +393,26 @@ fn property_in_interface_must_not_have_an_implementation() {
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: 4,
column: 23,
offset: 118,
}..TextLocation {
line: 4,
column: 31,
offset: 126,
},
),
},
secondary_locations: Some(
[
SourceLocation {
span: Range(
TextLocation {
line: 2,
column: 16,
offset: 51,
}..TextLocation {
line: 2,
column: 24,
offset: 59,
},
),
},
],
),
error_code: "E117",
sub_diagnostics: [],
internal_error: None,
},
Diagnostic {
message: "Interfaces can not have a default implementation in a Property",
primary_location: SourceLocation {
span: Range(
TextLocation {
line: 7,
column: 23,
offset: 203,
}..TextLocation {
line: 7,
column: 31,
offset: 211,
},
),
},
secondary_locations: Some(
[
SourceLocation {
span: Range(
TextLocation {
line: 2,
column: 16,
offset: 51,
}..TextLocation {
line: 2,
column: 24,
offset: 59,
},
),
},
],
),
error_code: "E117",
sub_diagnostics: [],
internal_error: None,
},
]
"#);
let (_, diagnostics) = parse_buffered(source);
insta::assert_snapshot!(diagnostics, @r"
error[E117]: Interfaces can not have a default implementation in a Property
┌─ <internal>:5:24
3 │ PROPERTY foo : DINT
│ -------- see also
4 │ GET
5 │ foo := 5;
│ ^^^^^^^^ Interfaces can not have a default implementation in a Property
error[E117]: Interfaces can not have a default implementation in a Property
┌─ <internal>:8:24
3 │ PROPERTY foo : DINT
│ -------- see also
·
8 │ foo := 5;
│ ^^^^^^^^ Interfaces can not have a default implementation in a Property
");
}

#[test]
Expand Down
7 changes: 6 additions & 1 deletion src/validation/pou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ where

// Check if the parameters match; note that the order of the parameters is important due to implicit calls
let parameters_ref = ctxt.index.get_declared_parameters(method_ref.get_name());
let parameters_impl = ctxt.index.get_declared_parameters(method_impl.get_name());
let parameters_impl = ctxt
.index
.get_declared_parameters(method_impl.get_name())
.into_iter()
.filter(|par| !par.source_location.is_internal())
.collect::<Vec<_>>();

for (idx, parameter_ref) in parameters_ref.iter().enumerate() {
match parameters_impl.get(idx) {
Expand Down

0 comments on commit ac734b0

Please sign in to comment.