rusty icon indicating copy to clipboard operation
rusty copied to clipboard

Segfault when passing a parameter to a reference to input

Open Angus-Bethke-Bachmann opened this issue 2 months ago • 1 comments

Describe the bug A Segfault occurs when passing a parameter to a reference to input.

To Reproduce Steps to reproduce the behavior:

TYPE EnumType : (
        red,
        green,
        blue
    );
END_TYPE

FUNCTION fnReferenceTo : EnumType
VAR_INPUT
    inVar	: Reference To EnumType;
END_VAR
    fnReferenceTo := inVar;
END_FUNCTION

FUNCTION main
VAR
    outVarReferenceTo : EnumType;
END_VAR
    outVarReferenceTo := fnReferenceTo(blue);
END_FUNCTION

Expected behavior There shouldn't be a segfault. A validation error should be present in the case of a invalid scenario, and it should work if the scenario is valid.

Angus-Bethke-Bachmann avatar Oct 17 '25 07:10 Angus-Bethke-Bachmann

This is due to constants being propagated. When trying to emit IR from this example, we get this:

Builder error: /tmp/.tmpkFPSpE/target/demo.st.ll:27:20: error: '@fnReferenceTo' defined with type 'i32 (i32*)*' but expected 'i32 (i32)*'
  %call = call i32 @fnReferenceTo(i32 2)
                   ^
.

In order to support passing enum variants (or any constant for that matter) by reference, we need to either reference that constant/variant in .rodata rather than folding it or we need to allocate a temporary variable to store the value into, which we can then pass to the function instead.

mhasel avatar Nov 11 '25 13:11 mhasel