Open
Description
According to the P4 spec, (explicit) casts between fixed-width bit and int types are allowed only when their widths are the same. P4C performs such a check, i.e.,
extern void sink(in int<31> t);
control C(in bit<32> b);
package P(C c);
control CImpl(in bit<32> b) {
apply {
sink((int<31>) b);
}
}
P(CImpl ()) main;
is rejected with:
[--Werror=type-error] error: '(int<31>)b'
sink((int<31>) b);
^^^^^^^^^^^
---- Actual error:
Cannot unify type 'bit<32>' with type 'int<31>'
---- Originating from:
Cannot cast from 'bit<32>' to 'int<31>'
However, it seems like on constants, the constant folding pass takes place before such type check.
Below program, although it casts int<32>
to bit<0>
, is not rejected by p4test
.
extern void sink(in bit<6> t);
const bit<32> two = 32w2;
const bit<6> twofour = ((bit<6>) (((bit<0>) (((int<32>) (two))))));
control C(in bit<32> b);
package P(C c);
control CImpl(in bit<32> b) {
apply {
g(twofour);
}
}
P(CImpl ()) main;
At -0010-ConstantFolding_0_DoConstantFolding
pass, twofour
becomes:
const bit<6> twofour = 6w0;
without emitting an error.