diff --git a/frontends/p4/typeChecking/typeChecker.cpp b/frontends/p4/typeChecking/typeChecker.cpp index 24c6ef13bf..28214cd8a2 100644 --- a/frontends/p4/typeChecking/typeChecker.cpp +++ b/frontends/p4/typeChecking/typeChecker.cpp @@ -1653,7 +1653,8 @@ bool TypeInference::compare(const IR::Node* errorPosition, bool defined = false; if (typeMap->equivalent(ltype, rtype) && - (!ltype->is() && !ltype->is())) { + (!ltype->is() && !ltype->is()) + && !ltype->to()) { defined = true; } else if (ltype->is() && rtype->is() && typeMap->equivalent(ltype, rtype)) { @@ -1673,14 +1674,19 @@ bool TypeInference::compare(const IR::Node* errorPosition, auto rs = rtype->to(); 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()) { diff --git a/testdata/p4_16_errors/issue3057-1.p4 b/testdata/p4_16_errors/issue3057-1.p4 new file mode 100644 index 0000000000..1fa1567b4b --- /dev/null +++ b/testdata/p4_16_errors/issue3057-1.p4 @@ -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; + diff --git a/testdata/p4_16_errors_outputs/issue3057-1.p4 b/testdata/p4_16_errors_outputs/issue3057-1.p4 new file mode 100644 index 0000000000..1fa1567b4b --- /dev/null +++ b/testdata/p4_16_errors_outputs/issue3057-1.p4 @@ -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; + diff --git a/testdata/p4_16_errors_outputs/issue3057-1.p4-stderr b/testdata/p4_16_errors_outputs/issue3057-1.p4-stderr new file mode 100644 index 0000000000..c5fad282bb --- /dev/null +++ b/testdata/p4_16_errors_outputs/issue3057-1.p4-stderr @@ -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}; + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/testdata/p4_16_samples/issue3057-2.p4 b/testdata/p4_16_samples/issue3057-2.p4 new file mode 100644 index 0000000000..4689d0b580 --- /dev/null +++ b/testdata/p4_16_samples/issue3057-2.p4 @@ -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; diff --git a/testdata/p4_16_samples_outputs/issue3057-2-first.p4 b/testdata/p4_16_samples_outputs/issue3057-2-first.p4 new file mode 100644 index 0000000000..cafb8f71b0 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3057-2-first.p4 @@ -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; + diff --git a/testdata/p4_16_samples_outputs/issue3057-2-frontend.p4 b/testdata/p4_16_samples_outputs/issue3057-2-frontend.p4 new file mode 100644 index 0000000000..996095c390 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3057-2-frontend.p4 @@ -0,0 +1,14 @@ +struct S { + bit<32> a; + bit<32> b; +} + +control proto(); +package top(proto _p); +control c() { + apply { + } +} + +top(c()) main; + diff --git a/testdata/p4_16_samples_outputs/issue3057-2-midend.p4 b/testdata/p4_16_samples_outputs/issue3057-2-midend.p4 new file mode 100644 index 0000000000..996095c390 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3057-2-midend.p4 @@ -0,0 +1,14 @@ +struct S { + bit<32> a; + bit<32> b; +} + +control proto(); +package top(proto _p); +control c() { + apply { + } +} + +top(c()) main; + diff --git a/testdata/p4_16_samples_outputs/issue3057-2.p4 b/testdata/p4_16_samples_outputs/issue3057-2.p4 new file mode 100644 index 0000000000..1b8962988d --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3057-2.p4 @@ -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; + diff --git a/testdata/p4_16_samples_outputs/issue3057-2.p4-stderr b/testdata/p4_16_samples_outputs/issue3057-2.p4-stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/issue3057-2.p4.entries.txt b/testdata/p4_16_samples_outputs/issue3057-2.p4.entries.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/issue3057-2.p4.p4info.txt b/testdata/p4_16_samples_outputs/issue3057-2.p4.p4info.txt new file mode 100644 index 0000000000..9ec92493e4 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3057-2.p4.p4info.txt @@ -0,0 +1,3 @@ +pkg_info { + arch: "v1model" +}