Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,8 +1609,18 @@ impl Index {
/// Returns all enum variants of the given variable.
pub fn get_enum_variants_by_variable(&self, variable: &VariableIndexEntry) -> Vec<&VariableIndexEntry> {
let Some(var) = self.type_index.find_type(&variable.data_type_name) else { return vec![] };
let DataTypeInformation::Enum { variants, .. } = var.get_type_information() else { return vec![] };
variants.iter().collect()

match var.get_type_information() {
DataTypeInformation::Enum { variants, .. } => variants.iter().collect(),
DataTypeInformation::Pointer { name, inner_type_name, .. } => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would also check here that the pointer is an auto deref pointer, otherwise we start returing type variants for pointer types.

Suggested change
DataTypeInformation::Pointer { name, inner_type_name, .. } => {
DataTypeInformation::Pointer { name, inner_type_name, auto_deref: Some(_).. } => {

Copy link
Author

Choose a reason for hiding this comment

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

Where would you recommend adding the unit tests? I haven't looked very deep, but I haven't figured out how properly use the .snap based tests.

Copy link
Collaborator

@ghaith ghaith Sep 15, 2025

Choose a reason for hiding this comment

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

the .snap files are snapshot tests, we create them using Cargo Insta the way this works is you write a test that should produce some kind of string, you can find examples in the codegen tests for that
What we usually have is something like

fn test() {
  let code = codegen("<plc code>");
  insta::assert_snapshot!(code, r#""#);
}

then if we run cargo test a snapshot file (.snap.new) will be generated. we then run cargo insta review and check if the snap file looks like what we expect, if it does we accept it and it will be added to the file.
If we skip the r#""# part in the assert_insta_snapshot macro, the result will be a .snap file.

In your case, i would recommend the first tests to go into index_tests.rs similar to pou_with_rescursive_type_fails and then you can add codegen tests to check if the argument is being passed correctly. The best place for codegen tests in this area would be the expression_generator tests.

Codegen tests are usually a bit cryptic, so what you can also do is first write some lit tests. These are integration test, just copy a .st file from the tests/lit/single file and change it to fit your test. any comment with //CHECK will be evaluated. You can use printf to output to the terminal, that's what CHECK will look for.
To use lit just run cargo xtask lit it will build the project, the standard functions and run all lit tests. You need to be in our dev container or have lit setup for that.
If you don't want to run lit locally you can also just compile and run the file by hand. You just need to include the printf function (under lit/utils) to your file and compile with --linker=cc or --linker=clang so libc gets linked with your binary.

let Some(inner_type) = self.type_index.find_type(inner_type_name) else { return vec![] };
let DataTypeInformation::Enum { variants, .. } = inner_type.get_type_information() else {
return vec![];
};
variants.iter().collect()
}
_ => vec![],
}
}

/// Tries to return an enum variant defined within a POU
Expand Down
Loading