-
Notifications
You must be signed in to change notification settings - Fork 80
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
Allow compile-time resolvable expressions for switch labels? #1281
Comments
For actions the problem is (I guess) that there is no type for the expression result. Allowing this would require introducing something like control c()
{
action a(){}
action b(){}
action c(){}
table t1 {
actions = { a ; b; }
}
table t2 {
actions = { a; c; }
}
apply {
switch(t.apply().action_run) {
true ? a : c : {}
}
}
} In this case, if we check only after constant folding, we would accept the program. But I would propose the program is ill-formed as if I replace Of course, the complication with something like Furthermore, currently we would be in practice mostly using this with expressions, as functions cannot be declared inside So overall I would say this is an interesting idea, and I could see it useful in some cases to pull some parameters out of the control block, but I think it would not be a small change in the spec to do it properly. Therefore it would be nice to have a real use case. |
While the particular example given in the issue has actions as the switch case labels, there are also switch statements that can other types as the case labels, e.g. If so, is it weird to consider that expressions would be allowed for types that already have expressions defined for them in the language, and not introduce new expressions whose result is an action name? Or is the question "should we add expressions whose result is an action name?" |
They should also be foldable if they are compile-time known. I believe #4656 already explores this. The spec is relatively flexible for the expression type of a switch label: The only restriction is that for |
I would like it if the question was interpreted that way :-). That is a way of allowing this that would be consistent with the rest of the spec. But of course, such expressions must have type (that should be expressible in P4 I'd say).
Frankly, I would not consider how this is implemented too much when speaking about the spec. It definitely must be implementable, but the exact implementation is up to discussion on the compiler side. The spec does not map to the compiler directly after all (e.g. there are not path expressions in the spec). |
What I want to point out is that the spec already contains support for such expressions in the form of Unless you want to explicitly add a new element that is an |
Prompted by the discussion in p4lang/p4c#4657.
Currently, this program is illegal according to the P4 specification:
There was a fix in the compiler that made it illegal: p4lang/p4c#3622
The spec reason is:
All switch labels must be names of actions of the table t, or default.
Should this actually be illegal? I currently can not think of a case where this would be troublesome. It might even be useful to change labels because of a compile-time value change?
The text was updated successfully, but these errors were encountered: