Skip to content

Commit

Permalink
Comparisons of list expressions, structure-valued expressions, tuples…
Browse files Browse the repository at this point in the history
… and structs #3057

* I corrected the code so that the compiler bug does not occur when comparing structures of unknown type, but to report an error

* I enabled a comparison between the structure and the cast structure so that cast can be put on either side

* Added tests
  • Loading branch information
milossyrmia committed Mar 11, 2022
1 parent a6b9aad commit 21c26e7
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 3 deletions.
11 changes: 8 additions & 3 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,8 @@ bool TypeInference::compare(const IR::Node* errorPosition,

bool defined = false;
if (typeMap->equivalent(ltype, rtype) &&
(!ltype->is<IR::Type_Void>() && !ltype->is<IR::Type_Varbits>())) {
(!ltype->is<IR::Type_Void>() && !ltype->is<IR::Type_Varbits>())
&& !ltype->to<IR::Type_UnknownStruct>()) {
defined = true;
} else if (ltype->is<IR::Type_Base>() && rtype->is<IR::Type_Base>() &&
typeMap->equivalent(ltype, rtype)) {
Expand All @@ -1679,8 +1680,12 @@ bool TypeInference::compare(const IR::Node* errorPosition,

bool lcst = isCompileTimeConstant(compare->left);
bool rcst = isCompileTimeConstant(compare->right);

auto tvs = unify(errorPosition, ltype, rtype);
TypeVariableSubstitution* tvs;
if (ls == nullptr){
tvs = unify(errorPosition, ltype, rtype);
} else {
tvs = unify(errorPosition, rtype, ltype);
}
if (tvs == nullptr)
return false;
if (!tvs->isIdentity()) {
Expand Down
17 changes: 17 additions & 0 deletions testdata/p4_16_errors/issue3057-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct S {
bit<32> a;
bit<32> b;
}

control proto();
package top(proto _p);
control c() {
apply {
bool b1 = { 1, 2 } == { 1, 2 };
bool b2 = {a = 32w1,b = 32w2} == {a = 32w1,b = 32w2};
bool b2_ = {a = 1,b = 2} == {a = 1,b = 2};
}
}

top(c()) main;

17 changes: 17 additions & 0 deletions testdata/p4_16_errors_outputs/issue3057-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct S {
bit<32> a;
bit<32> b;
}

control proto();
package top(proto _p);
control c() {
apply {
bool b1 = { 1, 2 } == { 1, 2 };
bool b2 = {a = 32w1,b = 32w2} == {a = 32w1,b = 32w2};
bool b2_ = {a = 1,b = 2} == {a = 1,b = 2};
}
}

top(c()) main;

6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/issue3057-1.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issue3057-1.p4(11): [--Werror=type-error] error: ==: cannot compare initializers with unknown types
bool b2 = {a = 32w1,b = 32w2} == {a = 32w1,b = 32w2};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
issue3057-1.p4(12): [--Werror=type-error] error: ==: cannot compare initializers with unknown types
bool b2_ = {a = 1,b = 2} == {a = 1,b = 2};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35 changes: 35 additions & 0 deletions testdata/p4_16_samples/issue3057-2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2016 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

struct S {
bit<32> a;
bit<32> b;
}


control proto();
package top(proto _p);

control c() {
apply {

bool b5 = (S) { a = 1, b = 2 } == { a = 1, b = 2 };
bool b5_ = { a = 1, b = 2 } == (S) { a = 1, b = 2 };

}
}

top(c()) main;
16 changes: 16 additions & 0 deletions testdata/p4_16_samples_outputs/issue3057-2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct S {
bit<32> a;
bit<32> b;
}

control proto();
package top(proto _p);
control c() {
apply {
bool b5 = (S){a = 32w1,b = 32w2} == (S){a = 32w1,b = 32w2};
bool b5_ = (S){a = 32w1,b = 32w2} == (S){a = 32w1,b = 32w2};
}
}

top(c()) main;

14 changes: 14 additions & 0 deletions testdata/p4_16_samples_outputs/issue3057-2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct S {
bit<32> a;
bit<32> b;
}

control proto();
package top(proto _p);
control c() {
apply {
}
}

top(c()) main;

14 changes: 14 additions & 0 deletions testdata/p4_16_samples_outputs/issue3057-2-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct S {
bit<32> a;
bit<32> b;
}

control proto();
package top(proto _p);
control c() {
apply {
}
}

top(c()) main;

16 changes: 16 additions & 0 deletions testdata/p4_16_samples_outputs/issue3057-2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct S {
bit<32> a;
bit<32> b;
}

control proto();
package top(proto _p);
control c() {
apply {
bool b5 = (S){a = 1,b = 2} == {a = 1,b = 2};
bool b5_ = {a = 1,b = 2} == (S){a = 1,b = 2};
}
}

top(c()) main;

Empty file.
Empty file.
3 changes: 3 additions & 0 deletions testdata/p4_16_samples_outputs/issue3057-2.p4.p4info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg_info {
arch: "v1model"
}

0 comments on commit 21c26e7

Please sign in to comment.