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

Allow implicit conversion of brace-lists to type list #4599

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions frontends/p4/typeChecking/typeUnification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,19 @@ bool TypeUnification::unify(const BinaryConstraint *constraint) {
}
return true;
}
} else if (dest->is<IR::Type_P4List>() && src->is<IR::Type_P4List>()) {
auto dvec = dest->to<IR::Type_P4List>();
auto svec = src->to<IR::Type_P4List>();
constraints->add(constraint->create(dvec->elementType, svec->elementType));
return true;
} else if (auto dvec = dest->to<IR::Type_P4List>()) {
if (auto svec = src->to<IR::Type_P4List>()) {
constraints->add(constraint->create(dvec->elementType, svec->elementType));
return true;
} else if (auto svec = src->to<IR::Type_List>()) {
std::set<const IR::Type *> elTypes;
for (auto *t : svec->components) {
if (elTypes.count(t)) continue;
elTypes.insert(t);
Comment on lines +495 to +496
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid double-search:

Suggested change
if (elTypes.count(t)) continue;
elTypes.insert(t);
if (!elTypes.insert(t).second) continue;

constraints->add(constraint->create(dvec->elementType, t));
}
return true;
}
}

return constraint->reportError(constraints->getCurrentSubstitution());
Expand Down
14 changes: 14 additions & 0 deletions testdata/p4_16_samples/listinit.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({
{ 16w0, "foo" },
{ 16w1, "bar" },
{ 16w2, "baz" }
}) obj;

test<bit<16>, _>({}) o2;

test<bit<16>, bit<8>>({ { 0, 10}, { 1, 12 }, { 2, 15 } }) o3;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples_outputs/listinit-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj;
test<bit<16>, _>({ }) o2;
test<bit<16>, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples_outputs/listinit-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj;
test<bit<16>, _>({ }) o2;
test<bit<16>, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples_outputs/listinit.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj;
test<bit<16>, _>({ }) o2;
test<bit<16>, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3;
10 changes: 10 additions & 0 deletions testdata/p4_16_samples_outputs/listinit.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
listinit.p4(10): [--Wwarn=unused] warning: obj: unused instance
}) obj;
^^^
listinit.p4(12): [--Wwarn=unused] warning: o2: unused instance
test<bit<16>, _>({}) o2;
^^
listinit.p4(14): [--Wwarn=unused] warning: o3: unused instance
test<bit<16>, bit<8>>({ { 0, 10}, { 1, 12 }, { 2, 15 } }) o3;
^^
[--Wwarn=missing] warning: Program does not contain a `main' module