Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3782,9 +3782,17 @@ const IR::Node* TypeInference::postorder(IR::SwitchStatement* stat) {
if (isCompileTimeConstant(stat->expression))
warn(ErrorType::WARN_MISMATCH, "%1%: constant expression in switch", stat->expression);

std::map<big_int, const IR::Node*> caselabels;
for (auto& c : stat->cases) {
if (!isCompileTimeConstant(c->label))
typeError("%1%: must be a compile-time constant", c->label);
if (auto cst = c->label->to<IR::Constant>()) {
auto it = caselabels.find(cst->value);
if (it != caselabels.end()) {
typeError("%1%: 'switch' label duplicates %2%", c->label, it->second);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why switch in single quote?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Similar to the error message for switch with action_run expression

}
caselabels.emplace(cst->value, c->label);
}
auto lt = getType(c->label);
if (lt == nullptr) continue;
if (lt->is<IR::Type_InfInt>() && type->is<IR::Type_Bits>()) {
Expand Down
17 changes: 17 additions & 0 deletions testdata/p4_16_errors/duplicate-switch-case.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://github.com/p4lang/p4c/issues/3612
#include <core.p4>

control c(in bit<4> a)
{
apply {
switch (a) {
4w0: {}
4w1: {} // { dg-note "" }
4w1: {} // { dg-error "" }
}
}
}

control ct(in bit<4> a);
package pt(ct t);
pt(c()) main;
18 changes: 18 additions & 0 deletions testdata/p4_16_errors_outputs/duplicate-switch-case.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <core.p4>

control c(in bit<4> a) {
apply {
switch (a) {
4w0: {
}
4w1: {
}
4w1: {
}
}
}
}

control ct(in bit<4> a);
package pt(ct t);
pt(c()) main;
6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/duplicate-switch-case.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
duplicate-switch-case.p4(10): [--Werror=type-error] error: 4w1: 'switch' label duplicates 4w1
4w1: {} // { dg-error "" }
^^^
duplicate-switch-case.p4(9)
4w1: {} // { dg-note "" }
^^^