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/list expression so that the cast can be put on either side. Before this change, if the cast was on the right side of the comparison operator error was reported.

* Added tests which verify the above
  • Loading branch information
milossyrmia committed Mar 15, 2022
1 parent e893e65 commit e054f2b
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 4 deletions.
14 changes: 10 additions & 4 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 @@ -1673,14 +1674,19 @@ bool TypeInference::compare(const IR::Node* errorPosition,
auto rs = rtype->to<IR::Type_UnknownStruct>();
if (ls != nullptr || rs != nullptr) {
if (ls != nullptr && rs != nullptr) {
typeError("%1%: cannot compare initializers with unknown types", errorPosition);
typeError("%1%: cannot compare structure-valued expressions with unknown types",
errorPosition);
return false;
}

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 structure-valued expressions with unknown types
bool b2 = {a = 32w1,b = 32w2} == {a = 32w1,b = 32w2};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
issue3057-1.p4(12): [--Werror=type-error] error: ==: cannot compare structure-valued expressions 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 e054f2b

Please sign in to comment.