rusty icon indicating copy to clipboard operation
rusty copied to clipboard

Calling `REF` or `ADR` on a propagated constant variable yields cryptic codegen error

Open mhasel opened this issue 1 year ago • 0 comments

When trying to compile the following snippet:

VAR_GLOBAL CONSTANT
    x: DINT := 4;
END_VAR

FUNCTION main
    VAR
        y : REF_TO DINT;
    END_VAR
    y := REF(x);
END_FUNCTION

the compiler will fail during codegen with the following error:

error[E071]: ()
  ┌─ target/demo.st:9:14
  │
9 │     y := REF(x);
  │              ^ ()

Compilation aborted due to previous errors.
Hint: You can use `plc explain <ErrorCode>` for more information

This is probably due to the constant x's value being folded into each occurance, resulting in the actual REF call to be y := REF(4).

mhasel avatar Dec 06 '24 14:12 mhasel