Skip to content

Commit 867e5d9

Browse files
authored
feat: add additional pointer and type validations (#1341)
* fix: stack-overflow on pointer-validation * feat: add additional pointer and type validations
1 parent 7a50845 commit 867e5d9

14 files changed

+216
-47
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# Unknown type
2+
3+
Use of undeclared type-identifier.

src/index.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,9 @@ impl Index {
16281628
if let DataTypeInformation::Pointer { inner_type_name, .. } = initial_type {
16291629
if let Some(ty) = self.find_effective_type_info(inner_type_name) {
16301630
return self.find_elementary_pointer_type(self.find_intrinsic_type(ty));
1631+
} else {
1632+
// the inner type can't be found, return VOID as placeholder
1633+
return self.get_void_type().get_type_information();
16311634
}
16321635
}
16331636

src/index/const_expressions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl ConstExpression {
8888
#[derive(Debug, PartialEq, Clone)]
8989
pub struct InitData {
9090
pub initializer: Option<AstNode>,
91-
pub target_type_name: String,
91+
pub target_type_name: Option<String>,
9292
pub scope: Option<String>,
9393
pub lhs: Option<String>,
9494
}
@@ -102,7 +102,7 @@ impl InitData {
102102
) -> Self {
103103
InitData {
104104
initializer: initializer.cloned(),
105-
target_type_name: target_type.unwrap_or_default().to_string(),
105+
target_type_name: target_type.map(|it| it.into()),
106106
scope: scope.map(|it| it.into()),
107107
lhs: target.map(|it| it.into()),
108108
}

src/lowering/initializers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'lwr> Init<'lwr> for Initializers {
5858
.for_each(|(scope, data)| {
5959
assignments.maybe_insert_initializer(
6060
&scope,
61-
Some(data.lhs.as_ref().unwrap_or(&data.target_type_name)),
61+
data.lhs.as_ref().or(data.target_type_name.as_ref()).map(|it| it.as_str()),
6262
&data.initializer,
6363
);
6464
});

src/parser.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ fn parse_pointer_definition(
746746
(
747747
DataTypeDeclaration::DataTypeDefinition {
748748
data_type: DataType::PointerType { name, referenced_type: Box::new(decl), auto_deref },
749+
// FIXME: this currently includes the initializer in the sourcelocation, resulting in 'REF_TO A := B' when creating a slice
749750
location: lexer.source_range_factory.create_range(start_pos..lexer.last_range.end),
750751
scope: lexer.scope.clone(),
751752
},
@@ -772,14 +773,15 @@ fn parse_type_reference_type_definition(
772773
None
773774
};
774775

775-
let initial_value =
776+
let end = lexer.last_range.end;
777+
778+
let initial_value: Option<AstNode> =
776779
if lexer.try_consume(&KeywordAssignment) || lexer.try_consume(&KeywordReferenceAssignment) {
777780
Some(parse_expression(lexer))
778781
} else {
779782
None
780783
};
781784

782-
let end = lexer.last_range.end;
783785
if name.is_some() || bounds.is_some() {
784786
let data_type = match bounds {
785787
Some(AstNode { stmt: AstStatement::ExpressionList(expressions), id, location }) => {

src/resolver/tests/const_resolver_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ fn ref_initializer_is_marked_as_resolve_later() {
13111311

13121312
assert_eq!(init.scope, Some("foo".into()));
13131313
assert_eq!(init.lhs, Some("ps".into()));
1314-
assert_eq!(init.target_type_name, "__foo_ps".to_string());
1314+
assert_eq!(init.target_type_name, Some("__foo_ps".to_string()));
13151315
}
13161316

13171317
#[test]
@@ -1338,7 +1338,7 @@ fn adr_initializer_is_marked_as_resolve_later() {
13381338

13391339
assert_eq!(init.scope, Some("foo".into()));
13401340
assert_eq!(init.lhs, Some("ps".into()));
1341-
assert_eq!(init.target_type_name, "__foo_ps".to_string());
1341+
assert_eq!(init.target_type_name, Some("__foo_ps".to_string()));
13421342
}
13431343

13441344
#[test]
@@ -1368,13 +1368,13 @@ fn alias_initializer_is_marked_as_resolve_later() {
13681368

13691369
assert_eq!(init.scope, Some("foo".into()));
13701370
assert_eq!(init.lhs, Some("ps1".into()));
1371-
assert_eq!(init.target_type_name, "__foo_ps1".to_string());
1371+
assert_eq!(init.target_type_name, Some("__foo_ps1".to_string()));
13721372

13731373
let Some(UnresolvableKind::Address(ref init)) = unresolvable[1].kind else { panic!() };
13741374

13751375
assert_eq!(init.scope, Some("foo".into()));
13761376
assert_eq!(init.lhs, Some("ps2".into()));
1377-
assert_eq!(init.target_type_name, "__foo_ps2".to_string());
1377+
assert_eq!(init.target_type_name, Some("__foo_ps2".to_string()));
13781378
}
13791379

13801380
#[test]
@@ -1401,5 +1401,5 @@ fn reference_to_initializer_is_marked_as_resolve_later() {
14011401

14021402
assert_eq!(init.scope, Some("foo".into()));
14031403
assert_eq!(init.lhs, Some("ps".into()));
1404-
assert_eq!(init.target_type_name, "__foo_ps".to_string());
1404+
assert_eq!(init.target_type_name, Some("__foo_ps".to_string()));
14051405
}

src/validation/tests/assignment_validation_tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,24 @@ fn ref_assignment_with_global_local_variables_and_aliased_types() {
13551355
18 │ invalidB : REFERENCE TO fooGlobal;
13561356
│ ^^^^^^^^^^^^^^^^^^^^^^ REFERENCE TO variables can not reference other variables
13571357
1358+
error[E052]: Unknown type: AliasedDINT
1359+
┌─ <internal>:11:64
1360+
1361+
11 │ referenceToAlias : REFERENCE TO AliasedDINT;
1362+
│ ^^^^^^^^^^^ Unknown type: AliasedDINT
1363+
1364+
error[E052]: Unknown type: fooLocal
1365+
┌─ <internal>:17:41
1366+
1367+
17 │ invalidA : REFERENCE TO fooLocal;
1368+
│ ^^^^^^^^ Unknown type: fooLocal
1369+
1370+
error[E052]: Unknown type: fooGlobal
1371+
┌─ <internal>:18:41
1372+
1373+
18 │ invalidB : REFERENCE TO fooGlobal;
1374+
│ ^^^^^^^^^ Unknown type: fooGlobal
1375+
13581376
error[E037]: Invalid assignment: cannot assign 'INT' to 'DINT'
13591377
┌─ <internal>:28:13
13601378

src/validation/tests/reference_resolve_tests.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ fn resolve_function_members_via_qualifier() {
176176
let diagnostics = parse_and_validate_buffered(
177177
"
178178
PROGRAM prg
179-
VAR
180-
s : MyStruct;
181-
END_VAR
182179
foo(a := 1, b := 2, c := 3); (* ok *)
183180
foo.a; (* not ok *)
184181
foo.b; (* not ok *)

src/validation/tests/snapshots/rusty__validation__tests__reference_resolve_tests__resolve_function_members_via_qualifier.snap

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ source: src/validation/tests/reference_resolve_tests.rs
33
expression: "&diagnostics"
44
---
55
error[E048]: Could not resolve reference to a
6-
┌─ <internal>:7:21
6+
┌─ <internal>:4:21
77
8-
7 │ foo.a; (* not ok *)
8+
4 │ foo.a; (* not ok *)
99
│ ^ Could not resolve reference to a
1010

1111
error[E048]: Could not resolve reference to b
12-
┌─ <internal>:8:21
12+
┌─ <internal>:5:21
1313
14-
8 │ foo.b; (* not ok *)
14+
5 │ foo.b; (* not ok *)
1515
│ ^ Could not resolve reference to b
1616

1717
error[E048]: Could not resolve reference to c
18-
┌─ <internal>:9:21
18+
┌─ <internal>:6:21
1919
20-
9 │ foo.c; (* not ok *)
20+
6 │ foo.c; (* not ok *)
2121
│ ^ Could not resolve reference to c
22-
23-

src/validation/tests/snapshots/rusty__validation__tests__statement_validation_tests__program_implicit_downcast.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ source: src/validation/tests/statement_validation_tests.rs
33
expression: "&diagnostics"
44
---
55
warning[E067]: Implicit downcast from 'LINT' to 'INT'.
6-
┌─ <internal>:11:17
6+
┌─ <internal>:10:17
77
8-
11 │ var1_lint, // downcast
8+
10 │ var1_lint, // downcast
99
│ ^^^^^^^^^ Implicit downcast from 'LINT' to 'INT'.
1010

1111
warning[E067]: Implicit downcast from 'LWORD' to 'DWORD'.
12-
┌─ <internal>:12:17
12+
┌─ <internal>:11:17
1313
14-
12 │ var_lword, // downcast
14+
11 │ var_lword, // downcast
1515
│ ^^^^^^^^^ Implicit downcast from 'LWORD' to 'DWORD'.
1616

1717
warning[E067]: Implicit downcast from 'INT' to 'SINT'.
18-
┌─ <internal>:14:17
18+
┌─ <internal>:13:17
1919
20-
14 │ INT#var1_lint, // downcast
20+
13 │ INT#var1_lint, // downcast
2121
│ ^^^^^^^^^^^^^ Implicit downcast from 'INT' to 'SINT'.
2222

2323
warning[E067]: Implicit downcast from 'LINT' to 'INT'.
24-
┌─ <internal>:15:17
24+
┌─ <internal>:14:17
2525
26-
15 │ var2_lint, // downcast
26+
14 │ var2_lint, // downcast
2727
│ ^^^^^^^^^ Implicit downcast from 'LINT' to 'INT'.
2828

2929
error[E037]: Invalid assignment: cannot assign 'WSTRING' to 'STRING'
30-
┌─ <internal>:16:17
30+
┌─ <internal>:15:17
3131
32-
16 │ var_wstr, // invalid
32+
15 │ var_wstr, // invalid
3333
│ ^^^^^^^^ Invalid assignment: cannot assign 'WSTRING' to 'STRING'
3434

3535
warning[E067]: Implicit downcast from 'LINT' to 'DINT'.
36-
┌─ <internal>:17:17
36+
┌─ <internal>:16:17
3737
38-
17 │ var1_lint // downcast
38+
16 │ var1_lint // downcast
3939
│ ^^^^^^^^^ Implicit downcast from 'LINT' to 'DINT'.

0 commit comments

Comments
 (0)