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

Forbid case-after-default, tweak error messages #4831

Merged
merged 4 commits into from
Jul 29, 2024
Merged
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
9 changes: 5 additions & 4 deletions frontends/p4/validateParsedProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ void ValidateParsedProgram::postorder(const IR::SwitchStatement *statement) {
for (auto c : statement->cases) {
if (defaultFound != nullptr) {
if (c->label->is<IR::DefaultExpression>())
::error(ErrorType::ERR_INVALID, "%1% has multiple 'default' labels: %2% and %3%.",
statement, defaultFound->label, c->label);
::error(ErrorType::ERR_INVALID, "%1%switch has multiple 'default' labels:%2%%3%",
statement->srcInfo, defaultFound->label->srcInfo, c->label->srcInfo);
else
warn(ErrorType::WARN_ORDERING, "%1%: label following 'default' %2% label.",
c->label, defaultFound->label);
::error(ErrorType::ERR_INVALID,
"%1%switch label %2% follows 'default' label, which is not allowed.%3%",
statement->srcInfo, c->label, defaultFound->label->srcInfo);
break;
}
if (c->label->is<IR::DefaultExpression>()) defaultFound = c;
Expand Down
16 changes: 16 additions & 0 deletions testdata/p4_16_errors/default-last-switch.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
control ctrl() {
action a() {}
action b() {}

table t {
actions = { a; b; }
default_action = a;
}

apply {
switch (t.apply().action_run) {
default:
b: { return; }
}
}
}
3 changes: 1 addition & 2 deletions testdata/p4_16_errors/issue3611.p4
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This issue apparently has nothing to do with case-after-default, and it just masks the "real" problem (the one the issue referred to).

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ control c()
}
apply {
switch(t.apply().action_run) {
default: {}
1: {} // { dg-error "" }
default: {}
a: {}
a: {}
default: {}
}
}
}
22 changes: 22 additions & 0 deletions testdata/p4_16_errors_outputs/default-last-switch.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
control ctrl() {
action a() {
}
action b() {
}
table t {
actions = {
a;
b;
}
default_action = a;
}
apply {
switch (t.apply().action_run) {
default:
b: {
return;
}
}
}
}

9 changes: 9 additions & 0 deletions testdata/p4_16_errors_outputs/default-last-switch.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default-last-switch.p4(11): [--Werror=invalid] error: switch label b follows 'default' label, which is not allowed.
switch (t.apply().action_run) {
^^^^^^
default-last-switch.p4(13)
b: { return; }
^
default-last-switch.p4(12)
default:
^^^^^^^
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue2525.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue2525.p4(64): [--Werror=invalid] error: SwitchStatement has multiple 'default' labels: DefaultExpression and DefaultExpression.
issue2525.p4(64): [--Werror=invalid] error: switch has multiple 'default' labels:
switch (t.apply().action_run) {
^^^^^^
issue2525.p4(66)
Expand Down
6 changes: 2 additions & 4 deletions testdata/p4_16_errors_outputs/issue3611.p4
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ control c() {
}
apply {
switch (t.apply().action_run) {
default: {
}
1: {
}
default: {
}
a: {
}
a: {
}
default: {
}
}
}
}
Expand Down
18 changes: 3 additions & 15 deletions testdata/p4_16_errors_outputs/issue3611.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
issue3611.p4(13): [--Wwarn=ordering] warning: 1: label following 'default' DefaultExpression label.
issue3611.p4(12): [--Werror=type-error] error: 1: 'switch' label must be an action name or 'default'
1: {} // { dg-error "" }
^
issue3611.p4(12)
default: {}
^^^^^^^
issue3611.p4(13): [--Werror=type-error] error: 1: 'switch' label must be an action name or 'default'
1: {} // { dg-error "" }
^
issue3611.p4(14): [--Werror=type-error] error: DefaultExpression: multiple 'default' labels DefaultExpression
default: {}
^^^^^^^
issue3611.p4(12)
default: {}
^^^^^^^
issue3611.p4(16): [--Werror=type-error] error: a: 'switch' label duplicates a
issue3611.p4(14): [--Werror=type-error] error: a: 'switch' label duplicates a
a: {}
^
issue3611.p4(15)
issue3611.p4(13)
a: {}
^
6 changes: 3 additions & 3 deletions testdata/p4_16_samples/default-switch.p4
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ control ctrl() {

apply {
switch (t.apply().action_run) {
default:
b: { return; }
b:
default: { return; }
}
}
}
}
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/default-switch-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ control ctrl() {
}
apply {
switch (t.apply().action_run) {
default:
b: {
b:
default: {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/default-switch.p4
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ control ctrl() {
}
apply {
switch (t.apply().action_run) {
default:
b: {
b:
default: {
return;
}
}
Expand Down
6 changes: 0 additions & 6 deletions testdata/p4_16_samples_outputs/default-switch.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
default-switch.p4(13): [--Wwarn=ordering] warning: b: label following 'default' DefaultExpression label.
b: { return; }
^
default-switch.p4(12)
default:
^^^^^^^
default-switch.p4(1): [--Wwarn=unused] warning: Control ctrl is not used; removing
control ctrl() {
^^^^
Expand Down
Loading