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

Question on comparison to negative constants #2392

Closed
fruffy opened this issue May 23, 2020 · 2 comments · Fixed by #2404
Closed

Question on comparison to negative constants #2392

fruffy opened this issue May 23, 2020 · 2 comments · Fixed by #2404
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.

Comments

@fruffy
Copy link
Collaborator

fruffy commented May 23, 2020

So I am a little bit confused on how to treat the following expression:
Is this true, false, or undefined?

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
    apply {
        if (8w255 == -1) {
            h.eth_hdr.eth_type = 1;
        } else {
            h.eth_hdr.eth_type = 2;
        }
    }
}

After FrontEnd_6_ConstantFolding this evaluates to false.
The compiler naturally also gives a warning:

bugs/negative_equality.p4(30): [--Wwarn=mismatch] warning: -1: negative value with unsigned type
        if (8w255 == -1) {

I assumed this equality would evaluate to true since both expressions are 8w255 after the implicit cast. Unless such a comparison always evaluates to false, regardless of value, because of the type mismatch. I was digging through the constantFolding.cpp code and saw that the result is evaluate before the cast: https://github.com/p4lang/p4c/blob/master/frontends/common/constantFolding.cpp#L419
Is this intentional?

negative_equality.p4.txt
negative_equality.stf.txt

@mihaibudiu
Copy link
Contributor

Good question, I think this is a bug, because the spec says that -1 should be cast to whatever type is on the left, and then this should be true.

@mihaibudiu mihaibudiu added the bug This behavior is unintended and should be fixed. label May 26, 2020
@mihaibudiu
Copy link
Contributor

We will discuss this in the design working group nevertheless.

@mihaibudiu mihaibudiu added the fixed This topic is considered to be fixed. label May 29, 2020
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. fixed This topic is considered to be fixed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants