Skip to content

Commit 9b10ef7

Browse files
authored
Merge pull request #1433 from PLC-lang/recursion_fix
fix: recursive type check no longer fails on duplicate fields
2 parents 26d7d0d + c7b02d3 commit 9b10ef7

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/index/tests/index_tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,3 +2256,36 @@ fn inheritance_chain_correctly_finds_parents() {
22562256
let inheritance_chain = index.get_inheritance_chain("grandparent", "child");
22572257
assert_eq!(inheritance_chain, Vec::<&PouIndexEntry>::new());
22582258
}
2259+
2260+
#[test]
2261+
fn pou_with_two_types_not_consireded_recursive() {
2262+
let (_, index) = index(
2263+
"
2264+
FUNCTION_BLOCK fb
2265+
VAR x,y : DINT; END_VAR
2266+
END_FUNCTION_BLOCK
2267+
PROGRAM p
2268+
VAR
2269+
x : fb;
2270+
y : fb;
2271+
END_VAR
2272+
END_PROGRAM",
2273+
);
2274+
2275+
let pou_type = index.find_pou_type("p").unwrap();
2276+
assert_eq!(pou_type.get_type_information().get_size(&index).unwrap().bits(), 128);
2277+
}
2278+
2279+
#[test]
2280+
fn pou_with_recursive_type_fails() {
2281+
let (_, index) = index(
2282+
"
2283+
FUNCTION_BLOCK fb
2284+
VAR x : fb; END_VAR
2285+
END_FUNCTION_BLOCK
2286+
",
2287+
);
2288+
2289+
let pou_type = index.find_pou_type("fb").unwrap();
2290+
assert!(pou_type.get_type_information().get_size(&index).is_err());
2291+
}

src/typesystem.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl DataTypeInformation {
631631
if self.is_struct() && !seen.insert(self.get_name()) {
632632
return Err(anyhow!("Recursive type detected: {}", self.get_name()));
633633
}
634-
match self {
634+
let res = match self {
635635
DataTypeInformation::Integer { size, .. } => Ok(Bytes::from_bits(*size)),
636636
DataTypeInformation::Float { size, .. } => Ok(Bytes::from_bits(*size)),
637637
DataTypeInformation::String { size, encoding } => Ok(size
@@ -668,7 +668,9 @@ impl DataTypeInformation {
668668
.map(|it| it.get_size(index))
669669
.unwrap_or_else(|| Ok(Bytes::from_bits(DINT_SIZE))),
670670
DataTypeInformation::Generic { .. } | DataTypeInformation::Void => Ok(Bytes::from_bits(0)),
671-
}
671+
};
672+
seen.remove(self.get_name());
673+
res
672674
}
673675

674676
/// Returns the String encoding's alignment (character)

0 commit comments

Comments
 (0)