Skip to content

Add validation for type names used as values in expressions #1546

@mhasel

Description

@mhasel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions