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

Type inference succeeds when it shouldn't #5041

Open
jaehyun1ee opened this issue Dec 2, 2024 · 0 comments
Open

Type inference succeeds when it shouldn't #5041

jaehyun1ee opened this issue Dec 2, 2024 · 0 comments
Labels
bug This behavior is unintended and should be fixed. p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).

Comments

@jaehyun1ee
Copy link

The p4c frontend regards the following program as valid, when it is actually not.

struct a_t { bool f; }
struct b_t { bit<32> f; }

control foo(inout a_t one, inout a_t two) { apply {} }
control bar(inout b_t one, inout b_t two) { apply {} }

control C<H, M>(inout H one, inout M two);

package P<H, M>(C<H, M> c1 = foo(), C<H, M> c2 = bar());

P<_, _>() main;

Here, we do not supply any argument to the instantiation of package main, so the default values of c1 and c2 take place.
However, c1 implies H = a_t and M = a_t while c2 implies that H = b_t and M = b_t.
So in this case, the compiler shouldn't be able to infer the correct type argument for the don't care types (_) in P<_, _>.

Also, if we change the program as follows, where we instantiate P() main; instead of P<_, _>() main;,

struct a_t { bool f; }
struct b_t { bit<32> f; }

control foo(inout a_t one, inout a_t two) { apply {} }
control bar(inout b_t one, inout b_t two) { apply {} }

control C<H, M>(inout H one, inout M two);

package P<H, M>(C<H, M> c1 = foo(), C<H, M> c2 = bar());

P() main;

then the compiler rejects the program (as expected) saying:

default-package-argument-reduced.p4(11): [--Werror=type-error] error: main: could not infer a type for variable H
P() main;
    ^^^^
default-package-argument-reduced.p4(9)
package P<H, M>(C<H, M> c1 = foo(), C<H, M> c2 = bar());

I believe it suggests some possibility that the frontend overlooks some case when inferring concrete types for don't care types _.

@fruffy fruffy added bug This behavior is unintended and should be fixed. p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). labels Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This behavior is unintended and should be fixed. p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

No branches or pull requests

2 participants