Skip to content

fix: also consider references in array validation#1660

Merged
ghaith merged 9 commits intomasterfrom
array_count_refs
Apr 13, 2026
Merged

fix: also consider references in array validation#1660
ghaith merged 9 commits intomasterfrom
array_count_refs

Conversation

@ghaith
Copy link
Copy Markdown
Collaborator

@ghaith ghaith commented Apr 9, 2026

This pull request improves the handling and testing of array initialization diagnostics, specifically for cases where arrays are initialized with fewer elements than their declared size. It enhances both the validation logic and the test infrastructure to ensure that warnings about partially initialized arrays are consistently detected and can be promoted to errors for stricter testing.

Validation logic improvements:

  • Updated the statement_to_array_length function in src/validation/array.rs to handle ReferenceExpr statements, ensuring that the array length is determined correctly for references, defaulting to 1 if the length cannot be determined.

Testing enhancements:

  • Added a new test fewer_elements_in_type_and_derived_variable in src/validation/tests/array_validation_test.rs to verify that warnings (E127) are emitted when both a type and a variable are initialized with too few elements.
  • Introduced a new lit test file array_partial_init_warning_type_and_var.st to check that both relevant diagnostics are emitted and promoted to errors when E127 is configured as an error.
  • Added a diagnostics configuration file diagnostics_e127_as_error.json to promote warning E127 to an error for testing purposes.

Test infrastructure updates:

  • Modified tests/lit/lit.cfg to add the %PLC substitution, making it easier to invoke the compiler in lit tests.

array validators did not account for references when counting the
initialised elements
Comment thread src/validation/tests/array_validation_test.rs Outdated
Comment on lines +3 to +4
// CHECK-DAG: {{.*}}array_partial_init_warning_type_and_var.st:{{[0-9]+}}:{{[0-9]+}}:{{.*}}error[E127]: Fewer initial values for array `a` than expected. Expected 5, found 2.
// CHECK-DAG: {{.*}}array_partial_init_warning_type_and_var.st:{{[0-9]+}}:{{[0-9]+}}:{{.*}}error[E127]: Fewer initial values for array `userArrayType` than expected. Expected 5, found 4.
Copy link
Copy Markdown
Member

@volsa volsa Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hate this, not for the fact that we UI test validations here (big fan) but that we're UI testing with lit. I think we should discuss about moving validations to a better UI testing framework, something similar to what astral does with ruff and rustc. At least that makes sense since the introduction of lowering.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you say it's a blocker for this fix now? i generally agree

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say no but we should write it down somewhere (either here or Jira) to convert existing validations to dedicated files with std{out,err} matching. Is bats a good framework for that btw?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tend to say no, it does not do string comparisons that well, and tests are written in shell language and can be hard to read. I prefer the lit way of inline assertions. So a file has both the code and its assertions

ghaith added 2 commits April 9, 2026 14:36
as part of the array validation improvements, make sure recursive arrays
don't break
volsa
volsa previously approved these changes Apr 9, 2026
Comment thread src/validation/tests/array_validation_test.rs Outdated
Comment on lines +15 to +16
11 │ TYPE userArrayType : ARRAY [1..5] OF INT := [1,2,4,5];
│ ^^^^^^^^^ Fewer initial values for array `userArrayType` than expected. Expected 5, found 4.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! I wonder though, will this develop into a similar situation like the "[...] use REF_TO instead of POINTER TO [...]" validation which we just disabled because it got too noisy for the user? Rust solved this nicely with validations vs clippy lints imo.

@ghaith ghaith enabled auto-merge (squash) April 10, 2026 11:01
@ghaith ghaith requested a review from volsa April 10, 2026 11:01
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 13, 2026

Build Artifacts

🪟 Windows

Artifact Link Size
stdlib.lib Download 4.3 MB
stdlib.dll Download 0.1 MB
plc.exe Download 38.4 MB

From workflow run

🐧 Linux

Artifact Link Size
deb-aarch64 Download 30.9 MB
plc-aarch64 Download 43.5 MB
deb-x86_64 Download 38.6 MB
schema Download 0.0 MB
stdlib Download 33.0 MB
plc-x86_64 Download 43.5 MB

From workflow run

@ghaith ghaith merged commit e20c267 into master Apr 13, 2026
18 of 19 checks passed
@ghaith ghaith deleted the array_count_refs branch April 13, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants