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

Are match_kinds compile-time known? #998

Closed
jnfoster opened this issue Dec 17, 2021 · 6 comments
Closed

Are match_kinds compile-time known? #998

jnfoster opened this issue Dec 17, 2021 · 6 comments
Labels

Comments

@jnfoster
Copy link
Collaborator

This code fails to type check:

#include <core.p4> 

match_kind { fuzzy }

T f<T>(T t) { return t; }

control c()() {
    apply {
        if(f(exact) == fuzzy) { }
    }
}

because match_kinds are not compile-time known values. However, this code does type check:

#include <core.p4> 

match_kind { fuzzy }

T f<T>(T t) { return t; }

control c()() {
    apply {
        if(f(exact) == fuzzy) { }
    }
}

It is a silly example, but is this what we want?

@vgurevich
Copy link
Contributor

vgurevich commented Dec 17, 2021

I think that the first question to ask is whether match kinds are real types :)

I was just thinking where we can use them in practice and so far the only thing that came to mind was to use them as parameters in a control, e.g.:

control c(. . .)
         (match_kind t1_match_kind=exact) 
{
    table t1 {
        key = { hdr.h.f1 : t1_match_kind; }
        actions = { . . . }
        size = 
                    t1_match_kind == exact   ? 65536 : 
                    t1_match_kind == ternary ? 1024    : 
                    0;
    }
    apply {
         . . .
    }
}

The compiler stopped at the line (match_kind t1_match_kind=exact) with the message:

test.p4(140):syntax error, unexpected MATCH_KIND
    (match_kind
     ^^^^^^^^^^

@jnfoster
Copy link
Collaborator Author

Indeed, there sees to be no way to name the match_kind type. But in some cases you can let type inference fill it in...

@QinshiWang
Copy link
Contributor

Should we just treat match_kind and error by utilizing a big part of the mechanism for enum? As the Petr4 paper argued that "The match_kind and error types are “open enumerations,” comparable to the extensible exception type in Standard ML".

@mihaibudiu
Copy link
Contributor

These types are very much like enums.

@jafingerhut
Copy link
Collaborator

I am not sure, but perhaps this PR is related? #1020

Issue I filed recently with a different test program I was trying to make compile with at least p4test, but failed: p4lang/p4c#3091

PR on p4c that is recently merged in (less than an hour ago as I write this comment) that Mihai wrote to fix that issue: p4lang/p4c#3099

@vgurevich You might want to try compiling your test program above with the latest main version of p4c and see if the error message has changed, or maybe there might even not be any error message now?

@mihaibudiu
Copy link
Contributor

I hope that we have now answered this question in the affirmative. Can we close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants