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

generic.p4 contains invalid code #5174

Open
asl opened this issue Mar 11, 2025 · 4 comments
Open

generic.p4 contains invalid code #5174

asl opened this issue Mar 11, 2025 · 4 comments
Labels
p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). question This is a topic requesting clarification.

Comments

@asl
Copy link
Contributor

asl commented Mar 11, 2025

generic.p4 test contains the following:

control z<D1, T1>(X<D1> x, in D1 v, in T1 t)
{
   // x's type is X<D1>
   // x.f's type is T f<T>(D1 d, T t);

    apply {
        T1 r1 = x.f<T1>(v, t);
        T1 r2 = x.f(v, t);
    }
}

While the spec clearly says:

Unlike control type declarations, control declarations may not be generic---e.g., the following declaration is illegal:

control C<H>(inout H data) { /* Body omitted */ }

However, no diagnostics is ever produced. Because control is unused?

generic.p4(17): [--Wwarn=unused] warning: 'T' is unused
extern Crc16<T>
             ^
generic.p4(27): [--Wwarn=unused] warning: Control q is not used; removing
control q<S>(in S dt)
        ^
generic.p4(41): [--Wwarn=unused] warning: Control z is not used; removing
control z<D1, T1>(X<D1> x, in D1 v, in T1 t)
        ^
[--Wwarn=missing] warning: Program does not contain a `main' module
@asl
Copy link
Contributor Author

asl commented Mar 11, 2025

Tagging @ChrisDodd

@asl
Copy link
Contributor Author

asl commented Mar 11, 2025

Also #2265 refers to the following code that also seems to be invalid per spec:

T f<T>(T data) {
    T tmp;
    tmp = data;
    return tmp;
}

control c<T>(inout T data) {
    apply {
        T tmp;
        data = tmp;
    }
}

@fruffy fruffy added p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). question This is a topic requesting clarification. labels Mar 11, 2025
@jafingerhut
Copy link
Contributor

Note that the commit history for test program generic.p4 goes back to 2016 with Mihai Budiu. It has been somewhat common that in a few areas, p4c has had experimental extensions that went beyond the language specification.

That said, I do not know the detailed history of proposals for supporting generics in more or fewer places in the P4 language, as I have never personally used them for anything, nor thought deeply about the implications on the language.

@asl
Copy link
Contributor Author

asl commented Mar 11, 2025

@jafingerhut Yeah. The problem is that while it might be an extension, no diagnostics is currently produced. So user might write such code by accident and not realize that this is something now well supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). question This is a topic requesting clarification.
Projects
None yet
Development

No branches or pull requests

3 participants