-
Notifications
You must be signed in to change notification settings - Fork 449
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
Implicit cast from int/bit<W> to int should be disallowed #5088
Comments
While I think 8.11.2. Implicit casts is pretty clear on this, we might consider adding The downside would be that something like |
I think there's a concern that allowing "loops" in possible implicit cast types could lead to issues when resolving overloading and typechecking, so if an implicit cast A -> B was allowed, an implicit cast B -> A should be disallowed. But that may be resolvable. |
@vlstill Hmm, but allowing both |
Good point, this would break constants completely :-/. I don't see other way than to disable the implicit cast you reported, even at risk of breaking existing code. In some future revision of P4, we might consider a different approach, where unsized constants are of generic integral type instead of In P4, that would looks somewhat like this: int a = 42; // ok
bit<4> b = 42; // ok, no cast, just type specialization
bit<4> c = 4w42; // ok, no cast, no type specialization
int d = 4w42; // error, CHANGE from P4C, not spec, constant is bit<4>
bit<4> e = a; // error, CHANGE, a is `int`, which cannot be implicitly casted to bit<4>
bit<4> f = (bit<4>)a; // ok, CHANGE, explicit cast
int g = b; // ok, CHANGE, implicit cast But we can't do that now without breaking code as currently |
That is supposed to be what |
Looking at the discussion in #2444, it seems that it was decided to add implicit conversions from |
Thank you @ChrisDodd for the pointer :) But to my understanding, it added implicit casts between |
There's a one-line comment from Mihai that "perhaps that should be allowed", and that it would be added to the discussions for the LDWG. Unfortunately, it looks like note from meetings before 2023 are lost? -- the link at p4lang/p4-spec#904 just points at more recent notes. |
There is a link near the beginning of the "recent meetings notes" doc to a google doc that contains notes from older meetings. That older Google doc does contain notes from the July 2020 meeting. |
There is not much in the document:
I think the issue should be #2444 as p4-spec is nowhere near that number of issues. Anyway, it is unfortunate, as pointed by @jaehyun1ee and @ChrisDodd, having the cast properly defined in both directions brings a lot of problems. It seems to me that the spec maybe only discussed the casts to bool, but the corresponding PR #2658 also allows the cast on integer constants. My guess is we will not fix this properly until we disentangle type checker from the requirement to see code after constant folding (see also #4634). And any fix will likely break some existing code, causing much inconvenience in the process. |
If we view |
The question is what to do with it now:
BTW. from quick testing it seems that the any-to-any worst case does not work in P4C, probably because it does not search for a proper chain of casts (as C or C++ compilers do), but for a single cast. I'm not sure what spec has to say about chaining casts. const bit<4> foo = 4; // OK, wanted; in spec
const int bar = foo; // OK, not in spec
const int<5> baz = bar; // OK, kinda; in spec
const int<5> boo = foo; // ERROR |
Summarizing the discussion and looking back the previous issue #2444, const int a = 2w1;
const bool b = (bool)a;
So the main intention of the following PR #2658 was to allow explicit I am not familiar with the p4c internals, so I wonder if there is a way to disallow implicit |
P4 inserts implicit casts from
int
to fixed-width types, but not the other way around.However, tests issue2444.p4 and issue3283.p4 expects an implicit cast from fixed-width types to
int
.The text was updated successfully, but these errors were encountered: