Skip to content

Commit

Permalink
Forbid use of .lastIndex outside of parsers (p4lang#3523)
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>

Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
Mihai Budiu authored Sep 10, 2022
1 parent c5b9873 commit 14c56cf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 5 deletions.
11 changes: 9 additions & 2 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3117,8 +3117,9 @@ const IR::Node* TypeInference::postorder(IR::Member* expression) {
if (member == IR::Type_Stack::next ||
member == IR::Type_Stack::last) {
if (parser == nullptr) {
typeError("%1%: 'last' and 'next' for stacks can only be used in a parser",
expression);
typeError(
"%1%: 'last', and 'next' for stacks can only be used in a parser",
expression);
return expression;
}
auto stack = type->to<IR::Type_Stack>();
Expand All @@ -3134,6 +3135,12 @@ const IR::Node* TypeInference::postorder(IR::Member* expression) {
setType(expression, IR::Type_Bits::get(32));
return expression;
} else if (member == IR::Type_Stack::lastIndex) {
if (parser == nullptr) {
typeError(
"%1%: 'lastIndex' for stacks can only be used in a parser",
expression);
return expression;
}
setType(getOriginal(), IR::Type_Bits::get(32, false));
setType(expression, IR::Type_Bits::get(32, false));
return expression;
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_errors_outputs/issue1853.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
issue1853.p4(26): [--Werror=type-error] error: hdr.value.last: 'last' and 'next' for stacks can only be used in a parser
issue1853.p4(26): [--Werror=type-error] error: hdr.value.last: 'last', and 'next' for stacks can only be used in a parser
modify_field(value[last].bos, 0);
^^^^^^^^^^^
11 changes: 11 additions & 0 deletions testdata/p4_16_errors/issue3522.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
header h {
bit field;
};

struct s {
h[2] field;
};

action a(in s v) {
bit<32> t = v.field.lastIndex;
}
11 changes: 11 additions & 0 deletions testdata/p4_16_errors_outputs/issue3522.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
header h {
bit<1> field;
}

struct s {
h[2] field;
}

action a(in s v) {
bit<32> t = v.field.lastIndex;
}
3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/issue3522.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
issue3522.p4(10): [--Werror=type-error] error: v.field.lastIndex: 'lastIndex' for stacks can only be used in a parser
bit<32> t = v.field.lastIndex;
^^^^^^^^^^^^^^^^^
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/next.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
next.p4(24): [--Werror=type-error] error: stack.last: 'last' and 'next' for stacks can only be used in a parser
next.p4(24): [--Werror=type-error] error: stack.last: 'last', and 'next' for stacks can only be used in a parser
stack.last = { 10 };
^^^^^^^^^^
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/stack2.p4-stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ stack2.p4(22)
stack2.p4(21)
h[5] stack1;
^^^^
stack2.p4(25): [--Werror=type-error] error: Expression stack1.lastIndex cannot be the target of an assignment
stack2.p4(25): [--Werror=type-error] error: stack1.lastIndex: 'lastIndex' for stacks can only be used in a parser
stack1.lastIndex = 3; // not an l-value
^^^^^^^^^^^^^^^^
stack2.p4(26): [--Werror=type-error] error: Expression stack2.size cannot be the target of an assignment
Expand Down

0 comments on commit 14c56cf

Please sign in to comment.