From f374988f770c3423b10b03afeff6cb7c924c4a12 Mon Sep 17 00:00:00 2001 From: Chris Dodd Date: Thu, 4 Apr 2024 23:31:17 +0000 Subject: [PATCH] Allow implicit conversion of brace-lists to type list --- frontends/p4/typeChecking/typeUnification.cpp | 18 +++++++++++++----- testdata/p4_16_samples/listinit.p4 | 14 ++++++++++++++ .../p4_16_samples_outputs/listinit-first.p4 | 7 +++++++ .../p4_16_samples_outputs/listinit-frontend.p4 | 7 +++++++ testdata/p4_16_samples_outputs/listinit.p4 | 7 +++++++ .../p4_16_samples_outputs/listinit.p4-stderr | 10 ++++++++++ 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 testdata/p4_16_samples/listinit.p4 create mode 100644 testdata/p4_16_samples_outputs/listinit-first.p4 create mode 100644 testdata/p4_16_samples_outputs/listinit-frontend.p4 create mode 100644 testdata/p4_16_samples_outputs/listinit.p4 create mode 100644 testdata/p4_16_samples_outputs/listinit.p4-stderr diff --git a/frontends/p4/typeChecking/typeUnification.cpp b/frontends/p4/typeChecking/typeUnification.cpp index 98bce43871c..aae3d5102f0 100644 --- a/frontends/p4/typeChecking/typeUnification.cpp +++ b/frontends/p4/typeChecking/typeUnification.cpp @@ -487,11 +487,19 @@ bool TypeUnification::unify(const BinaryConstraint *constraint) { } return true; } - } else if (dest->is() && src->is()) { - auto dvec = dest->to(); - auto svec = src->to(); - constraints->add(constraint->create(dvec->elementType, svec->elementType)); - return true; + } else if (auto dvec = dest->to()) { + if (auto svec = src->to()) { + constraints->add(constraint->create(dvec->elementType, svec->elementType)); + return true; + } else if (auto svec = src->to()) { + std::set elTypes; + for (auto *t : svec->components) { + if (elTypes.count(t)) continue; + elTypes.insert(t); + constraints->add(constraint->create(dvec->elementType, t)); + } + return true; + } } return constraint->reportError(constraints->getCurrentSubstitution()); diff --git a/testdata/p4_16_samples/listinit.p4 b/testdata/p4_16_samples/listinit.p4 new file mode 100644 index 00000000000..152ee3f5a54 --- /dev/null +++ b/testdata/p4_16_samples/listinit.p4 @@ -0,0 +1,14 @@ + +extern test { + test(list> pairs); +} + +test, _>({ + { 16w0, "foo" }, + { 16w1, "bar" }, + { 16w2, "baz" } +}) obj; + +test, _>({}) o2; + +test, bit<8>>({ { 0, 10}, { 1, 12 }, { 2, 15 } }) o3; diff --git a/testdata/p4_16_samples_outputs/listinit-first.p4 b/testdata/p4_16_samples_outputs/listinit-first.p4 new file mode 100644 index 00000000000..5915d633a9e --- /dev/null +++ b/testdata/p4_16_samples_outputs/listinit-first.p4 @@ -0,0 +1,7 @@ +extern test { + test(list> pairs); +} + +test, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj; +test, _>({ }) o2; +test, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3; diff --git a/testdata/p4_16_samples_outputs/listinit-frontend.p4 b/testdata/p4_16_samples_outputs/listinit-frontend.p4 new file mode 100644 index 00000000000..5915d633a9e --- /dev/null +++ b/testdata/p4_16_samples_outputs/listinit-frontend.p4 @@ -0,0 +1,7 @@ +extern test { + test(list> pairs); +} + +test, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj; +test, _>({ }) o2; +test, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3; diff --git a/testdata/p4_16_samples_outputs/listinit.p4 b/testdata/p4_16_samples_outputs/listinit.p4 new file mode 100644 index 00000000000..5915d633a9e --- /dev/null +++ b/testdata/p4_16_samples_outputs/listinit.p4 @@ -0,0 +1,7 @@ +extern test { + test(list> pairs); +} + +test, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj; +test, _>({ }) o2; +test, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3; diff --git a/testdata/p4_16_samples_outputs/listinit.p4-stderr b/testdata/p4_16_samples_outputs/listinit.p4-stderr new file mode 100644 index 00000000000..180e9bda066 --- /dev/null +++ b/testdata/p4_16_samples_outputs/listinit.p4-stderr @@ -0,0 +1,10 @@ +listinit.p4(10): [--Wwarn=unused] warning: obj: unused instance +}) obj; + ^^^ +listinit.p4(12): [--Wwarn=unused] warning: o2: unused instance +test, _>({}) o2; + ^^ +listinit.p4(14): [--Wwarn=unused] warning: o3: unused instance +test, bit<8>>({ { 0, 10}, { 1, 12 }, { 2, 15 } }) o3; + ^^ +[--Wwarn=missing] warning: Program does not contain a `main' module