diff --git a/backends/bmv2/jsonconverter.cpp b/backends/bmv2/jsonconverter.cpp index cd3c22577a..b01e0e6cdc 100644 --- a/backends/bmv2/jsonconverter.cpp +++ b/backends/bmv2/jsonconverter.cpp @@ -1995,6 +1995,11 @@ void JsonConverter::addTypesAndInstances(const IR::Type_StructLike* type, bool m for (auto f : *type->fields) { auto ft = typeMap->getType(f, true); if (ft->is()) { + // The headers struct can not contain nested structures. + if (!meta && !ft->is()) { + ::error("Type %1% should only contain headers or header stacks", type); + return; + } auto st = ft->to(); createJsonType(st); } @@ -2013,10 +2018,6 @@ void JsonConverter::addTypesAndInstances(const IR::Type_StructLike* type, bool m // Done elsewhere continue; } else { - if (!meta) { - ::error("Type %1% should only contain headers or stacks", type); - return; - } // Treat this field like a scalar local variable auto scalarFields = scalarsStruct->get("fields")->to(); CHECK_NULL(scalarFields); diff --git a/testdata/p4_16_bmv_errors/README.md b/testdata/p4_16_bmv_errors/README.md index fa5c297b5f..a179981cde 100644 --- a/testdata/p4_16_bmv_errors/README.md +++ b/testdata/p4_16_bmv_errors/README.md @@ -1 +1,2 @@ This folder contains negative tests: which are supposed to generate compiler errors when compiled. +All files should be named like *-bmv2.p4 diff --git a/testdata/p4_16_bmv_errors/nested-headers-bmv2.p4 b/testdata/p4_16_bmv_errors/nested-headers-bmv2.p4 new file mode 100644 index 0000000000..9fa85cac75 --- /dev/null +++ b/testdata/p4_16_bmv_errors/nested-headers-bmv2.p4 @@ -0,0 +1,54 @@ +/* +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. +*/ + +#include +#include + +header foo { + bit<8> foo_1; + bit<7> foo_2; + bit<8> foo_3; +} + +header bar { + bit<2> bar_1; +} + +struct baz { + foo baz_1; +} + +struct Headers { + bar a; + baz b; +} + +struct Meta {} + +parser p(packet_in b, out Headers h, inout Meta m, inout standard_metadata_t sm) { + state start { + transition accept; + } +} + +control vrfy(in Headers h, inout Meta m) { apply {} } +control update(inout Headers h, inout Meta m) { apply {} } + +control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { apply {} } +control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { apply {} } +control deparser(packet_out b, in Headers h) { apply {} } + +V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main;