-
Notifications
You must be signed in to change notification settings - Fork 446
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
overeager typechecking of generic types. #5067
Comments
Yes, but that would require instantiating the function first, and only then type checking it (like C++ templates). Instead, the P4 function has to type check for the generic types, and there are essentially no allowed operations on values of generic type. I believe the P4 inliner also needs types to inline functions. Usually, there are 2 approaches that programming languages with parametric polymorphism (type parameters) take:
Certainly it would be useful to have ability to write a function like the |
Actually, modern compilers do some semantic checks on uninstantiated templates. The C++ standard even requires this up to some extent (the code in the template is required to be semantically correct, so it should for example, reference undeclared functions). This often causes troubles with legacy code since the uninstantiated templates might stop compile :) |
FYI, there have been discussions in the P4 language design work group in past years regarding whether P4 the language, and/or p4c the implementation, should do one of those or the other. Me, I personally kind of like the flexibility that "expand, then type check" gives the P4 developer. It seems to me it would easily allow a parser or control to be written with constructor parameters that could be used as bitwidths of I do not want to incorrectly put words in the mouths of others, but if I recall correctly I believe at least Nate Foster, and perhaps some others, are more in favor of the "type check everything before inlining/expanding" approach. I do not recall all the pros vs. cons of these two approaches, but if someone wanted to create such a list, I'd be happy to review and add anything I thought of, to the list. |
Consider the following program with a generic function (minor change of testdata/p4_16_samples/function.p4):
It fails with error message
However, it would seem to be ok, as the
max
function is only ever instantiated withbit<16>
forT
, which should be ok. If you replace the explicitly typed constants asmax(10, 12)
, you get even more errors, even though that should be ok too.The text was updated successfully, but these errors were encountered: