Skip to content

Commit 2039fea

Browse files
committed
convert correctness tests to lit
1 parent a991a8c commit 2039fea

7 files changed

+67
-116
lines changed

src/validation/statement.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,12 @@ fn validate_ref_assignment<T: AnnotationMap>(
787787
}
788788

789789
// Lastly, assert the type the lhs references matches with the rhs
790-
let type_lhs = context.annotations.get_type(&assignment.left, context.index).unwrap();
791-
let type_rhs = context.annotations.get_type(&assignment.right, context.index).unwrap();
790+
let type_lhs = context.annotations.get_type_or_void(&assignment.left, context.index);
791+
let type_rhs = context.annotations.get_type_or_void(&assignment.right, context.index);
792+
let type_info_lhs = context.index.find_elementary_pointer_type(type_lhs.get_type_information());
793+
let type_info_rhs = context.index.find_elementary_pointer_type(type_rhs.get_type_information());
792794

793-
if type_lhs != type_rhs {
795+
if type_info_lhs != type_info_rhs {
794796
validator.push_diagnostic(
795797
Diagnostic::new(format!(
796798
"Invalid assignment, types {} and {} differ",

src/validation/tests/assignment_validation_tests.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,11 +1246,11 @@ fn reference_to_variables_and_ref_assignments() {
12461246
referenceToFooInitializedArray : REFERENCE TO ARRAY[1..5] OF DINT;
12471247
END_VAR
12481248
1249-
referenceToFoo REF= foo;
1249+
refToFoo REF= foo;
1250+
referenceToFoo REF= foo;
12501251
12511252
// Invalid
12521253
foo REF= foo;
1253-
refToFoo REF= foo;
12541254
referenceToFoo REF= 0;
12551255
referenceToFoo REF= referenceToFoo;
12561256
END_FUNCTION
@@ -1293,12 +1293,6 @@ fn reference_to_variables_and_ref_assignments() {
12931293
│ │
12941294
│ REFERENCE TO variables can not reference arrays, pointers or bits
12951295
1296-
error[E098]: Invalid assignment, types REF_TO DINT and DINT differ
1297-
┌─ <internal>:30:13
1298-
1299-
30 │ refToFoo REF= foo;
1300-
│ ^^^^^^^^^^^^^^^^^ Invalid assignment, types REF_TO DINT and DINT differ
1301-
13021296
error[E098]: Invalid assignment, expected a reference
13031297
┌─ <internal>:31:33
13041298

tests/correctness/pointers.rs

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -242,108 +242,3 @@ fn value_behind_function_block_pointer_is_assigned_to_correctly() {
242242
assert!(!maintype.a);
243243
assert!(maintype.b);
244244
}
245-
246-
#[test]
247-
fn reference_assignment() {
248-
let function = r"
249-
FUNCTION main : DINT
250-
VAR
251-
a : REF_TO DINT;
252-
b : DINT := 5;
253-
END_VAR
254-
255-
a REF= b;
256-
main := a^;
257-
END_FUNCTION
258-
";
259-
260-
let res: i32 = compile_and_run(function.to_string(), &mut MainType::default());
261-
assert_eq!(5, res);
262-
}
263-
264-
#[test]
265-
fn reference_to_assignment() {
266-
let function = r"
267-
FUNCTION main : DINT
268-
VAR
269-
a : REFERENCE TO DINT;
270-
b : DINT := 5;
271-
END_VAR
272-
a REF= b;
273-
main := a;
274-
END_FUNCTION
275-
";
276-
277-
let res: i32 = compile_and_run(function, &mut MainType::default());
278-
assert_eq!(5, res);
279-
}
280-
281-
#[test]
282-
fn reference_to_variable_referencing_other_reference_to_variable() {
283-
let function = r"
284-
FUNCTION main : DINT
285-
VAR
286-
foo : REFERENCE TO DINT;
287-
bar : REFERENCE TO DINT;
288-
qux : DINT;
289-
END_VAR
290-
291-
bar REF= qux;
292-
foo REF= bar;
293-
qux := 5;
294-
295-
main := foo; // foo -> bar -> qux
296-
END_FUNCTION
297-
";
298-
299-
let res: i32 = compile_and_run(function, &mut MainType::default());
300-
assert_eq!(5, res);
301-
}
302-
303-
#[test]
304-
fn reference_to_variable_referencing_itself() {
305-
let function = r"
306-
FUNCTION main : DINT
307-
VAR
308-
foo : REFERENCE TO DINT;
309-
bar : REFERENCE TO DINT;
310-
qux : DINT;
311-
END_VAR
312-
313-
foo REF= bar;
314-
bar REF= qux;
315-
316-
bar REF= bar;
317-
qux := 5;
318-
319-
main := bar; // bar (-> bar) -> qux
320-
END_FUNCTION
321-
";
322-
323-
let res: i32 = compile_and_run(function, &mut MainType::default());
324-
assert_eq!(5, res);
325-
}
326-
327-
#[test]
328-
fn reference_to_variable_referencing_struct() {
329-
let function = r"
330-
TYPE Transaction : STRUCT
331-
id : DINT;
332-
amount : DINT;
333-
message : STRING;
334-
END_STRUCT END_TYPE
335-
336-
FUNCTION main : DINT
337-
VAR
338-
txn : Transaction := (id := 1, amount := 5, message := 'whats up');
339-
refTxn : REFERENCE TO Transaction;
340-
END_VAR
341-
342-
refTxn REF= txn;
343-
main := refTxn.amount;
344-
END_FUNCTION
345-
";
346-
347-
let res: i32 = compile_and_run(function, &mut MainType::default());
348-
assert_eq!(5, res);
349-
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
2+
// CHECK: 5
3+
FUNCTION main : DINT
4+
VAR
5+
a : REF_TO DINT;
6+
b : DINT := 5;
7+
END_VAR
8+
a REF= b;
9+
10+
printf('%d$N', a^);
11+
END_FUNCTION
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
2+
// CHECK: 5
3+
FUNCTION main : DINT
4+
VAR
5+
foo : REFERENCE TO DINT;
6+
bar : REFERENCE TO DINT;
7+
qux : DINT;
8+
END_VAR
9+
10+
foo REF= bar;
11+
bar REF= qux;
12+
13+
bar REF= bar;
14+
qux := 5;
15+
16+
printf('%d$N', bar); // bar (-> bar) -> qux
17+
END_FUNCTION
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
2+
// CHECK: 5
3+
FUNCTION main : DINT
4+
VAR
5+
foo : REFERENCE TO DINT;
6+
bar : REFERENCE TO DINT;
7+
qux : DINT;
8+
END_VAR
9+
10+
bar REF= qux;
11+
foo REF= bar;
12+
qux := 5;
13+
14+
printf('%d$N', foo); // foo -> bar -> qux
15+
END_FUNCTION
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
2+
// CHECK: 5
3+
TYPE Transaction : STRUCT
4+
id : DINT;
5+
amount : DINT;
6+
message : STRING;
7+
END_STRUCT END_TYPE
8+
9+
FUNCTION main : DINT
10+
VAR
11+
txn : Transaction := (id := 1, amount := 5, message := 'whats up');
12+
refTxn : REFERENCE TO Transaction;
13+
END_VAR
14+
15+
refTxn REF= txn;
16+
printf('%d$N', refTxn.amount);
17+
END_FUNCTION

0 commit comments

Comments
 (0)