rusty icon indicating copy to clipboard operation
rusty copied to clipboard

Add validation for type names used as values in expressions

Open mhasel opened this issue 1 month ago • 0 comments

Describe the bug The compiler currently fails to detect when type names are used as values in expressions during the validation phase. This results in a late-stage codegen error that is cryptic and unhelpful to users. Current Behavior When a type name is used as a value in an assignment or expression, the compiler fails during codegen with: Could not resolve reference to <TYPE> at: <location> Hint: You can use plc explain <ErrorCode> for more information

To Reproduce Steps to reproduce the behavior: Compile this code example:

PROGRAM main
    VAR
        a : INT;
        b : MyInt;
        c : Color;
        d : MyStruct;
    END_VAR
    a := INT;        // Type name as value
    b := MyInt;      // Type name as value - alias type
    c := Color;      // Type name as value - enum type  
    d := MyStruct;   // Type name as value - struct type
END_PROGRAM

Here's another example:

TYPE Color : INT (red := 1, green := 2, blue := 3); END_TYPE

VAR_GLOBAL
    myColor : Color;
END_VAR

PROGRAM main
    myColor := red;      // Unqualified variant - ok
    myColor := Color;    // Type itself - should be unresolvable
END_PROGRAM

Expected behavior The compiler should detect this during the validation phase (before codegen) and emit a clear diagnostic message

mhasel avatar Nov 11 '25 09:11 mhasel