Skip to content

Commit

Permalink
Do not allow arguments with type table, control, etc (#3901)
Browse files Browse the repository at this point in the history
* Do not allow arguments with type table, control, etc
Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
  • Loading branch information
Mihai Budiu authored Mar 3, 2023
1 parent 9eeba09 commit facc5ed
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 61 deletions.
22 changes: 21 additions & 1 deletion frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ const IR::Type *TypeInference::canonicalize(const IR::Type *type) {
if (auto tt = type->to<IR::Type_Type>()) type = tt->type;

if (type->is<IR::Type_SpecializedCanonical>() || type->is<IR::Type_InfInt>() ||
type->is<IR::Type_Action>() || type->is<IR::Type_Error>() || type->is<IR::Type_Newtype>()) {
type->is<IR::Type_Action>() || type->is<IR::Type_Error>() || type->is<IR::Type_Newtype>() ||
type->is<IR::Type_Table>()) {
return type;
} else if (type->is<IR::Type_Dontcare>()) {
return IR::Type_Dontcare::get();
Expand Down Expand Up @@ -607,6 +608,11 @@ const IR::Node *TypeInference::postorder(IR::Declaration_MatchKind *decl) {
return decl;
}

const IR::Node *TypeInference::postorder(IR::Type_Table *type) {
(void)setTypeType(type);
return type;
}

const IR::Node *TypeInference::postorder(IR::P4Table *table) {
currentActionList = nullptr;
if (done()) return table;
Expand Down Expand Up @@ -3590,6 +3596,20 @@ const IR::Node *TypeInference::postorder(IR::MethodCallExpression *expression) {
param = *paramIt;
}

if (param->type->is<IR::Type_Dontcare>())
typeError(
"%1%: Could not infer a type for parameter %2% "
"(inferred type is don't care '_')",
arg, param);

// By calling generic functions with don't care parameters
// we can force parameters to have illegal types. Check here for this case.
// e.g., void f<T>(in T arg); table t { }; f(t);
if (param->type->is<IR::Type_Table>() || param->type->is<IR::Type_Action>() ||
param->type->is<IR::Type_Control>() || param->type->is<IR::Type_Package>() ||
param->type->is<IR::Type_Parser>())
typeError("%1%: argument cannot have type %2%", arg, param->type);

auto newExpr = arg->expression;
if (param->direction == IR::Direction::In) {
// This is like an assignment; may make additional conversions.
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/typeChecking/typeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class TypeInference : public Transform {
const IR::Node *postorder(IR::Method *method) override;

const IR::Node *postorder(IR::Type_Type *type) override;
const IR::Node *postorder(IR::Type_Table *type) override;
const IR::Node *postorder(IR::Type_Error *decl) override;
const IR::Node *postorder(IR::Type_InfInt *type) override;
const IR::Node *postorder(IR::Type_Method *type) override;
Expand Down
17 changes: 17 additions & 0 deletions testdata/p4_16_errors/issue3808-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
action NoAction(){}

extern f<T>
{
f();
void g(in T a);
}
control eq0(f<_> tt)
{
table t {
const size = 100;
actions = {NoAction;}
}
apply {
tt.g(t);
}
}
13 changes: 13 additions & 0 deletions testdata/p4_16_errors/issue3808.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
action NoAction(){}

extern void f<T>(in T a);
control eq0()
{
table t {
const size = 100;
actions = {NoAction;}
}
apply {
f<_>(t);
}
}
File renamed without changes.
9 changes: 3 additions & 6 deletions testdata/p4_16_errors_outputs/issue2454.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
issue2454.p4(3): [--Werror=type-error] error: t: parameter cannot have type control C
void f<T>(T t) {}
^
issue2454.p4(8): [--Werror=type-error] error: c: argument cannot have type control C
f(c);
^
issue2454.p4(1)
control C() { apply { } }
^
issue2454.p4(3): [--Werror=type-error] error: Error while analyzing f
void f<T>(T t) {}
^
19 changes: 19 additions & 0 deletions testdata/p4_16_errors_outputs/issue3808-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
action NoAction() {
}
extern f<T> {
f();
void g(in T a);
}

control eq0(f<_> tt) {
table t {
const size = 100;
actions = {
NoAction;
}
}
apply {
tt.g(t);
}
}

6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/issue3808-1.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issue3808-1.p4(15): [--Werror=type-error] error: t: Could not infer a type for parameter a (inferred type is don't care '_')
tt.g(t);
^
issue3808-1.p4(6)
void g(in T a);
^
15 changes: 15 additions & 0 deletions testdata/p4_16_errors_outputs/issue3808.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
action NoAction() {
}
extern void f<T>(in T a);
control eq0() {
table t {
const size = 100;
actions = {
NoAction;
}
}
apply {
f<_>(t);
}
}

3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/issue3808.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
issue3808.p4(11): [--Werror=type-error] error: t: argument cannot have type Type_Table
f<_>(t);
^
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 @@ -22,3 +22,15 @@ issue807.p4(26): [--Werror=type-error] error: c1: cannot declare variables of ty
issue807.p4(4)
control C1();
^^
issue807.p4(32): [--Werror=type-error] error: c1: argument cannot have type control C1
e.set1(c1);
^^
issue807.p4(4)
control C1();
^^
issue807.p4(33): [--Werror=type-error] error: c2: argument cannot have type control C2
e.set2(c2);
^^
issue807.p4(5)
control C2();
^^
6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/methodArgDontcare.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
methodArgDontcare.p4(9): [--Werror=type-error] error: 0: Could not infer a type for parameter arg (inferred type is don't care '_')
e.f(0);
^
methodArgDontcare.p4(3)
void f(in T arg);
^^^
15 changes: 0 additions & 15 deletions testdata/p4_16_samples_outputs/methodArgDontcare-first.p4

This file was deleted.

15 changes: 0 additions & 15 deletions testdata/p4_16_samples_outputs/methodArgDontcare-frontend.p4

This file was deleted.

24 changes: 0 additions & 24 deletions testdata/p4_16_samples_outputs/methodArgDontcare-midend.p4

This file was deleted.

Empty file.

0 comments on commit facc5ed

Please sign in to comment.