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

Do not allow variables with type Type_Parser/Control #3361

Merged
merged 2 commits into from
May 27, 2022
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
5 changes: 3 additions & 2 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,9 @@ const IR::Node* TypeInference::postorder(IR::Declaration_Variable* decl) {
const IR::Type* baseType = type;
if (auto sc = type->to<IR::Type_SpecializedCanonical>())
baseType = sc->baseType;
if (baseType->is<IR::IContainer>() || baseType->is<IR::Type_Extern>()) {
typeError("%1%: cannot declare variables of type %2% (consider using an instantiation)",
if (baseType->is<IR::IContainer>() || baseType->is<IR::Type_Extern>() ||
baseType->is<IR::Type_Parser>() || baseType->is<IR::Type_Control>()) {
typeError("%1%: cannot declare variables of type '%2%' (consider using an instantiation)",
decl, type);
return decl;
}
Expand Down
36 changes: 36 additions & 0 deletions testdata/p4_16_errors/issue3359.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
parser mypt(in bit tt);
package mypack(mypt t);

parser MyParser(in bit tt) {
state start {
transition select(tt) {
0: accept;
_: reject;
}
}
}

parser MyParser1(in bit tt) {
mypt t = MyParser();
state start {
t.apply(tt);
transition select(tt) {
0: accept;
_: reject;
}
}
}

parser MyParser2(in bit tt) {
mypt t;
state start {
t = MyParser();
t.apply(tt);
transition select(tt) {
0: accept;
_: reject;
}
}
}

mypack(MyParser1()) main;
36 changes: 36 additions & 0 deletions testdata/p4_16_errors_outputs/issue3359.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
parser mypt(in bit<1> tt);
package mypack(mypt t);
parser MyParser(in bit<1> tt) {
state start {
transition select(tt) {
0: accept;
default: reject;
}
}
}

parser MyParser1(in bit<1> tt) {
mypt t = MyParser();
state start {
t.apply(tt);
transition select(tt) {
0: accept;
default: reject;
}
}
}

parser MyParser2(in bit<1> tt) {
mypt t;
state start {
t = MyParser();
t.apply(tt);
transition select(tt) {
0: accept;
default: reject;
}
}
}

mypack(MyParser1()) main;

12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue3359.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
issue3359.p4(14): [--Werror=type-error] error: t: cannot declare variables of type 'parser mypt' (consider using an instantiation)
mypt t = MyParser();
^^^^^^^^^^^^^^^^^^^^
issue3359.p4(1)
parser mypt(in bit tt);
^^^^
issue3359.p4(25): [--Werror=type-error] error: t: cannot declare variables of type 'parser mypt' (consider using an instantiation)
mypt t;
^^^^^^^
issue3359.p4(1)
parser mypt(in bit tt);
^^^^
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue394.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue394.p4(19): [--Werror=type-error] error: c: cannot declare variables of type C (consider using an instantiation)
issue394.p4(19): [--Werror=type-error] error: c: cannot declare variables of type 'C' (consider using an instantiation)
C c;
^^^^
issue394.p4(16)
Expand Down
12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue807.p4-stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ issue807.p4(20): [--Werror=type-error] error: e.get2: illegal return type contro
C2 c2 = e.get2();
^^^^^^^^
issue807.p4(5)
control C2();
^^
issue807.p4(20): [--Werror=type-error] error: c2: cannot declare variables of type 'control C2' (consider using an instantiation)
C2 c2 = e.get2();
^^^^^^^^^^^^^^^^^
issue807.p4(5)
control C2();
^^
issue807.p4(26): [--Werror=type-error] error: e.get1: illegal return type control C1
Expand All @@ -10,3 +16,9 @@ issue807.p4(26): [--Werror=type-error] error: e.get1: illegal return type contro
issue807.p4(4)
control C1();
^^
issue807.p4(26): [--Werror=type-error] error: c1: cannot declare variables of type 'control C1' (consider using an instantiation)
C1 c1 = e.get1();
^^^^^^^^^^^^^^^^^
issue807.p4(4)
control C1();
^^
12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue816-1.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
issue816-1.p4(14): [--Werror=type-error] error: p: parameter cannot be a package
control MyC1()(P p) {
^
issue816-1.p4(25): [--Werror=type-error] error: c1: cannot declare variables of type 'control C' (consider using an instantiation)
C c1 = MyC1(p);
^^^^^^^^^^^^^^^
issue816-1.p4(4)
control C();
^
issue816-1.p4(18): [--Werror=type-error] error: p: parameter cannot be a package
control MyC2()(P p) {
^
issue816-1.p4(26): [--Werror=type-error] error: c2: cannot declare variables of type 'control C' (consider using an instantiation)
C c2 = MyC2(p);
^^^^^^^^^^^^^^^
issue816-1.p4(4)
control C();
^