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

Unifying struct and list expressions fix (#3057) #3122

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comparisons of list expressions, structure-valued expressions, tuples…
… 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
commit e054f2b691affbec14f65e10f0f5d1b9a416e23a
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;
mihaibudiu marked this conversation as resolved.
Show resolved Hide resolved
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"
}