diff --git a/frontends/p4/simplifyDefUse.cpp b/frontends/p4/simplifyDefUse.cpp index 033100f3ba6..1d5c082f2cb 100644 --- a/frontends/p4/simplifyDefUse.cpp +++ b/frontends/p4/simplifyDefUse.cpp @@ -714,9 +714,10 @@ class FindUninitialized : public Inspector { if (auto list = src->to()) { auto it = list->components.begin(); for (auto field : dst_struct->fields) { - IR::Member member(dst, field->name); - processHeadersInAssignment(&member, (*it)->expression, - typeMap->getType(field, true), + auto ftype = typeMap->getType(field, true); + auto member = new IR::Member(dst, field->name); + typeMap->setType(member, ftype); + processHeadersInAssignment(member, (*it)->expression, ftype, typeMap->getType((*it)->expression, true)); ++it; } @@ -727,11 +728,12 @@ class FindUninitialized : public Inspector { } } else if (src_type->to()) { for (auto field : dst_struct->fields) { - IR::Member dst_member(dst, field->name); - IR::Member src_member(src, field->name); - processHeadersInAssignment(&dst_member, &src_member, - typeMap->getType(field, true), - typeMap->getType(field, true)); + auto ftype = typeMap->getType(field, true); + auto dst_member = new IR::Member(dst, field->name); + auto src_member = new IR::Member(src, field->name); + typeMap->setType(dst_member, ftype); + typeMap->setType(src_member, ftype); + processHeadersInAssignment(dst_member, src_member, ftype, ftype); } } else { BUG("%1%: unexpected expression on RHS", src); @@ -752,11 +754,14 @@ class FindUninitialized : public Inspector { false; for (auto field : dst_headerunion->fields) { - IR::Member dst_member(dst, field->name); - IR::Member src_member(src, field->name); - auto valid = headerDefs->find(&src_member); + auto ftype = typeMap->getType(field, true); + auto dst_member = new IR::Member(dst, field->name); + auto src_member = new IR::Member(src, field->name); + typeMap->setType(dst_member, ftype); + typeMap->setType(src_member, ftype); + auto valid = headerDefs->find(src_member); if (!non_constant_indexing || valid == TernaryBool::Yes) - headerDefs->update(headerDefs->getStorageLocation(&dst_member), + headerDefs->update(headerDefs->getStorageLocation(dst_member), valid); } auto valid = headerDefs->find(src); diff --git a/testdata/p4_16_samples/issue2958.p4 b/testdata/p4_16_samples/issue2958.p4 new file mode 100644 index 00000000000..8ae39023834 --- /dev/null +++ b/testdata/p4_16_samples/issue2958.p4 @@ -0,0 +1,29 @@ +#include + +header ethernet_t { + bit<48> dst_addr; +} + +struct G { + ethernet_t eth; +} + +struct H { + G g; +} + +struct Headers { + ethernet_t eth_hdr; +} + +control ingress(inout Headers h) { + H tmp = { { { 1 } } }; + + apply { + tmp.g.eth.dst_addr = 1; + } +} + +control Ingress(inout Headers hdr); +package top(Ingress ig); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/issue2958-first.p4 b/testdata/p4_16_samples_outputs/issue2958-first.p4 new file mode 100644 index 00000000000..06573010529 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue2958-first.p4 @@ -0,0 +1,29 @@ +#include + +header ethernet_t { + bit<48> dst_addr; +} + +struct G { + ethernet_t eth; +} + +struct H { + G g; +} + +struct Headers { + ethernet_t eth_hdr; +} + +control ingress(inout Headers h) { + H tmp = (H){g = (G){eth = (ethernet_t){dst_addr = 48w1}}}; + apply { + tmp.g.eth.dst_addr = 48w1; + } +} + +control Ingress(inout Headers hdr); +package top(Ingress ig); +top(ingress()) main; + diff --git a/testdata/p4_16_samples_outputs/issue2958-frontend.p4 b/testdata/p4_16_samples_outputs/issue2958-frontend.p4 new file mode 100644 index 00000000000..4267a1617d8 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue2958-frontend.p4 @@ -0,0 +1,27 @@ +#include + +header ethernet_t { + bit<48> dst_addr; +} + +struct G { + ethernet_t eth; +} + +struct H { + G g; +} + +struct Headers { + ethernet_t eth_hdr; +} + +control ingress(inout Headers h) { + apply { + } +} + +control Ingress(inout Headers hdr); +package top(Ingress ig); +top(ingress()) main; + diff --git a/testdata/p4_16_samples_outputs/issue2958-midend.p4 b/testdata/p4_16_samples_outputs/issue2958-midend.p4 new file mode 100644 index 00000000000..4267a1617d8 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue2958-midend.p4 @@ -0,0 +1,27 @@ +#include + +header ethernet_t { + bit<48> dst_addr; +} + +struct G { + ethernet_t eth; +} + +struct H { + G g; +} + +struct Headers { + ethernet_t eth_hdr; +} + +control ingress(inout Headers h) { + apply { + } +} + +control Ingress(inout Headers hdr); +package top(Ingress ig); +top(ingress()) main; + diff --git a/testdata/p4_16_samples_outputs/issue2958.p4 b/testdata/p4_16_samples_outputs/issue2958.p4 new file mode 100644 index 00000000000..4b17fb2f5b9 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue2958.p4 @@ -0,0 +1,29 @@ +#include + +header ethernet_t { + bit<48> dst_addr; +} + +struct G { + ethernet_t eth; +} + +struct H { + G g; +} + +struct Headers { + ethernet_t eth_hdr; +} + +control ingress(inout Headers h) { + H tmp = { { { 1 } } }; + apply { + tmp.g.eth.dst_addr = 1; + } +} + +control Ingress(inout Headers hdr); +package top(Ingress ig); +top(ingress()) main; + diff --git a/testdata/p4_16_samples_outputs/issue2958.p4-stderr b/testdata/p4_16_samples_outputs/issue2958.p4-stderr new file mode 100644 index 00000000000..e69de29bb2d