PLC-lang / rusty

Structured Text Parser and LLVM Frontend
GNU Lesser General Public License v3.0
213 stars 52 forks source link

Misleading error message when calling `MUX` with aggregate types of different sizes and/or types #1134

Open mhasel opened 7 months ago

mhasel commented 7 months ago

When compiling the following example, the error messages printed to console are quite interesting.

FUNCTION main : DINT
VAR
    arr : ARRAY[0..3] OF DINT;
    okay : ARRAY[0..3] OF DINT;
    too_large : ARRAY[0..10] OF DINT;
    wrong_type : ARRAY[0..3] OF STRING;
END_VAR
        arr := MUX(1, okay, too_large, wrong_type);
END_FUNCTION

errors:

error: Invalid assignment: cannot assign 'ARRAY[0..3] OF DINT' to 'LREAL'
   ┌─ target/demo.st:11:23
   │
11 │         arr := MUX(1, okay, too_large, wrong_type);
   │                       ^^^^ Invalid assignment: cannot assign 'ARRAY[0..3] OF DINT' to 'LREAL'

error: Invalid assignment: cannot assign 'ARRAY[0..10] OF DINT' to 'LREAL'
   ┌─ target/demo.st:11:29
   │
11 │         arr := MUX(1, okay, too_large, wrong_type);
   │                             ^^^^^^^^^ Invalid assignment: cannot assign 'ARRAY[0..10] OF DINT' to 'LREAL'

error: Invalid assignment: cannot assign 'ARRAY[0..3] OF STRING' to 'LREAL'
   ┌─ target/demo.st:11:40
   │
11 │         arr := MUX(1, okay, too_large, wrong_type);
   │                                        ^^^^^^^^^^ Invalid assignment: cannot assign 'ARRAY[0..3] OF STRING' to 'LREAL'

error: Invalid assignment: cannot assign 'LREAL' to 'ARRAY[0..3] OF DINT'
   ┌─ target/demo.st:11:9
   │
11 │         arr := MUX(1, okay, too_large, wrong_type);
   │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid assignment: cannot assign 'LREAL' to 'ARRAY[0..3] OF DINT'

The types when validating somehow default to LREAL for both the left and right side of the assignment.

I am not quite sure if MUX is supposed to work with literal arrays, but currently it does not, even if they are matching in type and size. In any case, the error messages could do with some improvement.

When trying to compile arr := MUX(1, [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]); we end up with

error: Could not resolve generic type U with ANY
  ┌─ target/demo.st:5:23
  │
5 │         arr := MUX(1, [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]);
  │                       ^^^^^^^^^^^^ Could not resolve generic type U with ANY

                       ^... repeats for each literal array...

error: Invalid assignment: cannot assign '' to 'ARRAY[0..3] OF DINT'
  ┌─ target/demo.st:5:9
  │
5 │         arr := MUX(1, [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]);
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid assignment: cannot assign '' to 'ARRAY[0..3] OF DINT'
mhasel commented 7 months ago

There are similar problems for array-type arguments with the SEL function - the type reported in the error message defaults to LREAL for both left and right in different messages when passing incompatible args.