Skip to content

Serializable enums: should comparison of values of two different enum types work? (or enum and underlying type) #1369

@vlstill

Description

@vlstill

See discussion from p4lang/p4c#5246.

enum bit<8> E1 { A = 1, B = 2, C = 10 }
enum bit<8> E2 { X = 2, Y = 1, Z = 3 }
enum bit<2> E3 { P = 1, Q = 2 }

// ...

control c(inout headers hdr) {
    apply {
        bit<8> a = 10;
        if (a == E1.C) { ... } // (1) ??
        if (E1.A == E2.X) { ... } // (2) ??
        if (E1.A == E3.Q) { ... } // (3) ??
    }
}

The question is if any of the case (1), (2), (3) should work.

As @ChrisDodd pointed out,

an enum with an underlying type can be thought of as being a type derived from the underlying type carrying equality, assignment, and casts to/from the underlying type.

I would ready it as permitting (1) for sure and maybe (2), but not (3), because there the underlying type is different.

I think the case of different underlying type is clear (it is not allowed). Personally, I would prefer all of them being disallowed, but I am not sure if P4C actually disallows (1) currently, or only (2), so doing that might be a breaking change. Maybe at least a consideration for "P4-26".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions