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

Improve error message when shifting int by non-const #4653

Merged
merged 4 commits into from
May 7, 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: 7 additions & 2 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2684,8 +2684,13 @@ const IR::Node *TypeInference::shift(const IR::Operation_Binary *expression) {
return expression;
}

if (ltype->is<IR::Type_InfInt>() && !rtype->is<IR::Type_InfInt>()) {
typeError("%1%: width of left operand of shift needs to be specified", expression);
if (ltype->is<IR::Type_InfInt>() && !rtype->is<IR::Type_InfInt>() &&
!isCompileTimeConstant(expression->right)) {
typeError(
"%1%: shift result type is arbitrary-precision int, but right operand is not constant; "
"width of left operand of shift needs to be specified or both operands need to be "
"constant",
expression);
return expression;
}

Expand Down
15 changes: 15 additions & 0 deletions testdata/p4_16_errors/shift-int-non-const.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr, inout bit<4> b) {
apply {
const int a = 5;
hdr.v = (bit<8>)(a >> b);
}
}

control C(inout hdr_t hdr, inout bit<4> b);
package P(C c);

P(c()) main;
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue2206.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
issue2206.p4(27): [--Werror=type-error] error: 1 << h.h.c: width of left operand of shift needs to be specified
issue2206.p4(27): [--Werror=type-error] error: 1 << h.h.c: shift result type is arbitrary-precision int, but right operand is not constant; width of left operand of shift needs to be specified or both operands need to be constant
h.h.a = (1 << h.h.c) + 8w2;
^^^^^^^^^^
14 changes: 14 additions & 0 deletions testdata/p4_16_errors_outputs/shift-int-non-const.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr, inout bit<4> b) {
apply {
const int a = 5;
hdr.v = (bit<8>)(a >> b);
}
}

control C(inout hdr_t hdr, inout bit<4> b);
package P(C c);
P(c()) main;
3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/shift-int-non-const.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shift-int-non-const.p4(8): [--Werror=type-error] error: 5 >> b: shift result type is arbitrary-precision int, but right operand is not constant; width of left operand of shift needs to be specified or both operands need to be constant
hdr.v = (bit<8>)(a >> b);
^^^^^^
16 changes: 16 additions & 0 deletions testdata/p4_16_samples/shift-int-const.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr) {
apply {
const int a = 5;
const bit<4> b = 2;
hdr.v = (bit<8>)(a >> b);
}
}

control C(inout hdr_t hdr);
package P(C c);

P(c()) main;
15 changes: 15 additions & 0 deletions testdata/p4_16_samples_outputs/shift-int-const-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr) {
apply {
const int a = 5;
const bit<4> b = 4w2;
hdr.v = 8w1;
}
}

control C(inout hdr_t hdr);
package P(C c);
P(c()) main;
13 changes: 13 additions & 0 deletions testdata/p4_16_samples_outputs/shift-int-const-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr) {
apply {
hdr.v = 8w1;
}
}

control C(inout hdr_t hdr);
package P(C c);
P(c()) main;
22 changes: 22 additions & 0 deletions testdata/p4_16_samples_outputs/shift-int-const-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr) {
@hidden action shiftintconst9() {
hdr.v = 8w1;
}
@hidden table tbl_shiftintconst9 {
actions = {
shiftintconst9();
}
const default_action = shiftintconst9();
}
apply {
tbl_shiftintconst9.apply();
}
}

control C(inout hdr_t hdr);
package P(C c);
P(c()) main;
15 changes: 15 additions & 0 deletions testdata/p4_16_samples_outputs/shift-int-const.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
header hdr_t {
bit<8> v;
}

control c(inout hdr_t hdr) {
apply {
const int a = 5;
const bit<4> b = 2;
hdr.v = (bit<8>)(a >> b);
}
}

control C(inout hdr_t hdr);
package P(C c);
P(c()) main;
Empty file.
Loading