Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should the compiler reject shift by a type-created type? #5100

Open
vlstill opened this issue Jan 15, 2025 · 0 comments
Open

Should the compiler reject shift by a type-created type? #5100

vlstill opened this issue Jan 15, 2025 · 0 comments
Assignees
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).

Comments

@vlstill
Copy link
Contributor

vlstill commented Jan 15, 2025

In process of fixing #5094, I noticed that there are testdata/p4_16_samples/psa-dpdk-binary-operations.p4 which have shift by a type that is introduced by type:

// dpdk/psa.p4
// ...
typedef bit<32>  PortIdUint_t;
// ...
type PortIdUint_t         PortId_t;
// ...

// psa-dpdk-binary-operations.p4
// ...
    action forward() {
        b.meta = 32w0x1 << c.ingress_port;
    }

By the spec, this should not be accepted (8.23. Operations on types introduced by type)

Values with a type introduced by the type keyword provide only a few operations:

  • assignment to left-values of the same type
  • comparisons for equality and inequality if the original type supported such comparisons
  • casts to and from the original type

The rationale is probably quite simple -- these type (introduced by type) should be distinguishable from their underlying types. In part to actually protect against uses like these, where the type can be accidentally used in an arithmetic operation, possibly yielding unexpected result.

I think we should reject this code, but it is a breaking change that might require P4 programmers to work around it.

@vlstill vlstill added core Topics concerning the core segments of the compiler (frontend, midend, parser) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). labels Jan 15, 2025
@vlstill vlstill self-assigned this Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

No branches or pull requests

1 participant