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 casts to struct types #3234

Merged
merged 1 commit into from
Apr 21, 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
10 changes: 4 additions & 6 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2605,11 +2605,7 @@ const IR::Node* TypeInference::postorder(IR::Cast* expression) {
setType(result, st);
return result;
} else {
auto sit = getTypeType(se->type);
if (typeMap->equivalent(st, sit))
return expression->expr;
else
typeError("%1%: cast not supported", expression->destType);
typeError("%1%: cast not supported", expression->destType);
return expression;
}
} else if (auto le = expression->expr->to<IR::ListExpression>()) {
Expand All @@ -2621,8 +2617,10 @@ const IR::Node* TypeInference::postorder(IR::Cast* expression) {
auto src = assignment(expression, fieldI->type, compI);
vec.push_back(new IR::NamedExpression(fieldI->name, src));
}
auto setype = castType->getP4Type();
setType(setype, new IR::Type_Type(st));
auto result = new IR::StructExpression(
le->srcInfo, castType->getP4Type(), vec);
le->srcInfo, setype, setype, vec);
setType(result, st);
return result;
} else {
Expand Down
15 changes: 15 additions & 0 deletions testdata/p4_16_errors/issue3233.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct s
{
bit t;
bit t1;
}

s func(bit t, bit t1)
{
return (s)(s)(s){t, t1};
}

s func1(s t1)
{
return (s)(s)(s)t1;
}
11 changes: 11 additions & 0 deletions testdata/p4_16_errors_outputs/issue3233.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct s {
bit<1> t;
bit<1> t1;
}

s func(bit<1> t, bit<1> t1) {
return (s)(s)(s){ t, t1 };
}
s func1(s t1) {
return (s)(s)(s)t1;
}
6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/issue3233.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issue3233.p4(9): [--Werror=type-error] error: s: cast not supported
return (s)(s)(s){t, t1};
^
issue3233.p4(14): [--Werror=type-error] error: s: cast not supported
return (s)(s)(s)t1;
^