Skip to content

Commit 53acdd6

Browse files
authored
Do not emit a warning when a switch expression is constant (#5314)
Signed-off-by: Kyle Cripps <[email protected]>
1 parent 7247c11 commit 53acdd6

13 files changed

+127
-29
lines changed

frontends/p4/typeChecking/typeCheckStmt.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ const IR::Node *TypeInferenceBase::postorder(const IR::SwitchStatement *stat) {
5858
// switch (expression)
5959
Comparison comp;
6060
comp.left = stat->expression;
61-
if (isCompileTimeConstant(stat->expression))
62-
warn(ErrorType::WARN_MISMATCH, "%1%: constant expression in switch", stat->expression);
6361

6462
auto *sclone = stat->clone();
6563
bool changed = false;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void bar(in bit<2> x, out bit<8> y) {
2+
switch (x) {
3+
2w0b1: {
4+
y = 8w2;
5+
}
6+
2w0b10: {
7+
y = 8w3;
8+
}
9+
}
10+
}
11+
control C(out bit<8> y) {
12+
action foo() {
13+
bar(2w1, y);
14+
}
15+
table t {
16+
actions = {
17+
foo();
18+
}
19+
default_action = foo();
20+
}
21+
apply {
22+
t.apply();
23+
}
24+
}
25+
26+
control proto(out bit<8> y);
27+
package top(proto p);
28+
top(C()) main;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
control C(out bit<8> y) {
2+
@name("C.x_0") bit<2> x;
3+
@name("C.y_0") bit<8> y_1;
4+
@name("C.foo") action foo() {
5+
x = 2w1;
6+
switch (x) {
7+
2w0b1: {
8+
y_1 = 8w2;
9+
}
10+
2w0b10: {
11+
y_1 = 8w3;
12+
}
13+
}
14+
y = y_1;
15+
}
16+
@name("C.t") table t_0 {
17+
actions = {
18+
foo();
19+
}
20+
default_action = foo();
21+
}
22+
apply {
23+
t_0.apply();
24+
}
25+
}
26+
27+
control proto(out bit<8> y);
28+
package top(proto p);
29+
top(C()) main;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void bar(in bit<2> x, out bit<8> y) {
2+
switch (x) {
3+
0b1: {
4+
y = 2;
5+
}
6+
0b10: {
7+
y = 3;
8+
}
9+
}
10+
}
11+
control C(out bit<8> y) {
12+
action foo() {
13+
bar(1, y);
14+
}
15+
table t {
16+
actions = {
17+
foo;
18+
}
19+
default_action = foo;
20+
}
21+
apply {
22+
t.apply();
23+
}
24+
}
25+
26+
control proto(out bit<8> y);
27+
package top(proto p);
28+
top(C()) main;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
no_warn_const_switch_expr.p4(1): [--Wwarn=uninitialized_out_param] warning: out parameter 'y' may be uninitialized when 'bar' terminates
2+
void bar(in bit<2> x, out bit<8> y) {
3+
^
4+
no_warn_const_switch_expr.p4(1)
5+
void bar(in bit<2> x, out bit<8> y) {
6+
^^^
7+
[--Wwarn=uninitialized_use] warning: y_0 may be uninitialized
8+
no_warn_const_switch_expr.p4(2): error: SwitchStatement: switch statements not supported in actions on this target
9+
switch (x) {
10+
^^^^^^

testdata/p4_16_samples_outputs/issue2617.p4-stderr

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
issue2617.p4(64): [--Wwarn=mismatch] warning: 32w1: constant expression in switch
2-
switch (32w1) {
3-
^^^^
4-
issue2617.p4(70): [--Wwarn=mismatch] warning: E.C: constant expression in switch
5-
switch (E.C) {
6-
^^^
7-
issue2617.p4(75): [--Wwarn=mismatch] warning: E.C: constant expression in switch
8-
switch (E.C) {
9-
^^^
10-
issue2617.p4(80): [--Wwarn=mismatch] warning: X.B: constant expression in switch
11-
switch (X.B) {
12-
^^^
131
issue2617.p4(17): [--Wwarn=parser-transition] warning: SelectCase: unreachable case
142
2: two;
153
^^^^^^
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
issue4661_pure_extern_function_const_args.p4(13): [--Wwarn=mismatch] warning: baz(): constant expression in switch
2-
switch(baz()) {
3-
^^^^^
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
pragma-string.p4(20): [--Wwarn=unused] warning: 'original' is unused
2+
const bit b = 1;
3+
^
4+
pragma-string.p4(23): [--Wwarn=unused] warning: 'string \" with \" quotes' is unused
5+
const bit c = 1;
6+
^
7+
pragma-string.p4(27): [--Wwarn=unused] warning: 'string with
8+
newline' is unused
9+
const bit d = 1;
10+
^
11+
pragma-string.p4(31): [--Wwarn=unused] warning: 'string with quoted newline' is unused
12+
const bit e = 1;
13+
^
14+
pragma-string.p4(34): [--Wwarn=unused] warning: '8-bit string ⟶' is unused
15+
const bit f = 1;
16+
^
117
[--Wwarn=missing] warning: Program does not contain a `main' module
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
psa-example-switch-with-constant-expr.p4(65): [--Wwarn=mismatch] warning: 16w2: constant expression in switch
2-
switch (16w2) {
3-
^^^^
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
psa-example-switch-with-constant-expr.p4(65): [--Wwarn=mismatch] warning: 16w2: constant expression in switch
2-
switch (16w2) {
3-
^^^^

0 commit comments

Comments
 (0)