p4lang / p4c

P4_16 reference compiler

Home Page:https://p4.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong error message when arbitrary-precision int is shifted by runtime value

vlstill opened this issue · comments

Using shift.p4 as a test program.

$ ./p4c/p4test shift.p4
shift.p4(8): [--Werror=type-error] error: 5 >> b: width of left operand of shift needs to be specified
    hdr.v = (bit<8>)(a >> b);
                     ^^^^^^

This error message is not correct, as spec v1.2.4 sec 8.9.2 says:

The left operand of shifts can be any one out of unsigned bit-strings, signed bit-strings, and arbitrary-precision integers, and the right operand of shifts must be either an expression of type bit<S> or a non-negative integer compile-time known value. The result has the same type as the left operand.

So the problem is not that the left operand is int. The actual problem is that this would imply the result is also int, which is a problem. It is a problem because b is a runtime value and therefore the result is a runtime value too. And runtime values cannot have type int (sec 8.8 f spec):

The type int denotes arbitrary-precision integers. In P4, all expressions of type int must be compile-time known values. [...]

Note that this works if b is turned into a constant or if a is turned into fixed-width value.


I think this is just a case of unclear message in the end.