P2032

Code

P2032

Message

Reference type mismatch in assignment

This error occurs when a reference assignment has incompatible types. The reference type and the target type must match exactly, and a reference takes only REF(), NULL or another reference.

Example

The following code will generate error P2032:

PROGRAM Main
    VAR
        x : INT;
        y : REF_TO REAL;
    END_VAR

    y := REF(x);  (* Error: REF_TO REAL cannot hold reference to INT *)
END_PROGRAM

To fix this error, ensure the reference type matches the variable type:

PROGRAM Main
    VAR
        x : INT;
        y : REF_TO INT;
    END_VAR

    y := REF(x);  (* Correct: REF_TO INT matches INT variable *)
END_PROGRAM

A value that is not a reference

The following code will generate error P2032:

PROGRAM Main
    VAR
        x : INT;
        r : REF_TO INT;
    END_VAR

    r := 0;  (* Error: 0 is a number, not a reference *)
END_PROGRAM

A reference holds where a variable is, so a number, or a variable of another type, is not something it can hold. This applies to a CODESYS POINTER TO as well: p := 0 is an error, in every dialect.

To fix this error, assign NULL for a reference that refers to nothing, or the reference to a variable:

PROGRAM Main
    VAR
        x : INT;
        r : REF_TO INT;
    END_VAR

    r := NULL;    (* Correct: refers to nothing *)
    r := REF(x);  (* Correct: refers to x *)
END_PROGRAM

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.