8000 Fails to reject `int<x>` to `bit<y>` cast with different widths, on constants · Issue #5301 · p4lang/p4c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Fails to reject int<x> to bit<y> cast with different widths, on constants #5301
Open
@jaehyun1ee

Description

@jaehyun1ee

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    p4-specTopics related to the P4 specification (https://github.com/p4lang/p4-spec/).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0